Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit c5aa7a1

Browse files
Improve return type of customFormatErrorFn (#673)
1 parent a948a08 commit c5aa7a1

File tree

8 files changed

+34
-23
lines changed

8 files changed

+34
-23
lines changed

integrationTests/ts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"dependencies": {
66
"@types/node": "14.0.13",
77
"express-graphql": "file:../express-graphql.tgz",
8-
"graphql": "14.6.0",
8+
"graphql": "14.7.0",
99
"typescript-3.4": "npm:[email protected]",
1010
"typescript-3.5": "npm:[email protected]",
1111
"typescript-3.6": "npm:[email protected]",

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"express": "4.17.1",
8080
"flow-bin": "0.128.0",
8181
"graphiql": "1.0.3",
82-
"graphql": "15.2.0",
82+
"graphql": "15.3.0",
8383
"mocha": "8.0.1",
8484
"multer": "1.4.2",
8585
"nyc": "15.1.0",
@@ -95,6 +95,6 @@
9595
"unfetch": "4.1.0"
9696
},
9797
"peerDependencies": {
98-
"graphql": "^14.6.0 || ^15.0.0"
98+
"graphql": "^14.7.0 || ^15.3.0"
9999
}
100100
}

src/__tests__/http-test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,7 +2188,9 @@ function runTests(server: Server) {
21882188
urlString(),
21892189
graphqlHTTP({
21902190
schema: TestSchema,
2191-
customFormatErrorFn: () => null,
2191+
customFormatErrorFn: () => ({
2192+
message: 'Some generic error message.',
2193+
}),
21922194
extensions({ result }) {
21932195
return { preservedResult: { ...result } };
21942196
},
@@ -2204,7 +2206,7 @@ function runTests(server: Server) {
22042206
expect(response.status).to.equal(200);
22052207
expect(JSON.parse(response.text)).to.deep.equal({
22062208
data: { thrower: null },
2207-
errors: [null],
2209+
errors: [{ message: 'Some generic error message.' }],
22082210
extensions: {
22092211
preservedResult: {
22102212
data: { thrower: null },

src/index.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ExecutionArgs,
1212
ExecutionResult,
1313
GraphQLError,
14+
GraphQLFormattedError,
1415
GraphQLSchema,
1516
GraphQLFieldResolver,
1617
GraphQLTypeResolver,
@@ -90,7 +91,7 @@ export interface OptionsData {
9091
* fulfilling a GraphQL operation. If no function is provided, GraphQL's
9192
* default spec-compliant `formatError` function will be used.
9293
*/
93-
customFormatErrorFn?: (error: GraphQLError) => unknown;
94+
customFormatErrorFn?: (error: GraphQLError) => GraphQLFormattedError;
9495

9596
/**
9697
* An optional function which will be used to create a document instead of
@@ -102,7 +103,7 @@ export interface OptionsData {
102103
* `formatError` is deprecated and replaced by `customFormatErrorFn`. It will
103104
* be removed in version 1.0.0.
104105
*/
105-
formatError?: (error: GraphQLError) => unknown;
106+
formatError?: (error: GraphQLError) => GraphQLFormattedError;
106107

107108
/**
108109
* An optional function for adding additional metadata to the GraphQL response

src/index.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import type {
99
ValidationContext,
1010
ExecutionArgs,
1111
ExecutionResult,
12+
FormattedExecutionResult,
1213
GraphQLError,
1314
GraphQLSchema,
1415
GraphQLFieldResolver,
1516
GraphQLTypeResolver,
17+
GraphQLFormattedError,
1618
} from 'graphql';
1719
import accepts from 'accepts';
1820
import httpError from 'http-errors';
@@ -98,7 +100,7 @@ export type OptionsData = {|
98100
* fulfilling a GraphQL operation. If no function is provided, GraphQL's
99101
* default spec-compliant `formatError` function will be used.
100102
*/
101-
customFormatErrorFn?: (error: GraphQLError) => mixed,
103+
customFormatErrorFn?: (error: GraphQLError) => GraphQLFormattedError,
102104

103105
/**
104106
* An optional function which will be used to create a document instead of
@@ -110,7 +112,7 @@ export type OptionsData = {|
110112
* `formatError` is deprecated and replaced by `customFormatErrorFn`. It will
111113
* be removed in version 1.0.0.
112114
*/
113-
formatError?: (error: GraphQLError) => mixed,
115+
formatError?: (error: GraphQLError) => GraphQLFormattedError,
114116

115117
/**
116118
* An optional function for adding additional metadata to the GraphQL response
@@ -380,22 +382,28 @@ export function graphqlHTTP(options: Options): Middleware {
380382
}
381383

382384
// Format any encountered errors.
383-
if (result.errors) {
384-
(result: any).errors = result.errors.map(formatErrorFn);
385-
}
385+
const formattedResult: FormattedExecutionResult = {
386+
...result,
387+
errors: result.errors?.map(formatErrorFn),
388+
};
386389

387390
// If allowed to show GraphiQL, present it instead of JSON.
388391
if (showGraphiQL) {
389-
return respondWithGraphiQL(response, graphiqlOptions, params, result);
392+
return respondWithGraphiQL(
393+
response,
394+
graphiqlOptions,
395+
params,
396+
formattedResult,
397+
);
390398
}
391399

392400
// If "pretty" JSON isn't requested, and the server provides a
393401
// response.json method (express), use that directly.
394402
// Otherwise use the simplified sendResponse method.
395403
if (!pretty && typeof response.json === 'function') {
396-
response.json(result);
404+
response.json(formattedResult);
397405
} else {
398-
const payload = JSON.stringify(result, null, pretty ? 2 : 0);
406+
const payload = JSON.stringify(formattedResult, null, pretty ? 2 : 0);
399407
sendResponse(response, 'application/json', payload);
400408
}
401409

@@ -431,7 +439,7 @@ function respondWithGraphiQL(
431439
response: $Response,
432440
options?: GraphiQLOptions,
433441
params?: GraphQLParams,
434-
result?: ExecutionResult,
442+
result?: FormattedExecutionResult,
435443
): void {
436444
const data: GraphiQLData = {
437445
query: params?.query,

src/renderGraphiQL.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { ExecutionResult } from 'graphql';
1+
import { FormattedExecutionResult } from 'graphql';
22

33
export interface GraphiQLData {
44
query?: string | null;
55
variables?: { readonly [name: string]: unknown } | null;
66
operationName?: string | null;
7-
result?: ExecutionResult;
7+
result?: FormattedExecutionResult;
88
}
99

1010
export interface GraphiQLOptions {

src/renderGraphiQL.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// @flow strict
22

3-
import type { ExecutionResult } from 'graphql';
3+
import type { FormattedExecutionResult } from 'graphql';
44

55
export type GraphiQLData = {|
66
query?: string | null,
77
variables?: { +[name: string]: mixed, ... } | null,
88
operationName?: string | null,
9-
result?: ExecutionResult,
9+
result?: FormattedExecutionResult,
1010
|};
1111

1212
export type GraphiQLOptions = {|

0 commit comments

Comments
 (0)