-
Notifications
You must be signed in to change notification settings - Fork 33
Question: Does apollo-link-persisted-queries support apollo-link-ws ? #18
Comments
For those looking for the answer to this, my investigation suggests no at the moment. Or at least no if you use Firstly if (!query) {
throw new Error('Must provide a query.');
} So, even when you do add a persisted queries link in front of const client = new ApolloClient({
cache,
typeDefs,
resolvers,
link: createPersistedQueryLink().concat(new WebSocketLink(subscriptionClient)),
}); Secondly, if you write a middleware to strip the const subscriptionClient = new SubscriptionClient(`${BACKEND}/graphql`, {
reconnect: true,
}).use([
{
applyMiddleware: (operation, next) => {
if (operation.extensions?.persistedQuery) {
// as this middleware has to mutate the existing object
// this is the only option
// eslint-disable-next-line no-param-reassign
delete operation.query;
}
next();
},
},
]); and you patch This seems to be missing feature in I am going to look into putting a PR in to make |
There is my workaround to use persisted query with subscription: /**
* workaround to use persisted query with subscription
* https://github.com/apollographql/apollo-link-persisted-queries/issues/18
*/
function patchSubscriptionClient(client: SubscriptionClient) {
// make client to respect
// `operation.getContext().http.includeQuery`
// so apq link can work
client.use([
{
applyMiddleware: (operation, next) => {
if (operation.query) {
operation.setContext({ query: operation.query });
}
const ctx = operation.getContext();
const includeQuery: boolean | undefined = ctx?.http?.includeQuery;
if (includeQuery) {
operation.query = ctx.query;
} else {
delete operation.query;
}
next();
},
},
]);
// allow empty query
const c = (client as unknown) as Record<string, unknown>;
const raw = c.checkOperationOptions as (...args: unknown[]) => unknown;
c.checkOperationOptions = (...args: unknown[]) => {
try {
return raw(...args);
} catch (err) {
if (err instanceof Error && err.message === 'Must provide a query.') {
return;
}
throw err;
}
};
} |
And if yes, can someone provide an example?
The text was updated successfully, but these errors were encountered: