Skip to content

Commit 50197a3

Browse files
tpoisseauyaacovCR
authored andcommitted
fix: properly type extensions in GraphQLFormattedError (graphql#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: apollographql/apollo-client#11789
1 parent 2e9d6a1 commit 50197a3

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/error/GraphQLError.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ export interface GraphQLErrorExtensions {
2323
[attributeName: string]: unknown;
2424
}
2525

26+
/**
27+
* Custom formatted extensions
28+
*
29+
* @remarks
30+
* Use a unique identifier name for your extension, for example the name of
31+
* your library or project. Do not use a shortened identifier as this increases
32+
* the risk of conflicts. We recommend you add at most one extension field,
33+
* an object which can contain all the values you need.
34+
*/
35+
export interface GraphQLFormattedErrorExtensions {
36+
[attributeName: string]: unknown;
37+
}
38+
2639
export interface GraphQLErrorOptions {
2740
nodes?: ReadonlyArray<ASTNode> | ASTNode | null | undefined;
2841
source?: Maybe<Source>;
@@ -238,5 +251,5 @@ export interface GraphQLFormattedError {
238251
* Reserved for implementors to extend the protocol however they see fit,
239252
* and hence there are no additional restrictions on its contents.
240253
*/
241-
readonly extensions?: { [key: string]: unknown };
254+
readonly extensions?: GraphQLFormattedErrorExtensions;
242255
}

src/error/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export type {
33
GraphQLErrorOptions,
44
GraphQLFormattedError,
55
GraphQLErrorExtensions,
6+
GraphQLFormattedErrorExtensions,
67
} from './GraphQLError.js';
78

89
export { syntaxError } from './syntaxError.js';

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ export type {
424424
GraphQLErrorOptions,
425425
GraphQLFormattedError,
426426
GraphQLErrorExtensions,
427+
GraphQLFormattedErrorExtensions,
427428
} from './error/index.js';
428429

429430
// Utilities for operating on GraphQL type schema and parsed sources.

0 commit comments

Comments
 (0)