From ba7d20a5edf0dd669ad3a475a69182abe95c4eba Mon Sep 17 00:00:00 2001 From: tpoisseau <22891227+tpoisseau@users.noreply.github.com> Date: Wed, 6 Nov 2024 10:13:55 +0100 Subject: [PATCH 1/4] fix: properly type `extensions` in GraphQLFormattedError Following typescript documentation, it's not possible override interface property, we can only add new props. Since it's declared as unknown dict, even if we merge `GraphQLErrorExtensions`, we can't access to our extensions without workaround and verbose typescript in source-code. It's annoying since apollo expose `GraphQLFormattedError` instead `GraphQLError` Refs: https://www.typescriptlang.org/docs/handbook/declaration-merging.html Refs: https://github.com/apollographql/apollo-client/pull/11789 --- src/error/GraphQLError.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/error/GraphQLError.ts b/src/error/GraphQLError.ts index b88d27a56c..dd3d1ac42f 100644 --- a/src/error/GraphQLError.ts +++ b/src/error/GraphQLError.ts @@ -275,7 +275,7 @@ export interface GraphQLFormattedError { * Reserved for implementors to extend the protocol however they see fit, * and hence there are no additional restrictions on its contents. */ - readonly extensions?: { [key: string]: unknown }; + readonly extensions?: GraphQLErrorExtensions; } /** From 7df19d2050124716aa706be23e05413b570d0b38 Mon Sep 17 00:00:00 2001 From: tpoisseau <22891227+tpoisseau@users.noreply.github.com> Date: Thu, 7 Nov 2024 09:03:48 +0100 Subject: [PATCH 2/4] feat: split GraphQLErrorExtensions and GraphQLFormattedErrorExtensions Refs: https://github.com/graphql/graphql-js/pull/4286#discussion_r1831441208 --- src/error/GraphQLError.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/error/GraphQLError.ts b/src/error/GraphQLError.ts index dd3d1ac42f..77a5e78779 100644 --- a/src/error/GraphQLError.ts +++ b/src/error/GraphQLError.ts @@ -20,6 +20,19 @@ export interface GraphQLErrorExtensions { [attributeName: string]: unknown; } +/** + * Custom formatted 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 GraphQLFormattedErrorExtensions { + [attributeName: string]: unknown; +} + export interface GraphQLErrorOptions { nodes?: ReadonlyArray | ASTNode | null; source?: Maybe; @@ -275,7 +288,7 @@ export interface GraphQLFormattedError { * Reserved for implementors to extend the protocol however they see fit, * and hence there are no additional restrictions on its contents. */ - readonly extensions?: GraphQLErrorExtensions; + readonly extensions?: GraphQLFormattedErrorExtensions; } /** From 9018817196bf1884d440f3a584b4c7f650696acb Mon Sep 17 00:00:00 2001 From: tpoisseau <22891227+tpoisseau@users.noreply.github.com> Date: Thu, 7 Nov 2024 09:04:46 +0100 Subject: [PATCH 3/4] chore: export GraphQLFormattedErrorExtensions --- src/error/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/error/index.ts b/src/error/index.ts index dd92e48364..7e5d267f50 100644 --- a/src/error/index.ts +++ b/src/error/index.ts @@ -3,6 +3,7 @@ export type { GraphQLErrorOptions, GraphQLFormattedError, GraphQLErrorExtensions, + GraphQLFormattedErrorExtensions, } from './GraphQLError'; export { syntaxError } from './syntaxError'; From 1109e0784a0f01a149ae4784359e4dbf398a9aa2 Mon Sep 17 00:00:00 2001 From: tpoisseau <22891227+tpoisseau@users.noreply.github.com> Date: Thu, 7 Nov 2024 09:08:13 +0100 Subject: [PATCH 4/4] chore: export GraphQLFormattedErrorExtensions --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index 877939d879..73c713a203 100644 --- a/src/index.ts +++ b/src/index.ts @@ -398,6 +398,7 @@ export type { GraphQLErrorOptions, GraphQLFormattedError, GraphQLErrorExtensions, + GraphQLFormattedErrorExtensions, } from './error/index'; // Utilities for operating on GraphQL type schema and parsed sources.