-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[cacheRedirects/typePolicies] Retrieving fragments from the cache and returning an array of references #6278
Comments
I think the answer here is the new InMemoryCache({
typePolicies: {
Query: {
fields: {
messages(_, { args, toReference, readField }) {
return readField("messages", toReference({
__typename: "Chat",
id: args.id,
}));
},
},
},
},
}) If you pass only a field name to |
Thanks @benjamn for the quick answer. Unfortunately
And it looks something like this:
I'm not sure if |
@darkbasic With PR #6306 (included in new InMemoryCache({
typePolicies: {
Query: {
fields: {
messages(_, { args, toReference, readField }) {
return readField({
fieldName: "messages",
args: { last, before },
from: toReference({
__typename: "Chat",
id: args.id,
}),
});
},
},
},
},
}) Let me know if you run into any problems with that style in your real application, since I definitely intend for |
Thanks @benjamn I tried beta.52 and it works fine! To everyone reading please note that |
Hey @darkbasic and @benjamn. For the same example above, how would one add the ability to read a message by id from the cache as opposed to getting the list of messages? I have a similar situation, so I think the example above works for the purposes of my question. Since readFragment doesn't work in the typePolicies, is there another direct way of getting the message without using new InMemoryCache({
typePolicies: {
Query: {
fields: {
readMessageById(_, { args, toReference, readField }) { // define custom local field
const messages = readField({
fieldName: "messages",
args: { last, before },
from: toReference({
__typename: "Chat",
id: args.id,
}),
});
return messages?.find({id} => args.messageId);
});
},
},
},
},
}) |
I've resolved to using readFragment outside of typePolicies for this scenario which works fine and seems like the proper way to do this. |
typePolicies are really easy if you just fetched a list of Chats and then want to query one of them:
But what if you fetched a Chat along with all its messages
and now you want to query all messages for that given chat?
The first thing which came to my mind was something like this:
But unfortunately:
typePolicies
, which leads me to think that I shouldn't be supposed to do so. The workaround is to access it through the variable name I used to instantiate the Apollo client.readFragment
from withintypePolicies
always returnsnull
. Doing so from a React Component works flawlessly.So, is something like this even supported? I hope so, because otherwise the whole
cacheRedirects
/typePolicies
thing would be pretty useless.I'm using
@apollo/[email protected]
.The text was updated successfully, but these errors were encountered: