Connections for Subscriptions over Multipart HTTP never close in React #3772
-
Hello! I created a subscription using subscriptionExchange({
forwardSubscription: createFetchMultipartSubscription(endpoint, {
fetch: (input, init) =>
fetch(input, {
...init,
credentials: 'include',
}),
}),
}), It appears that Is this something that URQL should be able to handle itself? Thank you - I'll look for a solution in the meantime. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Update: I wrote this custom hook that uses a custom /**
* This hook ensures a subscription is cancelled when the component unmounts.
* It is particularly useful for subscriptions over multipart HTTP, as it prevents connections from outliving the component.
* Its interface is the same as URQL's useSubscription hook.
*
* @param args - The arguments to be passed to the useSubscription hook.
* @returns A tuple containing the result of the useSubscription hook and the executeSubscription function.
*/
export const useCancellingSubscription = <
Data = any,
Result = Data,
Variables extends AnyVariables = AnyVariables,
>(
args: UseSubscriptionArgs<Variables, Data>,
): UseSubscriptionResponse<Result, Variables> => {
const [data, executeSubscription] = useSubscription<Data, Result, Variables>({
...args,
pause: true,
});
const { pause } = args;
useEffect(() => {
const abort = new AbortController();
if (!pause) {
executeSubscription({
fetch: (input, init) => {
return fetch(input, {
...init,
signal: abort.signal,
});
},
});
}
return () => {
abort.abort();
};
}, [executeSubscription, pause]);
return [data, executeSubscription];
}; And adjusted the URQL provider to accept a custom fetch function via operation context: subscriptionExchange({
forwardSubscription: (request, operation) => {
return createFetchMultipartSubscription(endpoint, {
fetch: (input, init) => {
// Use a custom fetch function if available (e.g. for a custom signal / abort controller; see `useCancellingSubscription`)
const fetchFn = operation.context.fetch || fetch;
return fetchFn(input, {
...init,
credentials: 'include',
});
},
})(request);
},
}), |
Beta Was this translation helpful? Give feedback.
-
This was fixed upstream in the apollo fetch multipart subscription URQL plugin here: apollographql/apollo-client#12567 |
Beta Was this translation helpful? Give feedback.
This was fixed upstream in the apollo fetch multipart subscription URQL plugin here: apollographql/apollo-client#12567