readFragment - Need help understanding why this example is returning null #1143
-
|
We have a display containing a list of summary information about school visits. In some cases, the full information will be present in the cache; in other cases the user will not have accessed this information yet. The application has a full offline mode. I am attempting to use the resolver to populate a flag field indicating whether or not the full information is in the cache. My test code is: resolvers: {
SampledGradeCard: {
offlineMeta(parent, args, cache, info) {
const data = cache.readFragment(
gql`
fragment _ on SampledGrade {
visitId
}
`,
parent.visitId
)
if (parent.visitId === '2410371+01/25/2021') console.log({ data })
return data === null ? 'not-in-cache' : 'in-cache'
}
}
},Keys are set as follows (abridged): keys: {
SampledGrade: data => data.visitId,
}A small extract of the cache: {
"SampledGrade:2410371+01/25/2021.__typename": "\"SampledGrade\"",
"SampledGrade:2410371+01/25/2021.visitId": "\"2410371+01/25/2021\"",The debug test shows that the visit id I've hardcoded is in the incoming values, but the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
|
As you can see here by Given that and the method the By the way, given this there's a shorter way to do this. You may also use |
Beta Was this translation helpful? Give feedback.
-
|
Thank you @kitten. I was confused by the example in the docs: import gql from 'graphql-tag';
const data = cache.readFragment(
gql`
fragment _ on Todo {
id
text
}
`,
'1'
);Your explanation helps a lot! Do you know if |
Beta Was this translation helpful? Give feedback.
-
|
Thanks! |
Beta Was this translation helpful? Give feedback.
cache.readFragment’s second argument here needs to be a key or a keyable entity, so{ __typename: "SampledGrade", visitId: "..." }if that's your full type orcache.keyOfEntity({ __typename: "SampledGrade", visitId: "..." }).As you can see here by
keyOfEntityis what you may be confused about. A key is not an ID, because we still preserve the type in our key. Essentially that's opinionated but unlike Relay we assert that no two types can share the same entity as they're different, so all keys contain the type name.Given that and the method the
readFragmentmethod only accepts a full key or a partial entity from which a key can be generated.By the way, given this there's a shorter way …