Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions integrationTests/ts/extensions-test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GraphQLError } from 'graphql/error';
import { GraphQLString, GraphQLObjectType } from 'graphql/type';

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

checkExtensionTypes(sayHiField.args[0].extensions.someArgumentExtension);

declare module 'graphql' {
export interface GraphQLErrorExtensions {
someErrorExtension?: SomeExtension;
}
}

const error = new GraphQLError('foo');
checkExtensionTypes(error.extensions.someErrorExtension);
17 changes: 15 additions & 2 deletions src/error/GraphQLError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ import type { SourceLocation } from '../language/location';
import { getLocation } from '../language/location';
import { printLocation, printSourceLocation } from '../language/printLocation';

/**
* Custom extensions
*
* @remarks
* Use a unique identifier name for your extension, for example the name of
* your library or project. Do not use a shortened identifier as this increases
* the risk of conflicts. We recommend you add at most one extension field,
* an object which can contain all the values you need.
*/
export interface GraphQLErrorExtensions {
[attributeName: string]: unknown;
}

/**
* 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 @@ -61,7 +74,7 @@ export class GraphQLError extends Error {
/**
* Extension fields to add to the formatted error.
*/
readonly extensions: { [key: string]: unknown };
readonly extensions: GraphQLErrorExtensions;

constructor(
message: string,
Expand All @@ -70,7 +83,7 @@ export class GraphQLError extends Error {
positions?: Maybe<ReadonlyArray<number>>,
path?: Maybe<ReadonlyArray<string | number>>,
originalError?: Maybe<Error & { readonly extensions?: unknown }>,
extensions?: Maybe<{ [key: string]: unknown }>,
extensions?: Maybe<GraphQLErrorExtensions>,
) {
super(message);

Expand Down
5 changes: 4 additions & 1 deletion src/error/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export { GraphQLError, printError, formatError } from './GraphQLError';
export type { GraphQLFormattedError } from './GraphQLError';
export type {
GraphQLFormattedError,
GraphQLErrorExtensions,
} from './GraphQLError';

export { syntaxError } from './syntaxError';

Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,10 @@ export {
formatError,
} from './error/index';

export type { GraphQLFormattedError } from './error/index';
export type {
GraphQLFormattedError,
GraphQLErrorExtensions,
} from './error/index';

/** Utilities for operating on GraphQL type schema and parsed sources. */
export {
Expand Down