Skip to content

Commit 3f49961

Browse files
authored
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: apollographql/apollo-client#11789
1 parent 423e72d commit 3f49961

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
@@ -20,6 +20,19 @@ export interface GraphQLErrorExtensions {
2020
[attributeName: string]: unknown;
2121
}
2222

23+
/**
24+
* Custom formatted extensions
25+
*
26+
* @remarks
27+
* Use a unique identifier name for your extension, for example the name of
28+
* your library or project. Do not use a shortened identifier as this increases
29+
* the risk of conflicts. We recommend you add at most one extension field,
30+
* an object which can contain all the values you need.
31+
*/
32+
export interface GraphQLFormattedErrorExtensions {
33+
[attributeName: string]: unknown;
34+
}
35+
2336
export interface GraphQLErrorOptions {
2437
nodes?: ReadonlyArray<ASTNode> | ASTNode | null;
2538
source?: Maybe<Source>;
@@ -275,7 +288,7 @@ export interface GraphQLFormattedError {
275288
* Reserved for implementors to extend the protocol however they see fit,
276289
* and hence there are no additional restrictions on its contents.
277290
*/
278-
readonly extensions?: { [key: string]: unknown };
291+
readonly extensions?: GraphQLFormattedErrorExtensions;
279292
}
280293

281294
/**

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';
78

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

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ export type {
398398
GraphQLErrorOptions,
399399
GraphQLFormattedError,
400400
GraphQLErrorExtensions,
401+
GraphQLFormattedErrorExtensions,
401402
} from './error/index';
402403

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

0 commit comments

Comments
 (0)