mirror of
https://github.com/vale981/apollo-server
synced 2025-03-17 00:06:42 -04:00
30 lines
815 B
TypeScript
30 lines
815 B
TypeScript
![]() |
import { GraphQLExtension, GraphQLResponse } from 'graphql-extensions';
|
||
|
import { formatApolloErrors } from 'apollo-server-errors';
|
||
|
|
||
|
export class FormatErrorExtension extends GraphQLExtension {
|
||
|
private formatError: Function;
|
||
|
private debug: boolean;
|
||
|
|
||
|
public constructor(formatError: Function, debug: boolean = false) {
|
||
|
super();
|
||
|
this.formatError = formatError;
|
||
|
this.debug = debug;
|
||
|
}
|
||
|
|
||
|
public willSendResponse(o: {
|
||
|
graphqlResponse: GraphQLResponse;
|
||
|
}): void | { graphqlResponse: GraphQLResponse } {
|
||
|
if (o.graphqlResponse.errors) {
|
||
|
return {
|
||
|
graphqlResponse: {
|
||
|
...o.graphqlResponse,
|
||
|
errors: formatApolloErrors(o.graphqlResponse.errors, {
|
||
|
formatter: this.formatError,
|
||
|
debug: this.debug,
|
||
|
}),
|
||
|
},
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
}
|