Skip to content

Commit

Permalink
APQ: config override error with status code 200
Browse files Browse the repository at this point in the history
  • Loading branch information
gilgardosh committed Oct 24, 2023
1 parent b5cbf30 commit 2164cf5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-frogs-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-yoga/plugin-apq': patch
---

Enhance config: enable override error status code with 200
13 changes: 10 additions & 3 deletions packages/plugins/apq/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ export interface APQOptions {
str: string,
api: { crypto: Crypto; TextEncoder: typeof TextEncoder },
) => PromiseOrValue<string>;
responseConfig?: {
/**
* If true, the status code of the response (if the query
* is not found or mismatched) will be 200.
*/
forceStatusCodeOk?: boolean;
};
}

export interface APQStore {
Expand Down Expand Up @@ -70,7 +77,7 @@ function decodeAPQExtension(
}

export function useAPQ(options: APQOptions = {}): Plugin {
const { store = createInMemoryAPQStore(), hash = hashSHA256 } = options;
const { store = createInMemoryAPQStore(), hash = hashSHA256, responseConfig = {} } = options;

return {
async onParams({ params, setParams, fetchAPI }) {
Expand All @@ -86,7 +93,7 @@ export function useAPQ(options: APQOptions = {}): Plugin {
throw createGraphQLError('PersistedQueryNotFound', {
extensions: {
http: {
status: 404,
status: responseConfig.forceStatusCodeOk ? 200 : 404,
},
code: 'PERSISTED_QUERY_NOT_FOUND',
},
Expand All @@ -102,7 +109,7 @@ export function useAPQ(options: APQOptions = {}): Plugin {
throw createGraphQLError('PersistedQueryMismatch', {
extensions: {
http: {
status: 400,
status: responseConfig.forceStatusCodeOk ? 200 : 400,
},
code: 'PERSISTED_QUERY_MISMATCH',
},
Expand Down
51 changes: 51 additions & 0 deletions website/src/pages/docs/features/automatic-persisted-queries.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,54 @@ server.listen(4000, () => {
```

For external stores the `set` and `get` properties on the store can also return a `Promise`.

## Configure Error responses

By default, responses for missing or mismatching query will include `extensions` property with HTTP
status code.

For example:

```ts {4}
{
extensions: {
http: {
status: 404
},
code: 'PERSISTED_QUERY_NOT_FOUND'
}
}
```

You can force the error responses to use 200 OK status code:

```ts filename="Automatic Persisted Operations with a custom store" {18-20}
import { createServer } from 'node:http'
import { createSchema, createYoga } from 'graphql-yoga'
import { useAPQ } from '@graphql-yoga/plugin-apq'

// Note: this store grows infinitely, so it is not a good idea to use it in production.
const store: APQStore = new Map()

const yoga = createYoga({
schema: createSchema({
typeDefs: /* GraphQL */ `
type Query {
hello: String!
}
`
}),
plugins: [
useAPQ({
responseConfig: {
forceStatusCodeOk: true
}
})
]
})

const server = createServer(yoga)
server.listen(4000, () => {
console.info('Server is running on http://localhost:4000/graphql')
})
```

0 comments on commit 2164cf5

Please sign in to comment.