Skip to content

Commit

Permalink
ctx.errors: handles undefined and null values
Browse files Browse the repository at this point in the history
  • Loading branch information
aganglada authored and kettanaito committed Oct 22, 2020
1 parent a1d1c4d commit 327b384
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/context/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@ test('combines with data in the response JSON body', () => {
}),
)
})

test('bypasses undefined errors', () => {
const result = response(errors(undefined), errors(null))

expect(result.headers.get('content-type')).not.toEqual('application/json')
expect(result).toHaveProperty('body', null)
})
14 changes: 10 additions & 4 deletions src/context/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import { ResponseTransformer } from '../response'
import { json } from './json'

/**
* Returns a list of GraphQL errors.
* Sets a given list of GraphQL errors on the mocked response.
*/
export const errors = (
errorsList: Partial<GraphQLError>[],
): ResponseTransformer<{ errors: typeof errorsList }> => {
export const errors = <
ErrorsType extends Partial<GraphQLError>[] | null | undefined
>(
errorsList: ErrorsType,
): ResponseTransformer<{ errors: ErrorsType }> => {
if (errorsList == null) {
return (res) => res
}

return json({ errors: errorsList }, { merge: true })
}

0 comments on commit 327b384

Please sign in to comment.