Replies: 2 comments 1 reply
-
Hey @hdodov thanks for bringing this up. I am unsure what the answer is. Here is a simplified version, disregarding RTE all together that has the same outcome: export const PostsCollection = {
slug: 'posts',
hooks: {
afterRead: [
({ doc }) => {
const tagName = doc?.tag ? doc.tag.name : null;
// ^ issue is here, GraphQL sets depth to 0 _always_, REST is set to 1 by default
// so REST and GraphQL return different data
return {
...doc,
tagName,
};
},
],
},
fields: [
{
name: 'tag',
type: 'relationship',
relationTo: 'tags',
},
{
type: 'text',
name: 'tagName',
},
],
}; In REST a {
"docs": [
{
"id": "post-id",
"tag": {
"id": "tag-id",
"name": "tag-name",
},
"tagName": "tag-name",
},
]
} A GraphQL like so: query {
Posts {
docs {
id
tag {
id
name
}
tagName
}
}
} will return: {
"docs": [
{
"id": "post-id",
"tag": {
"id": "tag-id",
"name": "tag-name",
},
"tagName": null,
},
]
} In order to align them, we would need to pass depth through from the GraphQL operation wrappers. Is this something we want to do? I imagine we explicitly set it to I brought it up in our core-dev discord channel too. |
Beta Was this translation helpful? Give feedback.
-
OK so I just looked into this and I have some answers. First up, this is going to be quite difficult to achieve, but it should be possible to implement and I do believe we should implement a way to flatten this logic. The discrepancy is caused because in REST and Local APIs, we leverage the The GraphQL field resolvers for To match REST to GraphQL here, we need to remove the "population" logic from field resolvers themselves and introduce a way to pass the GraphQL We would then recurse into the GraphQL This will be a relatively large lift, and I'm not sure that we will be able to commit to this within the coming month or so. Afterward, I do think that this will be a codebase simplification actually, so I think it's worth it to tackle. Is there another way to accomplish what you're looking to do in the meantime? I'm going to add this to our Roadmap, but in the meantime, I think we should figure out another workaround for you here. What do you think? |
Beta Was this translation helpful? Give feedback.
-
Link to reproduction
https://github.com/hdodov/payload/tree/bug-read-hook-population
To Reproduce
npm run test:int _community
(or Yarn)Describe the Bug
Context:
afterRead
hook that does something with the URL of the uploaded media file in the richtext field, and therefore expects that the media file data is populatedProblem: The media file data is not populated when requested with the GraphQL API, even though it works as expected when using REST.
Here's what the hook looks like:
https://github.com/hdodov/payload/blob/bug-read-hook-population/test/_community/collections/Posts/index.ts#L17-L35
When performing a REST request, the document in the hook looks like this:
However, when performing the GraphQL request, the document looks like this:
I thought that I had to specify the
depth
option, but as the docs say:…so I guess it's a bug, then. Here's my query:
It should be noted, however, that the document is populated in GraphQL, but only in the final result of the query:
It's just that it's populated after the
afterRead
hook, so the hook has no access to the data. Whereas in REST, the document is populated before theafterRead
hook fires, so the hook can work with the data.Payload Version
1.11.2
Beta Was this translation helpful? Give feedback.
All reactions