Replies: 3 comments 1 reply
-
I don't have an answer for this - but I'm looking for the exact same thing, not sure why it hasn't come up sooner. Other than a custom endpoint there doesn't seem to be a simple configuration available. The use case is similar, only needing certain fields like title, description when returning a list in a collection, ignoring other data from the same level. |
Beta Was this translation helpful? Give feedback.
-
It looks like the https://payloadcms.com/docs/hooks/collections#afterread // file: `src/collections/Something`
export const Something: CollectionConfig = {
fields: [
...
]
...
hooks: {
afterRead: [
(async ({doc, req, query, findMany}) => {
if (req.user != null) return doc; // ignore hook for logged in users
const docCopy = {...doc};
// do not return createdAt field for any api calls
// delete docCopy.createdAt;
if (findMany) {
// delete fields when listing collection
delete docCopy.richText;
} else {
// delete fields when single item is queried
}
return docCopy;
}) as CollectionAfterReadHook
],
},
...
} |
Beta Was this translation helpful? Give feedback.
-
This plugin contains Select - Rest API From readme:
Is it? I didn't use but sadly probably works only for 0 depth (so can't exclude field inside blocks array relations etc), i'll check it |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
So, this seems like something that should've been answered or mentioned somewhere before, but I cannot find anything on the official docs or by searching and reading through the Q&A section of the forums here.
Is there a way to filter / exclude fields from the REST API response when. querying the collection, like when doing:
I want only certain fields to be returned, like maybe imagined
title
,publishedAt
andcoverImage
.Imagine I also have a
content
field that is of typerichText
. I don't want that to be returned for when I just want to list the collection items and show theircoverImage
andpublishedAt
date.I cannot really do
depth=0
, because I want to go in withdepth: 1
for thecoverImage
field and also, it does not remove most of the text for therichText
fields within the collection.As far as I understand, my only option now is to create a separate endpoint for this on the collection and then do the exclusion myself.
Beta Was this translation helpful? Give feedback.
All reactions