Skip to content

Commit

Permalink
Backport GraphQLError toJSON method to v15 (#4324)
Browse files Browse the repository at this point in the history
Eases up the transition to future v17 that has completely removed the
`formatError` utility.
  • Loading branch information
enisdenjo authored Jan 13, 2025
1 parent 511e0f1 commit 2065e70
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 40 deletions.
41 changes: 41 additions & 0 deletions src/error/GraphQLError.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,51 @@ export class GraphQLError extends Error {
* Extension fields to add to the formatted error.
*/
readonly extensions: { [key: string]: any };

toString(): string;

toJSON(): GraphQLFormattedError;
}

/**
* Prints a GraphQLError to a string, representing useful location information
* about the error's position in the source.
*/
export function printError(error: GraphQLError): string;

/**
* Given a GraphQLError, format it according to the rules described by the
* Response Format, Errors section of the GraphQL Specification.
*/
export function formatError(error: GraphQLError): GraphQLFormattedError;

/**
* @see https://github.com/graphql/graphql-spec/blob/master/spec/Section%207%20--%20Response.md#errors
*/
export interface GraphQLFormattedError<
TExtensions extends Record<string, any> = Record<string, any>
> {
/**
* A short, human-readable summary of the problem that **SHOULD NOT** change
* from occurrence to occurrence of the problem, except for purposes of
* localization.
*/
readonly message: string;
/**
* If an error can be associated to a particular point in the requested
* GraphQL document, it should contain a list of locations.
*/
readonly locations?: ReadonlyArray<SourceLocation>;
/**
* 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.
*/
readonly path?: ReadonlyArray<string | number>;
/**
* Reserved for implementors to extend the protocol however they see fit,
* and hence there are no additional restrictions on its contents.
*/
readonly extensions?: TExtensions;
}
7 changes: 7 additions & 0 deletions src/error/GraphQLError.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import type { SourceLocation } from '../language/location';
import { getLocation } from '../language/location';
import { printLocation, printSourceLocation } from '../language/printLocation';

import { formatError } from './formatError';
import type { GraphQLFormattedError } from './formatError';

/**
* A GraphQLError describes an Error found during the parse, validate, or
* execute phases of performing a GraphQL operation. In addition to a message
Expand Down Expand Up @@ -157,6 +160,10 @@ export class GraphQLError extends Error {
return printError(this);
}

toJSON(): GraphQLFormattedError {
return formatError(this);
}

// FIXME: workaround to not break chai comparisons, should be remove in v16
// $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
get [SYMBOL_TO_STRING_TAG](): string {
Expand Down
49 changes: 49 additions & 0 deletions src/error/__tests__/GraphQLError-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,52 @@ describe('printError', () => {
`);
});
});

describe('toJSON', () => {
it('includes path', () => {
const error = new GraphQLError('msg', null, null, null, [
'path',
3,
'to',
'field',
]);

expect(error.toJSON()).to.deep.equal({
message: 'msg',
locations: undefined,
path: ['path', 3, 'to', 'field'],
});
});

it('includes extension fields', () => {
const error = new GraphQLError('msg', null, null, null, null, null, {
foo: 'bar',
});

expect(error.toJSON()).to.deep.equal({
message: 'msg',
locations: undefined,
path: undefined,
extensions: { foo: 'bar' },
});
});

it('can be created with full argument list', () => {
const error = new GraphQLError(
'msg',
[operationNode],
source,
[6],
['path', 2, 'a'],
new Error('I like turtles'),
{ hee: 'I like turtles' },
);

expect(error.toJSON()).to.deep.equal({
message: 'msg',
locations: [{ column: 5, line: 2 }],
path: ['path', 2, 'a'],
extensions: { hee: 'I like turtles' },
});
});
});
41 changes: 1 addition & 40 deletions src/error/formatError.d.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1 @@
import { SourceLocation } from '../language/location';

import { GraphQLError } from './GraphQLError';

/**
* Given a GraphQLError, format it according to the rules described by the
* Response Format, Errors section of the GraphQL Specification.
*/
export function formatError(error: GraphQLError): GraphQLFormattedError;

/**
* @see https://github.com/graphql/graphql-spec/blob/master/spec/Section%207%20--%20Response.md#errors
*/
export interface GraphQLFormattedError<
TExtensions extends Record<string, any> = Record<string, any>
> {
/**
* A short, human-readable summary of the problem that **SHOULD NOT** change
* from occurrence to occurrence of the problem, except for purposes of
* localization.
*/
readonly message: string;
/**
* If an error can be associated to a particular point in the requested
* GraphQL document, it should contain a list of locations.
*/
readonly locations?: ReadonlyArray<SourceLocation>;
/**
* 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.
*/
readonly path?: ReadonlyArray<string | number>;
/**
* Reserved for implementors to extend the protocol however they see fit,
* and hence there are no additional restrictions on its contents.
*/
readonly extensions?: TExtensions;
}
export { formatError, GraphQLFormattedError } from './GraphQLError';

0 comments on commit 2065e70

Please sign in to comment.