From 242bdb20249a385542bc832e656e90627d6f0e30 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Wed, 13 Oct 2021 16:06:49 +0300 Subject: [PATCH] Change type of extensions from anonymous Record to named interfaces --- integrationTests/ts/extensions-test.ts | 10 ++++++++++ src/error/GraphQLError.ts | 17 +++++++++++++++-- src/error/index.ts | 5 ++++- src/index.ts | 5 ++++- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/integrationTests/ts/extensions-test.ts b/integrationTests/ts/extensions-test.ts index 1a291e1432..689d943598 100644 --- a/integrationTests/ts/extensions-test.ts +++ b/integrationTests/ts/extensions-test.ts @@ -1,3 +1,4 @@ +import { GraphQLError } from 'graphql/error'; import { GraphQLString, GraphQLObjectType } from 'graphql/type'; interface SomeExtension { @@ -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); diff --git a/src/error/GraphQLError.ts b/src/error/GraphQLError.ts index 59b74e1648..70e4b6c826 100644 --- a/src/error/GraphQLError.ts +++ b/src/error/GraphQLError.ts @@ -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 @@ -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, @@ -70,7 +83,7 @@ export class GraphQLError extends Error { positions?: Maybe>, path?: Maybe>, originalError?: Maybe, - extensions?: Maybe<{ [key: string]: unknown }>, + extensions?: Maybe, ) { super(message); diff --git a/src/error/index.ts b/src/error/index.ts index b947c93fc7..41ad0d6f09 100644 --- a/src/error/index.ts +++ b/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'; diff --git a/src/index.ts b/src/index.ts index 32d4aa0204..c6e5107d74 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 {