Skip to content

Commit 04ab9f6

Browse files
committed
fix(useQuery): improve error handling with errorPolicy set to 'all'
1 parent eaf1da7 commit 04ab9f6

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

packages/vue-apollo-composable/src/useQuery.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { paramToRef } from './util/paramToRef'
2929
import { paramToReactive } from './util/paramToReactive'
3030
import { useEventHook } from './util/useEventHook'
3131
import { trackQuery } from './util/loadingTracking'
32-
import { toApolloError } from './util/toApolloError'
32+
import { resultErrorsToApolloError, toApolloError } from './util/toApolloError'
3333
import { isServer } from './util/env'
3434

3535
import type { CurrentInstance } from './util/types'
@@ -261,15 +261,15 @@ export function useQueryImpl<
261261

262262
processNextResult(queryResult)
263263

264-
// ApolloQueryResult.error may be set at the same time as we get a result
265-
// when `errorPolicy` is `all`
266-
if (queryResult.error !== undefined) {
267-
processError(queryResult.error)
268-
} else {
269-
if (firstResolve) {
270-
firstResolve()
271-
stop()
272-
}
264+
// When `errorPolicy` is `all`, `onError` will not get called and
265+
// ApolloQueryResult.errors may be set at the same time as we get a result
266+
if (!queryResult.error && queryResult.errors?.length) {
267+
processError(resultErrorsToApolloError(queryResult.errors))
268+
}
269+
270+
if (firstResolve) {
271+
firstResolve()
272+
stop()
273273
}
274274
}
275275

packages/vue-apollo-composable/src/util/toApolloError.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ApolloError, isApolloError } from '@apollo/client/core'
2+
import { GraphQLErrors } from '@apollo/client/errors'
23

34
export function toApolloError (error: unknown): ApolloError {
45
if (!(error instanceof Error)) {
@@ -14,3 +15,10 @@ export function toApolloError (error: unknown): ApolloError {
1415

1516
return new ApolloError({ networkError: error, errorMessage: error.message })
1617
}
18+
19+
export function resultErrorsToApolloError (errors: GraphQLErrors): ApolloError {
20+
return new ApolloError({
21+
graphQLErrors: errors,
22+
errorMessage: `GraphQL response contains errors: ${errors.map((e: any) => e.message).join(' | ')}`,
23+
})
24+
}

0 commit comments

Comments
 (0)