I'm currently working on a react app, that uses a GraphQL backend and has additional local state. I am using a resolver to resolve a local field that changes over time but the resolver is only triggered once.
I tried to use cache.readQuery to re-run the query in case the local field changes, but it does not seem to work as I expected.
export const resolvers = {
Query: {
resolvedDevice: (obj, args, { cache }, info) => {
const data = cache.readQuery({
query: gql`
query {
selectedDevice @client
}
`
});
// do stuff with the data
}
},
Mutation: {
selectDevice: (_, { id }, { cache }) => {
cache.writeData({ data: { selectedDevice: id } });
}
}
};
const query = gql`
query GetResolvedDevice {
resolvedDevice @client
}
`;
In this case "resolvedDevice" within the resolver is only executed once, even if I mutate the cache via the mutation "selectDevice". I expected that when changing the local state via mutation the resolvers also runs again, because cache is changing.
Here is the code that executes the query:
const ModalContainer = props => {
const { loading, error, data } = useQuery(query);
if (loading || error) {
return null;
}
return (
<Modal
device={data.resolvedDevice}
/>
);
};
And in this component I am running a mutation on selectedDevice:
export const SELECT_DEVICE = gql`
mutation SelectDevice($id: String!) {
selectDevice(id: $id) @client
}
`;
const DevicesNoGeoContainer = () => {
const [selectDevice] = useMutation(SELECT_DEVICE);
return (
<DevicesNoGeo
onGeoClick={id => {
selectDevice({ variables: { id } });
}}
/>
);
};