-
Hi ! I'm currently experiencing with urql for an app that uses Hasura for the graphql backend. In my app there is a webchat that relies on a subscription to get the messages and a mutation to insert them. using the offlineExchange and an optimistic answer I'm able to store a message that is sent while offline and then have it sent when the connexion comes back. Although, when offline I thought that using a Here's how I'm handling the provider: const GraphQLProvider = ({ children }) => {
const { auth, signed_in } = useAuth();
const token = useMemo(() => auth?.getJWTToken(), [auth, signed_in]);
const { client, subscriptionClient } = useMemo(() => {
const subscriptionClient = new SubscriptionClient(WS_ENDPOINT, {
reconnect: true,
connectionParams: () => ({
headers: get_headers(auth),
}),
});
const client = new Client({
url: HTTP_ENDPOINT,
requestPolicy: 'cache-and-network',
fetchOptions: () => {
return {
headers: get_headers(auth),
};
},
exchanges: [
devtoolsExchange,
dedupExchange,
offlineExchange({
schema,
storage,
updates: {},
optimistic: {
// The name of the field
insert_messages_one: (variables, cache, info) => {
const fragment = gql`
fragment _ on messages {
id
sender_id
manager_id
volunteer_id
trial_id
created_at
content
}
`;
const temp_message = {
__typename: 'messages',
id: new Date().getTime(),
created_at: new Date().getTime(),
...variables.object,
};
cache.writeFragment(fragment, temp_message);
return temp_message;
},
},
}),
fetchExchange,
subscriptionExchange({
forwardSubscription: (operation) =>
subscriptionClient.request(operation),
}),
],
});
return { client, subscriptionClient };
}, [auth, token, schema]);
return <Provider value={client}>{children}</Provider>;
}; Any idea of what I might have done or understood wrong ? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
It looks like that write is redundant and in the wrong place? The write isn't needed if you emulate your mutation but you do need to have the message added to a list for it to show up probably https://formidable.com/open-source/urql/docs/graphcache/cache-updates/ |
Beta Was this translation helpful? Give feedback.
It looks like that write is redundant and in the wrong place? The write isn't needed if you emulate your mutation but you do need to have the message added to a list for it to show up probably https://formidable.com/open-source/urql/docs/graphcache/cache-updates/