Skip to content

Commit

Permalink
fix: apply the useHTTPValidationError plugin last (#1600)
Browse files Browse the repository at this point in the history
* test: add failing test

* fix: put the useHTTPValidationError plugin last

* chore: add changeset
  • Loading branch information
n1ru4l committed Aug 16, 2022
1 parent b3798d4 commit 94b41f3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/grumpy-pens-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-yoga/common': patch
'@graphql-yoga/node': patch
---

Apply the HTTP validation error plugin last in order to not interfere error masking when using the `handleValidationErrors` option.
5 changes: 3 additions & 2 deletions packages/common/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,7 @@ export class YogaServer<

// So the user can manipulate the query parameter
useCheckGraphQLQueryParam(),
// We handle validation errors at the end
useHTTPValidationError(),

// We make sure that the user doesn't send a mutation with GET
usePreventMutationViaGET(),

Expand All @@ -395,6 +394,8 @@ export class YogaServer<
typeof maskedErrors === 'object' ? maskedErrors : undefined,
),
),
// We handle validation errors at the end
useHTTPValidationError(),
]

this.getEnveloped = envelop({
Expand Down
39 changes: 39 additions & 0 deletions packages/node/__tests__/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,45 @@ describe('Masked Error Option', () => {
},
})
})

it('can mask validation error', async () => {
const yoga = createServer({
schema,
logging: false,
maskedErrors: {
handleValidationErrors: true,
isDev: true,
},
})

const response = await request(yoga).post('/graphql').send({
query: '{ bubatzbieber }',
})
const body = JSON.parse(response.text)
expect(body).toMatchObject({
data: null,
errors: [
{
locations: [
{
column: 3,
line: 1,
},
],
message: 'Unexpected error.',
},
],
})
const { extensions } = body.errors![0]
expect(extensions).toMatchObject({
originalError: {
message: 'Cannot query field "bubatzbieber" on type "Query".',
stack: expect.stringContaining(
'GraphQLError: Cannot query field "bubatzbieber" on type "Query"',
),
},
})
})
})

describe('Context error', () => {
Expand Down

0 comments on commit 94b41f3

Please sign in to comment.