Skip to content

Commit

Permalink
Go
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Jul 30, 2022
1 parent 0d0e8ab commit 38d7d34
Showing 1 changed file with 116 additions and 78 deletions.
194 changes: 116 additions & 78 deletions packages/graphql-yoga/__tests__/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,84 +220,6 @@ describe('Masked Error Option', () => {
const body = JSON.parse(response.text)
expect(body.errors?.[0]?.message).toBe('Unexpected error.')
})
it('should respect the highest status code if there are many errors thrown with different HTTP status codes', async () => {
const yoga = createYoga({
schema: {
typeDefs: /* GraphQL */ `
type Query {
secret: String
inaccessibleOnDb: String
}
`,
resolvers: {
Query: {
secret: () => {
throw createGraphQLError('You cannot access this secret', {
extensions: {
http: {
status: 401,
},
},
})
},
inaccessibleOnDb: () => {
throw createGraphQLError('DB is not available', {
extensions: {
http: {
status: 503,
},
},
})
},
},
},
},
})

const response = await request(yoga).post('/graphql').send({
query: '{ secret inaccessibleOnDb }',
})

expect(response.statusCode).toBe(503)

const body = JSON.parse(response.text)
expect(body).toMatchInlineSnapshot(`
Object {
"data": Object {
"inaccessibleOnDb": null,
"secret": null,
},
"errors": Array [
Object {
"extensions": Object {},
"locations": Array [
Object {
"column": 3,
"line": 1,
},
],
"message": "You cannot access this secret",
"path": Array [
"secret",
],
},
Object {
"extensions": Object {},
"locations": Array [
Object {
"column": 10,
"line": 1,
},
],
"message": "DB is not available",
"path": Array [
"inaccessibleOnDb",
],
},
],
}
`)
})
})

describe('Context error', () => {
Expand Down Expand Up @@ -406,6 +328,122 @@ describe('Context error', () => {
})
})

describe('HTTP Error Extensions', () => {
it('should respect the status code and headers given with the thrown error during the execution', async () => {
const yoga = createYoga({
schema: {
typeDefs: /* GraphQL */ `
type Query {
hello: String
}
`,
resolvers: {
Query: {
hello() {
throw new GraphQLError('Some random error!', {
extensions: {
http: {
status: 401,
headers: {
'WWW-Authenticate': 'Bearer',
},
},
},
})
},
},
},
},
logging: false,
})

const response = await request(yoga).post('/graphql').send({
query: '{ hello }',
})

expect(response.statusCode).toBe(401)
expect(response.headers['www-authenticate']).toBe('Bearer')
})
it('should respect the highest status code if there are many errors thrown with different HTTP status codes', async () => {
const yoga = createYoga({
schema: {
typeDefs: /* GraphQL */ `
type Query {
secret: String
inaccessibleOnDb: String
}
`,
resolvers: {
Query: {
secret: () => {
throw createGraphQLError('You cannot access this secret', {
extensions: {
http: {
status: 401,
},
},
})
},
inaccessibleOnDb: () => {
throw createGraphQLError('DB is not available', {
extensions: {
http: {
status: 503,
},
},
})
},
},
},
},
})

const response = await request(yoga).post('/graphql').send({
query: '{ secret inaccessibleOnDb }',
})

expect(response.statusCode).toBe(503)

const body = JSON.parse(response.text)
expect(body).toMatchInlineSnapshot(`
Object {
"data": Object {
"inaccessibleOnDb": null,
"secret": null,
},
"errors": Array [
Object {
"extensions": Object {},
"locations": Array [
Object {
"column": 3,
"line": 1,
},
],
"message": "You cannot access this secret",
"path": Array [
"secret",
],
},
Object {
"extensions": Object {},
"locations": Array [
Object {
"column": 10,
"line": 1,
},
],
"message": "DB is not available",
"path": Array [
"inaccessibleOnDb",
],
},
],
}
`)
})
})

it('parse error is sent to clients', async () => {
const yoga = createYoga({
logging: false,
Expand Down

0 comments on commit 38d7d34

Please sign in to comment.