Skip to content

Commit

Permalink
Change type of error extensions from anonymous Record to named interf…
Browse files Browse the repository at this point in the history
…aces (#3315)
  • Loading branch information
IvanGoncharov committed Oct 13, 2021
1 parent 04c6fce commit 06d9cb4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
10 changes: 10 additions & 0 deletions integrationTests/ts/extensions-test.ts
@@ -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
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
@@ -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
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

0 comments on commit 06d9cb4

Please sign in to comment.