Skip to content

Commit

Permalink
fix: include headers in ClientError response (graffle-js#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
emckay committed Jan 12, 2021
1 parent 5b233c5 commit 2ea15ac
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class GraphQLClient {
): Promise<{ data?: T; extensions?: any; headers: Dom.Headers; status: number; errors?: GraphQLError[] }> {
let { headers, fetch: localFetch = crossFetch, ...others } = this.options
const body = createRequestBody(query, variables)


const response = await localFetch(this.url, {
method: 'POST',
Expand Down Expand Up @@ -78,8 +78,7 @@ export class GraphQLClient {
/**
* Send a GraphQL document to the server.
*/
async request<T = any, V = Variables>(document: RequestDocument, variables?: V,
requestHeaders?: Dom.RequestInit['headers']): Promise<T> {
async request<T = any, V = Variables>(document: RequestDocument, variables?: V, requestHeaders?: Dom.RequestInit['headers']): Promise<T> {
let { headers, fetch: localFetch = crossFetch, ...others } = this.options
const resolvedDoc = resolveRequestDocument(document)
const body = createRequestBody(resolvedDoc, variables)
Expand All @@ -101,7 +100,10 @@ export class GraphQLClient {
return result.data
} else {
const errorResult = typeof result === 'string' ? { error: result } : result
throw new ClientError({ ...errorResult, status: response.status }, { query: resolvedDoc, variables })
throw new ClientError(
{ ...errorResult, status: response.status, headers: response.headers },
{ query: resolvedDoc, variables }
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ test('basic error', async () => {
const res = await request(ctx.url, `x`).catch((x) => x)

expect(res).toMatchInlineSnapshot(
`[Error: GraphQL Error (Code: 200): {"response":{"errors":{"message":"Syntax Error GraphQL request (1:1) Unexpected Name \\"x\\"\\n\\n1: x\\n ^\\n","locations":[{"line":1,"column":1}]},"status":200},"request":{"query":"x"}}]`
`[Error: GraphQL Error (Code: 200): {"response":{"errors":{"message":"Syntax Error GraphQL request (1:1) Unexpected Name \\"x\\"\\n\\n1: x\\n ^\\n","locations":[{"line":1,"column":1}]},"status":200,"headers":{}},"request":{"query":"x"}}]`
)
})

Expand Down

0 comments on commit 2ea15ac

Please # to comment.