Skip to content

Commit 3fd1cd1

Browse files
committed
fix: log full response when not a graphql compatible error
1 parent 60bf4d1 commit 3fd1cd1

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

src/BatchedGraphQLClient.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,27 @@ export class BatchedGraphQLClient {
5454

5555
const results = await getResults(response)
5656

57-
if (Array.isArray(results)) {
58-
const allResultsHaveData =
59-
results.filter(r => r.data).length === results.length
60-
61-
if (response.ok && !results.find(r => r.errors) && allResultsHaveData) {
62-
return results.map(r => r.data)
63-
} else {
64-
const errorIndex = results.findIndex(r => r.errors)
65-
const result = results[errorIndex]
66-
const errorResult =
67-
typeof result === 'string' ? { error: result } : result
68-
throw new ClientError({ ...errorResult, status: response.status })
69-
}
70-
} else {
71-
// if it is not an array, there must be an error
57+
// if it is not an array, there must be an error
58+
if (!Array.isArray(results)) {
7259
throw new ClientError({ ...results, status: response.status })
7360
}
61+
62+
// check if there was an error in one of the responses
63+
if (
64+
!response.ok ||
65+
results.some(r => r.errors !== undefined || r.data === undefined)
66+
) {
67+
const errorIndex = results.findIndex(
68+
r => r.errors !== undefined || r.data === undefined,
69+
)
70+
const result = results[errorIndex]
71+
const errorResult =
72+
typeof result === 'string' ? { errors: [{ message: result }] } : result
73+
74+
throw new ClientError({ ...errorResult, status: response.status })
75+
}
76+
77+
return results.map(r => r.data)
7478
}
7579
}
7680

src/ClientError.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { GraphQLResponse, GraphQLRequestContext } from './types'
22

33
export class ClientError extends Error {
44
result: GraphQLResponse
5-
request: GraphQLRequestContext
65

76
constructor(result: GraphQLResponse) {
87
const message = ClientError.extractMessage(result)
@@ -22,7 +21,7 @@ export class ClientError extends Error {
2221
try {
2322
return response.errors![0].message
2423
} catch (e) {
25-
return `GraphQL Error (Code: ${response.status})`
24+
return `GraphQL Error: ${JSON.stringify(response, null, 2)}`
2625
}
2726
}
2827
}

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export type HttpOptions = Options & {
1818

1919
export interface GraphQLError {
2020
message: string
21-
locations: { line: number; column: number }[]
22-
path: string[]
21+
locations?: { line: number; column: number }[]
22+
path?: string[]
2323
}
2424

2525
export interface GraphQLResponse {

0 commit comments

Comments
 (0)