Skip to content

Commit

Permalink
fix: check if property errors from response is an empty array (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
RicsiToth authored Feb 18, 2023
1 parent 3618169 commit 4302ba1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,10 @@ async function makeRequest<T = any, V extends Variables = Variables>({
isBatchingQuery && Array.isArray(result) ? !result.some(({ data }) => !data) : !!result.data

const successfullyPassedErrorPolicy =
!result.errors || fetchOptions.errorPolicy === 'all' || fetchOptions.errorPolicy === 'ignore'
!result.errors ||
(Array.isArray(result.errors) && !result.errors.length) ||
fetchOptions.errorPolicy === 'all' ||
fetchOptions.errorPolicy === 'ignore'

if (response.ok && successfullyPassedErrorPolicy && successfullyReceivedData) {
const { headers, status } = response
Expand Down
4 changes: 2 additions & 2 deletions tests/__helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import body from 'body-parser'
import express, { Application, Request } from 'express'
import getPort from 'get-port'
import { createServer, Server } from 'http'
import { JsonObject } from 'type-fest'
import { JsonArray, JsonObject } from 'type-fest'
import { beforeAll, afterEach, afterAll, beforeEach } from 'vitest'

type CapturedRequest = Pick<Request, 'headers' | 'method' | 'body'>
Expand All @@ -21,7 +21,7 @@ type Context<S extends MockSpec | MockSpecBatch = MockSpec> = {
type MockSpecBody = {
data?: JsonObject
extensions?: JsonObject
errors?: JsonObject
errors?: JsonObject | JsonArray
}

type MockSpec = {
Expand Down
13 changes: 13 additions & 0 deletions tests/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,16 @@ describe('operationName parsing', () => {
expect(requestBody.operationName).toEqual('myStringOperation')
})
})

test('should not throw error when errors property is an empty array (occured when using UltraGraphQL)', async () => {
ctx.res({
body: {
data: { test: 'test' },
errors: [],
},
})

const res = await new GraphQLClient(ctx.url).request(`{ test }`)

expect(res).toEqual(expect.objectContaining({ test: 'test' }))
})

0 comments on commit 4302ba1

Please sign in to comment.