Skip to content

Commit 06d9cb4

Browse files
Change type of error extensions from anonymous Record to named interfaces (#3315)
1 parent 04c6fce commit 06d9cb4

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

integrationTests/ts/extensions-test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { GraphQLError } from 'graphql/error';
12
import { GraphQLString, GraphQLObjectType } from 'graphql/type';
23

34
interface SomeExtension {
@@ -50,3 +51,12 @@ const sayHiField = queryType.getFields().sayHi;
5051
checkExtensionTypes(sayHiField.extensions.someFieldExtension);
5152

5253
checkExtensionTypes(sayHiField.args[0].extensions.someArgumentExtension);
54+
55+
declare module 'graphql' {
56+
export interface GraphQLErrorExtensions {
57+
someErrorExtension?: SomeExtension;
58+
}
59+
}
60+
61+
const error = new GraphQLError('foo');
62+
checkExtensionTypes(error.extensions.someErrorExtension);

src/error/GraphQLError.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ import type { SourceLocation } from '../language/location';
77
import { getLocation } from '../language/location';
88
import { printLocation, printSourceLocation } from '../language/printLocation';
99

10+
/**
11+
* Custom extensions
12+
*
13+
* @remarks
14+
* Use a unique identifier name for your extension, for example the name of
15+
* your library or project. Do not use a shortened identifier as this increases
16+
* the risk of conflicts. We recommend you add at most one extension field,
17+
* an object which can contain all the values you need.
18+
*/
19+
export interface GraphQLErrorExtensions {
20+
[attributeName: string]: unknown;
21+
}
22+
1023
/**
1124
* A GraphQLError describes an Error found during the parse, validate, or
1225
* execute phases of performing a GraphQL operation. In addition to a message
@@ -61,7 +74,7 @@ export class GraphQLError extends Error {
6174
/**
6275
* Extension fields to add to the formatted error.
6376
*/
64-
readonly extensions: { [key: string]: unknown };
77+
readonly extensions: GraphQLErrorExtensions;
6578

6679
constructor(
6780
message: string,
@@ -70,7 +83,7 @@ export class GraphQLError extends Error {
7083
positions?: Maybe<ReadonlyArray<number>>,
7184
path?: Maybe<ReadonlyArray<string | number>>,
7285
originalError?: Maybe<Error & { readonly extensions?: unknown }>,
73-
extensions?: Maybe<{ [key: string]: unknown }>,
86+
extensions?: Maybe<GraphQLErrorExtensions>,
7487
) {
7588
super(message);
7689

src/error/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
export { GraphQLError, printError, formatError } from './GraphQLError';
2-
export type { GraphQLFormattedError } from './GraphQLError';
2+
export type {
3+
GraphQLFormattedError,
4+
GraphQLErrorExtensions,
5+
} from './GraphQLError';
36

47
export { syntaxError } from './syntaxError';
58

src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,10 @@ export {
382382
formatError,
383383
} from './error/index';
384384

385-
export type { GraphQLFormattedError } from './error/index';
385+
export type {
386+
GraphQLFormattedError,
387+
GraphQLErrorExtensions,
388+
} from './error/index';
386389

387390
/** Utilities for operating on GraphQL type schema and parsed sources. */
388391
export {

0 commit comments

Comments
 (0)