Skip to content

Commit

Permalink
docs(recipe): use extensions field for persisted queries [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Jul 8, 2021
1 parent e2036af commit f2a92af
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ useServer(
```typescript
// 🛸 server

import { parse } from 'graphql';
import { parse, ExecutionArgs } from 'graphql';
import ws from 'ws'; // yarn add ws
import { useServer } from 'graphql-ws/lib/use/ws';
import { schema } from './my-graphql-schema';
Expand All @@ -1397,15 +1397,19 @@ const wsServer = new ws.Server({
useServer(
{
onSubscribe: (_ctx, msg) => {
const query = queriesStore[msg.payload.query];
if (!query) {
// for extra security you only allow the queries from the store
throw new Error('404: Query Not Found');
const persistedQuery =
queriesStore[msg.payload.extensions?.persistedQuery];
if (persistedQuery) {
return {
...persistedQuery,
variableValues: msg.payload.variables, // use the variables from the client
};
}
return {
...query,
variableValues: msg.payload.variables, // use the variables from the client
};

// for extra security you only allow the queries from the store.
// if you want to support both, simply remove the throw below and
// graphql-ws will handle the query for you
throw new Error('404: Query Not Found');
},
},
wsServer,
Expand All @@ -1429,7 +1433,10 @@ const client = createClient({
await new Promise((resolve, reject) => {
client.subscribe(
{
query: 'iWantTheGreetings',
query: '', // query field is required, but you can leave it empty for persisted queries
extensions: {
persistedQuery: 'iWantTheGreetings',
},
},
{
next: onNext,
Expand Down

0 comments on commit f2a92af

Please sign in to comment.