Skip to content

Commit

Permalink
fix: properly type extensions in GraphQLFormattedError (graphql#4286)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tpoisseau authored and yaacovCR committed Dec 3, 2024
1 parent 8c995a6 commit 0a88274
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/error/GraphQLError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> | ASTNode | null | undefined;
source?: Maybe<Source>;
Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions src/error/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export type {
GraphQLErrorOptions,
GraphQLFormattedError,
GraphQLErrorExtensions,
GraphQLFormattedErrorExtensions,
} from './GraphQLError.js';

export { syntaxError } from './syntaxError.js';
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ export type {
GraphQLErrorOptions,
GraphQLFormattedError,
GraphQLErrorExtensions,
GraphQLFormattedErrorExtensions,
} from './error/index.js';

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

0 comments on commit 0a88274

Please sign in to comment.