From 33e64db9e8730f0c716fcdd41c93014d48f1796a Mon Sep 17 00:00:00 2001 From: tpoisseau <22891227+tpoisseau@users.noreply.github.com> Date: Fri, 8 Nov 2024 11:26:16 +0100 Subject: [PATCH] fix: properly type `extensions` in GraphQLFormattedError (#4286) 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 | 15 ++++++++++++++- src/error/index.ts | 1 + src/index.ts | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/error/GraphQLError.ts b/src/error/GraphQLError.ts index a3ce229968..a2404d6040 100644 --- a/src/error/GraphQLError.ts +++ b/src/error/GraphQLError.ts @@ -23,6 +23,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 | undefined; source?: Maybe; @@ -238,5 +251,5 @@ 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?: GraphQLFormattedErrorExtensions; } diff --git a/src/error/index.ts b/src/error/index.ts index ed9e24d241..17e9059dd1 100644 --- a/src/error/index.ts +++ b/src/error/index.ts @@ -3,6 +3,7 @@ export type { GraphQLErrorOptions, GraphQLFormattedError, GraphQLErrorExtensions, + GraphQLFormattedErrorExtensions, } from './GraphQLError.js'; export { syntaxError } from './syntaxError.js'; diff --git a/src/index.ts b/src/index.ts index c3deb51ffe..fe3b2b1a80 100644 --- a/src/index.ts +++ b/src/index.ts @@ -423,6 +423,7 @@ export type { GraphQLErrorOptions, GraphQLFormattedError, GraphQLErrorExtensions, + GraphQLFormattedErrorExtensions, } from './error/index.js'; // Utilities for operating on GraphQL type schema and parsed sources.