Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
GraphQL Client: Update GraphQL error types (#22721)
Browse files Browse the repository at this point in the history
  • Loading branch information
umpox authored Jul 21, 2021
1 parent a692cbd commit aaec123
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 56 deletions.
2 changes: 1 addition & 1 deletion client/browser/src/shared/repo/backend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export const fetchBlobContentLines = memoizeObservable(
if (errors) {
if (errors.length === 1) {
const error = errors[0]
const errorPath = error.path.join('.')
const errorPath = error.path?.join('.')

// Originally this checked only for 'repository.commit.file.content'.
// But if a file doesn't exist, the error path is 'repository.commit.file'
Expand Down
3 changes: 3 additions & 0 deletions client/extension-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@
},
"devDependencies": {
"typedoc": "^0.17.8"
},
"peerDependencies": {
"graphql": "^15.4.0"
}
}
42 changes: 1 addition & 41 deletions client/extension-api/src/sourcegraph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1819,47 +1819,7 @@ declare module 'sourcegraph' {
}
export interface ErrorGraphQLResult {
data: undefined
errors: GraphQLError[]
}

/**
* A spec-compliant member of the GraphQL `errors` array.
*/
export interface GraphQLError {
/**
* Every error must contain an entry with the key message with a string description of the error intended for
* the developer as a guide to understand and correct the error.
*/
message: string

/**
* If an error can be associated to a particular point in the requested GraphQL document, it should contain an
* entry with the key locations with a list of locations, where each location is a map with the keys line and
* column, both positive numbers starting from 1 which describe the beginning of an associated syntax element.
*/
locations?: {
line: number
column: number
}[]

/**
* If an error can be associated to a particular field in the GraphQL result, it must contain an entry with the
* key path that details the path of the response field which experienced the error. This allows clients to
* identify whether a null result is intentional or caused by a runtime error.
*
* This field should be a list of path segments starting at the root of the response and ending with the field
* associated with the error. Path segments that represent fields should be strings, and path segments that
* represent list indices should be 0‐indexed integers. If the error happens in an aliased field, the path to
* the error should use the aliased name, since it represents a path in the response, not in the query.
*/
path?: (string | number)[]

/**
* GraphQL services may provide an additional entry to errors with key extensions. This entry, if set, must
* have a map as its value. This entry is reserved for implementors to add additional information to errors
* however they see fit, and there are no additional restrictions on its contents.
*/
extensions?: Record<string, unknown>
errors: readonly import('graphql').GraphQLError[]
}
}

Expand Down
16 changes: 5 additions & 11 deletions client/shared/src/graphql/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
MutationHookOptions,
MutationTuple,
} from '@apollo/client'
import { GraphQLError } from 'graphql'
import { useMemo } from 'react'
import { Observable } from 'rxjs'
import { fromFetch } from 'rxjs/fetch'
Expand All @@ -21,7 +22,6 @@ import { checkOk } from '../backend/fetch'
import { createAggregateError } from '../util/errors'

import { cache } from './cache'
import * as GQL from './schema'

/**
* Use this template string tag for all GraphQL queries.
Expand All @@ -35,7 +35,7 @@ export interface SuccessGraphQLResult<T> {
}
export interface ErrorGraphQLResult {
data: undefined
errors: GQL.IGraphQLResponseError[]
errors: readonly GraphQLError[]
}

export type GraphQLResult<T> = SuccessGraphQLResult<T> | ErrorGraphQLResult
Expand All @@ -54,17 +54,11 @@ export function dataOrThrowErrors<T>(result: GraphQLResult<T>): T {
return result.data
}

export interface GraphQLError extends Error {
queryName: string
}
export const createInvalidGraphQLQueryResponseError = (queryName: string): GraphQLError =>
Object.assign(new Error(`Invalid GraphQL response: query ${queryName}`), {
queryName,
})
new GraphQLError(`Invalid GraphQL response: query ${queryName}`)

export const createInvalidGraphQLMutationResponseError = (queryName: string): GraphQLError =>
Object.assign(new Error(`Invalid GraphQL response: mutation ${queryName}`), {
queryName,
})
new GraphQLError(`Invalid GraphQL response: mutation ${queryName}`)

export interface GraphQLRequestOptions extends Omit<RequestInit, 'method' | 'body'> {
baseUrl?: string
Expand Down
4 changes: 2 additions & 2 deletions client/shared/src/testing/integration/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as util from 'util'

import { Polly, PollyServer } from '@pollyjs/core'
import FSPersister from '@pollyjs/persister-fs'
import { GraphQLError } from 'graphql'
import { snakeCase } from 'lodash'
import * as mime from 'mime-types'
import { Test } from 'mocha'
Expand All @@ -13,7 +14,6 @@ import { Subject, Subscription, throwError } from 'rxjs'
import { first, timeoutWith } from 'rxjs/operators'

import { ErrorGraphQLResult, SuccessGraphQLResult } from '../../graphql/graphql'
import { IGraphQLResponseError } from '../../graphql/schema'
import { asError } from '../../util/errors'
import { keyExistsIn } from '../../util/types'
import { recordCoverage } from '../coverage'
Expand All @@ -34,7 +34,7 @@ const ASSETS_DIRECTORY = path.resolve(__dirname, '../../../../../ui/assets')
const record = readEnvironmentBoolean({ variable: 'RECORD', defaultValue: false })

export class IntegrationTestGraphQlError extends Error {
constructor(public errors: IGraphQLResponseError[]) {
constructor(public errors: GraphQLError[]) {
super('graphql error for integration tests')
}
}
Expand Down
2 changes: 1 addition & 1 deletion client/shared/src/util/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const isAggregateError = (value: unknown): value is AggregateError =>
*
* @param errors The errors or ErrorLikes to aggregate
*/
export const createAggregateError = (errors: ErrorLike[] = []): Error =>
export const createAggregateError = (errors: readonly ErrorLike[] = []): Error =>
errors.length === 1
? asError(errors[0])
: Object.assign(new Error(errors.map(error => error.message).join('\n')), {
Expand Down

0 comments on commit aaec123

Please sign in to comment.