To implement Persisted Queries, use the onSubscribe hook on the server to look up the query in a store using an ID provided in the client's extensions.persistedQuery field. If found, return the stored ExecutionArgs. On the client, you can leave the query field empty and provide the ID in the extensions.
Server Implementation:
Use onSubscribe to match params.extensions?.persistedQuery against your store.
Client Implementation:
Pass the ID in the extensions object of the subscription request.
// Server
export const handler = createHandler({
onSubscribe: (_req, params) => {
const persistedQuery = queriesStore[String(params.extensions?.persistedQuery)];
if (persistedQuery) {
return {
...persistedQuery,
variableValues: params.variables,
contextValue: undefined,
};
}
return [null, { status: 404, statusText: 'Not Found' }];
},
});
// Client
client.subscribe(
{
query: '',
extensions: {
persistedQuery: 'iWantTheGreetings',
},
},
{
next: onNext,
error: reject,
complete: resolve,
},
);