Hey guys, I’m using the @neo4j/graphql library that auto-generates queries and mutations from my schema’s type definitions.
I have a list of posts in my app from the following query:
const POSTS = gql`
query posts($limit:Int) {
posts(
options: {
limit: $limit
}
) {
title
text
}
}
`
Which gives me a list of posts and stores them in the cache.
When a user clicks on the post, he gets redirected to the post’s page where he can see the post’s comments.
Here, I’d like to retrieve the post from the cache with a similar query field:
const POST = gql`
query post($postId:String , $limit:Int) {
posts(
where: {postId:$postId}
options: {
limit: $limit
}
) {
title
text
}
}
`
Even though this is the same posts
query field, adding the where: {postId:$postId}
argument returns an array with only one post. But using this query, apollo doesn’t know how to find the post in the cache.
Therefore I need to define a typePolicy such as:
export const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
posts : {
read(_, { args, toReference }) {
return toReference({
__typename: 'Post',
id: args.id,
});
}
},
}
}
}
})
But here the posts
query field is used both to query the list of posts and individual posts. Resulting in an error.
Is there any way around this? Maybe with the name of the query?