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

Improve return type of customFormatErrorFn #673

Merged
merged 1 commit into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integrationTests/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"dependencies": {
"@types/node": "14.0.13",
"express-graphql": "file:../express-graphql.tgz",
"graphql": "14.6.0",
"graphql": "14.7.0",
"typescript-3.4": "npm:typescript@3.4.x",
"typescript-3.5": "npm:typescript@3.5.x",
"typescript-3.6": "npm:typescript@3.6.x",
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"express": "4.17.1",
"flow-bin": "0.128.0",
"graphiql": "1.0.3",
"graphql": "15.2.0",
"graphql": "15.3.0",
"mocha": "8.0.1",
"multer": "1.4.2",
"nyc": "15.1.0",
Expand All @@ -95,6 +95,6 @@
"unfetch": "4.1.0"
},
"peerDependencies": {
"graphql": "^14.6.0 || ^15.0.0"
"graphql": "^14.7.0 || ^15.3.0"
}
}
6 changes: 4 additions & 2 deletions src/__tests__/http-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2188,7 +2188,9 @@ function runTests(server: Server) {
urlString(),
graphqlHTTP({
schema: TestSchema,
customFormatErrorFn: () => null,
customFormatErrorFn: () => ({
message: 'Some generic error message.',
}),
extensions({ result }) {
return { preservedResult: { ...result } };
},
Expand All @@ -2204,7 +2206,7 @@ function runTests(server: Server) {
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
5 changes: 3 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ExecutionArgs,
ExecutionResult,
GraphQLError,
GraphQLFormattedError,
GraphQLSchema,
GraphQLFieldResolver,
GraphQLTypeResolver,
Expand Down Expand Up @@ -90,7 +91,7 @@ export interface OptionsData {
* fulfilling a GraphQL operation. If no function is provided, GraphQL's
* default spec-compliant `formatError` function will be used.
*/
customFormatErrorFn?: (error: GraphQLError) => unknown;
customFormatErrorFn?: (error: GraphQLError) => GraphQLFormattedError;

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

/**
* An optional function for adding additional metadata to the GraphQL response
Expand Down
26 changes: 17 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import type {
ValidationContext,
ExecutionArgs,
ExecutionResult,
FormattedExecutionResult,
GraphQLError,
GraphQLSchema,
GraphQLFieldResolver,
GraphQLTypeResolver,
GraphQLFormattedError,
} from 'graphql';
import accepts from 'accepts';
import httpError from 'http-errors';
Expand Down Expand Up @@ -98,7 +100,7 @@ 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) => mixed,
customFormatErrorFn?: (error: GraphQLError) => GraphQLFormattedError,

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

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

// Format any encountered errors.
if (result.errors) {
(result: any).errors = result.errors.map(formatErrorFn);
}
const formattedResult: FormattedExecutionResult = {
...result,
errors: result.errors?.map(formatErrorFn),
};

// If allowed to show GraphiQL, present it instead of JSON.
if (showGraphiQL) {
return respondWithGraphiQL(response, graphiqlOptions, params, result);
return respondWithGraphiQL(
response,
graphiqlOptions,
params,
formattedResult,
);
}

// If "pretty" JSON isn't requested, and the server provides a
// response.json method (express), use that directly.
// Otherwise use the simplified sendResponse method.
if (!pretty && typeof response.json === 'function') {
response.json(result);
response.json(formattedResult);
} else {
const payload = JSON.stringify(result, null, pretty ? 2 : 0);
const payload = JSON.stringify(formattedResult, null, pretty ? 2 : 0);
sendResponse(response, 'application/json', payload);
}

Expand Down Expand Up @@ -431,7 +439,7 @@ function respondWithGraphiQL(
response: $Response,
options?: GraphiQLOptions,
params?: GraphQLParams,
result?: ExecutionResult,
result?: FormattedExecutionResult,
): void {
const data: GraphiQLData = {
query: params?.query,
Expand Down
4 changes: 2 additions & 2 deletions src/renderGraphiQL.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ExecutionResult } from 'graphql';
import { FormattedExecutionResult } from 'graphql';

export interface GraphiQLData {
query?: string | null;
variables?: { readonly [name: string]: unknown } | null;
operationName?: string | null;
result?: ExecutionResult;
result?: FormattedExecutionResult;
}

export interface GraphiQLOptions {
Expand Down
4 changes: 2 additions & 2 deletions src/renderGraphiQL.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// @flow strict

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

export type GraphiQLData = {|
query?: string | null,
variables?: { +[name: string]: mixed, ... } | null,
operationName?: string | null,
result?: ExecutionResult,
result?: FormattedExecutionResult,
|};

export type GraphiQLOptions = {|
Expand Down