I’m working with Apollo Client and React.
The question is how to make Apollo realise that getClass is looking for one element into the result of getClasses.
For example, I have a list of all classes that getClasses return, then when the user clicks on the link it shows the data of that class (ShowDetails). The problem is that right now Apollo is making another httpRequest to fetch the data that is already in the cache.
Of course, that I can pass the data with props, but I want the child component to be independent of the parent. If the user access directly to the ShowDetails I need to make a request, but if he access using the link I want to use the cached data.
This makes sense or I missing something ?
const CLASS_FRAGMENT = gql`
fragment ClassFields on Class {
id
date
hour
name
activity
}
`;
export const GET_ALL_CLASSES = gql`
${CLASS_FRAGMENT}
query getClasses {
classes {
...ClassFields
}
}
`;
export const GET_CLASS = gql`
${CLASS_FRAGMENT}
query getClass($id: ID!) {
getClasses(id: $id) {
...ClassFields
}
}
`;
This is the configuration for Apollo Client
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
groups: {
merge(existing = [], incoming) {
return incoming;
},
},
classes: {
merge(existing = [], incoming) {
return incoming;
},
},
students: {
merge(existing = [], incoming) {
return incoming;
},
},
payments: {
merge(existing = [], incoming) {
return incoming;
},
},
},
},
Group: {
keyFields: ["id"],
},
Class: {
keyFields: ["id"],
},
Affiliate: {
keyFields: ["id"],
},
Payment: {
keyField: ["id"],
},
},
});
export const useApolloClient = () => {
return new ApolloClient({
link: from([errorLink, httpLink]),
cache,
});
};
Thanks!