Skip to content

Commit

Permalink
Improve return type of customFormatErrorFn
Browse files Browse the repository at this point in the history
  • Loading branch information
chentsulin committed Nov 24, 2021
1 parent 1e03a14 commit eaf1bb4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/__tests__/http-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2369,7 +2369,9 @@ describe('GraphQL-HTTP tests', () => {
urlString(),
graphqlHTTP({
schema: TestSchema,
customFormatErrorFn: () => null,
customFormatErrorFn: () => ({
message: 'Some generic error message.',
}),
extensions({ result }) {
return { preservedResult: { ...result } };
},
Expand All @@ -2386,7 +2388,7 @@ describe('GraphQL-HTTP tests', () => {
expect(response.status).to.equal(200);
expect(JSON.parse(response.text)).to.deep.equal({
data: { thrower: null },
errors: [null],
errors: [{ message: 'Some generic error message.' }],
extensions: {
preservedResult: {
data: { thrower: null },
Expand Down
29 changes: 19 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import type {
ExecutionArgs,
ExecutionResult,
FormattedExecutionResult,
GraphQLError,
GraphQLSchema,
GraphQLFieldResolver,
GraphQLTypeResolver,
GraphQLFormattedError,
ValidationContext,
ASTVisitor,
} from 'graphql';
Expand Down Expand Up @@ -91,7 +93,10 @@ export type OptionsData = {|
* fulfilling a GraphQL operation. If no function is provided, GraphQL's
* default spec-compliant `formatError` function will be used.
*/
customFormatErrorFn?: ?(error: GraphQLError, context?: ?any) => mixed,
customFormatErrorFn?: ?(
error: GraphQLError,
context?: ?any,
) => GraphQLFormattedError,

/**
* An optional function which will be used to create a document instead of
Expand All @@ -103,7 +108,7 @@ export type OptionsData = {|
* `formatError` is deprecated and replaced by `customFormatErrorFn`. It will
* be removed in version 1.0.0.
*/
formatError?: ?(error: GraphQLError, context?: ?any) => mixed,
formatError?: ?(error: GraphQLError, context?: ?any) => GraphQLFormattedError,

/**
* An optional function for adding additional metadata to the GraphQL response
Expand Down Expand Up @@ -383,27 +388,31 @@ function graphqlHTTP(options: Options): Middleware {
if (response.status === 200 && result && !result.data) {
response.status = 500;
}
// Format any encountered errors.
if (result && result.errors) {
(result: any).errors = result.errors.map((err) =>
formatErrorFn(err, context),
);
}

const formattedResult: FormattedExecutionResult =
result && result.errors
? {
...result,
errors: result.errors.map((err) => formatErrorFn(err, context)),
}
: result;

// If allowed to show GraphiQL, present it instead of JSON.
if (showGraphiQL) {
const payload = renderGraphiQL({
query,
variables,
operationName,
result,
result: formattedResult,
options: typeof showGraphiQL !== 'boolean' ? showGraphiQL : {},
});
response.type = 'text/html';
response.body = payload;
} else {
// Otherwise, present JSON directly.
const payload = pretty ? JSON.stringify(result, null, 2) : result;
const payload = pretty
? JSON.stringify(formattedResult, null, 2)
: formattedResult;
response.type = 'application/json';
response.body = payload;
}
Expand Down
4 changes: 2 additions & 2 deletions src/renderGraphiQL.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow strict

import type { ExecutionResult } from 'graphql';
import type { FormattedExecutionResult } from 'graphql';

type EditorThemeParam =
| {|
Expand All @@ -13,7 +13,7 @@ type GraphiQLData = {|
query: ?string,
variables: ?{ [param: string]: mixed },
operationName: ?string,
result?: ?ExecutionResult,
result?: ?FormattedExecutionResult,
options: ?GraphiQLOptions,
|};

Expand Down

0 comments on commit eaf1bb4

Please sign in to comment.