Is it possible to request specific data or fields using REST API?
Like request for title price and slug rather than all the data in list.
Hey
@1126955972439580776,
So generally speaking the issue you are experiencing is called
Overfetchingwhich isn't really an issue unique to Payload. It is simply a limitation of RESTful API's. You have a few options of how to proceed here.
You could create a custom endpoint in your collection which would return only the fields you want:
https://payloadcms.com/docs/beta/rest-api/overview#custom-endpointsYou could also use the GraphQL endpoint which comes with Payload as GraphQL is seen as a direct solution to
Overfetching/
Underfetchingby allowing you to specify specific fields to return.
https://payloadcms.com/docs/beta/graphql/overviewIf neither of these solutions work for you, you could also create a collection
afterReadhook that strips the outgoing response of unnecessary fields, although I would not recommend this approach.
https://payloadcms.com/docs/beta/hooks/collections#afterreadLastly, you could use the
Searchplugin and override the fields that get indexed to only the fields you require and simply use this collection as a proxy.
https://payloadcms.com/docs/beta/plugins/search#optionsI would not recommend this approach either, but it may work for your use-case.
very nice explanation
I shall go with the custom endpoint
Thank you
My pleasure!
need some more help
Here is the endpoint
endpoints: [
{
path: '/list',
method: 'get',
handler: async (req) => {
return Response.json({
message: 'this is endpoint'
})
}
}
],
How I can add the field I want to return?
That would entirely depend on what you're trying to achieve here. You want to perform a query? You can use
req.payloadto get access to the local api.
Have a look here for more:
https://payloadcms.com/docs/beta/rest-api/overview#helpful-tipsAlso look at the example above in that link
I want to perform get request to get the product list with title, slug and price.
Okay, so you would use the local api to fetch your list, then you would loop through the items and strip them of unnecessary fields and return the result.
...
handler: async (req) => {
const data = await req.payload.find({
collection: <your-collection-slug>,
})
// your logic here
return Response.json({
message: 'this is endpoint',
result: <your-sanitized-data-here>
})
}
...See Local API docs here:
https://payloadcms.com/docs/beta/local-api/overviewit worked but I am failing to write a logic to get the specific fields and return it.
I tried to use
const result = data.docs.map(item => item.title)
But it is returning title value like “product title” and not key value pair object as json
Like {“title”:product title}
Any idea, how I can do it?
Yeah, so what you want is something like this:
const result = data.docs.map(item => ({
title: item.title,
slug: item.slug,
price: item.price,
...
}))This will return an array of objects where each object has those fields.
You are awesome man thank you for helping me
It worked perfectly the way I want
My pleasure
there is one issue I just noticed
There is no pagination in this endpoint
No worries fixed it
is it good practice to handle pagination in endpoint?
const data = await req.payload.find({
collection: 'products',
limit: 1,
page: Number(req.searchParams.get('page’))
})
It is working but don’t know if it is good or there is better way
Seems fine to me!
One thing you may consider is implementing a
limitquery parameter so that your
limit: 1can be overriden to get more data
Yes will do that
To extend on this, you can further reduce over fetching data of the database by directly accessing the database adapters (req.payload.db.drizzle for sql databases) and do raw sql statements.
Also I’m sure
@423216344302092288is working on the select feature similar to how Strapi does it.
Yeah, in fact it's done already!
https://github.com/payloadcms/payload/pull/8550Star
Discord
online
Get dedicated engineering support directly from the Payload team.