Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: context error masking #1604

Merged
merged 6 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@graphql-yoga/plugin-response-cache": patch
---

dependencies updates:

- Updated dependency [`@envelop/response-cache@^3.1.0` ↗︎](https://www.npmjs.com/package/@envelop/response-cache/v/null) (from `^3.0.0`, in `dependencies`)
9 changes: 9 additions & 0 deletions .changeset/graphql-yoga-1604-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"graphql-yoga": patch
---

dependencies updates:

- Updated dependency [`@envelop/core@^2.5.0` ↗︎](https://www.npmjs.com/package/@envelop/core/v/null) (from `^2.4.1`, in `dependencies`)
- Updated dependency [`@envelop/parser-cache@^4.6.0` ↗︎](https://www.npmjs.com/package/@envelop/parser-cache/v/null) (from `^4.4.0`, in `dependencies`)
- Updated dependency [`@envelop/validation-cache@^4.6.0` ↗︎](https://www.npmjs.com/package/@envelop/validation-cache/v/null) (from `^4.4.0`, in `dependencies`)
2 changes: 1 addition & 1 deletion examples/node-esm/__integration-tests__/esm.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('Node ESM', () => {
}),
})
expect(response.status).toBe(200)
const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.data.greetings).toBe('Hello world!')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ it('should expose Node req and res objects in the context', async () => {
`http://localhost:${port}/graphql?query=query{isNode}`,
)
expect(response.status).toBe(200)
const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.errors).toBeUndefined()
expect(body.data.isNode).toBe(true)
} finally {
Expand Down
66 changes: 54 additions & 12 deletions packages/graphql-yoga/__tests__/error-masking.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('error masking', () => {
body: JSON.stringify({ query: '{ hi hello }' }),
})

const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.data.hi).toBeNull()
expect(body.errors![0].message).toBe('Unexpected error.')
expect(body.data.hello).toBeNull()
Expand All @@ -57,7 +57,7 @@ describe('error masking', () => {
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ query: '{ hi hello }' }),
})
const body = JSON.parse(await response.text())
const body = await response.json()

expect(body.data.hi).toBeNull()
expect(body.errors![0].message).toBe('Hahahaha')
Expand All @@ -77,7 +77,7 @@ describe('error masking', () => {
body: JSON.stringify({ query: '{ hi hello }' }),
})

const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.data.hi).toBeNull()
expect(body.errors![0].message).toBe('Unexpected error.')
expect(body.data.hello).toBeNull()
Expand All @@ -99,7 +99,7 @@ describe('error masking', () => {
body: JSON.stringify({ query: '{ hi hello }' }),
})

const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.data.hi).toBeNull()
expect(body.errors?.[0]?.message).toBe('Unexpected error.')
expect(body.errors?.[0]?.extensions).toStrictEqual({
Expand Down Expand Up @@ -129,7 +129,7 @@ describe('error masking', () => {
body: JSON.stringify({ query: '{ hi hello }' }),
})

const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.data.hi).toBeNull()
expect(body.errors?.[0]?.message).toBe('Unexpected error.')
expect(body.errors?.[0]?.extensions).toStrictEqual({
Expand Down Expand Up @@ -165,7 +165,7 @@ describe('error masking', () => {

expect(response.status).toBe(500)

const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.errors?.[0]?.message).toBe('Unexpected error.')
})

Expand All @@ -184,7 +184,7 @@ describe('error masking', () => {
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ query: '{ __typename }' }),
})
const body = JSON.parse(await response.text())
const body = await response.json()
expect(body).toMatchInlineSnapshot(`
Object {
"errors": Array [
Expand All @@ -210,7 +210,7 @@ describe('error masking', () => {
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ query: '{ __typename }' }),
})
const body = JSON.parse(await response.text())
const body = await response.json()
expect(body).toMatchInlineSnapshot(`
Object {
"errors": Array [
Expand All @@ -236,7 +236,7 @@ describe('error masking', () => {
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ query: '{ __typename }' }),
})
const body = JSON.parse(await response.text())
const body = await response.json()
expect(body).toMatchInlineSnapshot(`
Object {
"errors": Array [
Expand Down Expand Up @@ -272,7 +272,7 @@ describe('error masking', () => {
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ query: '{ greetings }' }),
})
const body = JSON.parse(await response.text())
const body = await response.json()
expect(body).toMatchInlineSnapshot(`
Object {
"errors": Array [
Expand Down Expand Up @@ -309,7 +309,7 @@ describe('error masking', () => {
})

expect(response.status).toEqual(400)
const body = JSON.parse(await response.text())
const body = await response.json()

expect(body).toMatchInlineSnapshot(`
Object {
Expand Down Expand Up @@ -349,7 +349,7 @@ describe('error masking', () => {
})

expect(response.status).toEqual(400)
const body = JSON.parse(await response.text())
const body = await response.json()

expect(body).toMatchInlineSnapshot(`
Object {
Expand All @@ -367,4 +367,46 @@ describe('error masking', () => {
}
`)
})

it('error thrown within context factory is exposed via originalError extension field in dev mode', async () => {
const yoga = createYoga({
logging: false,
context: () => {
throw new Error('I am the original error.')
},
maskedErrors: {
isDev: true,
},
schema: createSchema({
typeDefs: /* GraphQL */ `
type Query {
a: String!
}
`,
}),
})
const response = await yoga.fetch('http://yoga/graphql', {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({ query: '{a}' }),
})

const body = await response.json()
expect(body).toStrictEqual({
errors: [
{
message: 'Unexpected error.',
extensions: {
originalError: {
message: 'I am the original error.',
stack: expect.stringContaining('Error: I am the original error.'),
},
},
},
],
})
expect(response.status).toEqual(500)
})
})
2 changes: 1 addition & 1 deletion packages/graphql-yoga/__tests__/http-extensions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('GraphQLError.extensions.http', () => {

expect(response.status).toBe(503)

const body = JSON.parse(await response.text())
const body = await response.json()
expect(body).toMatchObject({
data: {
a: null,
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-yoga/__tests__/introspection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('introspection', () => {
})

expect(response.status).toBe(200)
const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.errors).toBeUndefined()
expect(body.data?.__schema.queryType.name).toBe('Query')
})
Expand Down
28 changes: 14 additions & 14 deletions packages/graphql-yoga/__tests__/requests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('requests', () => {
})

expect(response.status).toBe(200)
const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.errors).toBeUndefined()
expect(body.data.ping).toBe('pong')
})
Expand All @@ -57,7 +57,7 @@ describe('requests', () => {
)

expect(response.status).toBe(200)
const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.errors).toBeUndefined()
expect(body.data.ping).toBe('pong')
})
Expand All @@ -75,7 +75,7 @@ describe('requests', () => {
expect(response.status).toBe(405)

expect(response.headers.get('allow')).toEqual('POST')
const body = JSON.parse(await response.text())
const body = await response.json()

expect(body.data).toBeUndefined()
expect(body.errors).toHaveLength(1)
Expand All @@ -98,7 +98,7 @@ describe('requests', () => {
})

expect(response.status).toBe(200)
const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.errors).toBeUndefined()
expect(body.data.echo).toBe(null)
})
Expand All @@ -118,7 +118,7 @@ describe('requests', () => {
})

expect(response.status).toBe(200)
const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.errors).toBeUndefined()
expect(body.data.echo).toBe('hello')
})
Expand All @@ -131,7 +131,7 @@ describe('requests', () => {
})
expect(response.status).toBe(400)

const body = JSON.parse(await response.text())
const body = await response.json()

expect(body.errors).toBeDefined()
expect(body.data).toBeUndefined()
Expand All @@ -145,7 +145,7 @@ describe('requests', () => {
})
expect(response.status).toBe(400)

const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.errors).toBeDefined()
expect(body.errors[0].message).toEqual('POST body sent invalid JSON.')

Expand All @@ -161,7 +161,7 @@ describe('requests', () => {

expect(response.status).toBe(400)

const body = JSON.parse(await response.text())
const body = await response.json()

expect(body.errors).toBeDefined()
expect(body.data).toBeUndefined()
Expand All @@ -176,7 +176,7 @@ describe('requests', () => {

expect(response.status).toBe(400)

const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.data).toBeUndefined()
expect(body.errors?.[0].message).toBe('Must provide query string.')
})
Expand All @@ -190,7 +190,7 @@ describe('requests', () => {

expect(response.status).toBe(400)

const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.data).toBeUndefined()
expect(body.errors?.[0].message).toBe(
'Expected "query" param to be a string, but given object.',
Expand Down Expand Up @@ -224,7 +224,7 @@ describe('requests', () => {
})

expect(response.status).toBe(200)
const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.errors).toBeUndefined()
expect(body.data.ping).toBe('pong')
})
Expand All @@ -239,7 +239,7 @@ describe('requests', () => {
})

expect(response.status).toBe(200)
const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.errors).toBeUndefined()
expect(body.data.ping).toBe('pong')
})
Expand All @@ -254,7 +254,7 @@ describe('requests', () => {
})

expect(response.status).toBe(200)
const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.errors).toBeUndefined()
expect(body.data.ping).toBe('pong')
})
Expand All @@ -269,7 +269,7 @@ describe('requests', () => {
})

expect(response.status).toBe(400)
const body = JSON.parse(await response.text())
const body = await response.json()
expect(body.data).toBeUndefined()
expect(body.errors?.[0].message).toBe(
'Unexpected parameter "test" in the request body.',
Expand Down
10 changes: 5 additions & 5 deletions packages/graphql-yoga/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
"dependencies": {
"@graphql-tools/code-file-loader": "^7.3.0",
"@graphql-tools/mock": "^8.7.0",
"@envelop/core": "^2.4.1",
"@envelop/parser-cache": "^4.4.0",
"@envelop/validation-cache": "^4.4.0",
"@envelop/core": "^2.5.0",
"@envelop/parser-cache": "^4.6.0",
"@envelop/validation-cache": "^4.6.0",
"@graphql-typed-document-node/core": "^3.1.1",
"@graphql-tools/schema": "^9.0.0",
"@graphql-tools/utils": "^8.8.0",
Expand All @@ -72,8 +72,8 @@
"devDependencies": {
"@types/node": "16.11.56",
"html-minifier-terser": "7.0.0",
"@envelop/disable-introspection": "^3.4.0",
"@envelop/live-query": "^4.0.0",
"@envelop/disable-introspection": "^3.5.0",
"@envelop/live-query": "^4.1.0",
"@types/eventsource": "1.1.9",
"eventsource": "2.0.2",
"graphql": "^16.0.1",
Expand Down
Loading