Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call formatError when passed in options to runQuery, fixes #1439 #1660

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All of the packages in the `apollo-server` repo are released with the same versi

### vNEXT

- Fix [#1439](https://github.com/apollographql/apollo-server/issues/1439), use `formatError` if passed in options.
- Core: Allow context to be passed to all GraphQLExtension methods. [PR #1547](https://github.com/apollographql/apollo-server/pull/1547)

### v2.0.7
Expand Down
36 changes: 36 additions & 0 deletions packages/apollo-server-core/src/__tests__/runQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { runQuery } from '../runQuery';

import { GraphQLExtensionStack, GraphQLExtension } from 'graphql-extensions';
import { FormatErrorExtension } from '../formatters';

const queryType = new GraphQLObjectType({
name: 'QueryType',
Expand Down Expand Up @@ -419,4 +420,39 @@ describe('runQuery', () => {
expect(ids.length).toBeGreaterThanOrEqual(2);
});
});

describe('formatError', () => {
it('is called with errors if passed in options', () => {
const formatError = jest.fn();
const query = `query { testError }`;
return runQuery({
schema,
queryString: query,
variables: { base: 1 },
request: new MockReq(),
formatError,
}).then(() => {
expect(formatError).toBeCalledTimes(1);
});
});

it('overrides FormatErrorExtension in extensions if passed', () => {
const formatError = jest.fn();
const extensionsFormatError = jest.fn();
const query = `query { testError }`;
return runQuery({
schema,
queryString: query,
variables: { base: 1 },
request: new MockReq(),
formatError,
extensions: [
() => new FormatErrorExtension(extensionsFormatError, false),
],
}).then(() => {
expect(formatError).toBeCalledTimes(1);
expect(extensionsFormatError).toBeCalledTimes(0);
});
});
});
});
22 changes: 21 additions & 1 deletion packages/apollo-server-core/src/runQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
ValidationError,
SyntaxError,
} from 'apollo-server-errors';
import { FormatErrorExtension } from '../src/formatters';

export interface GraphQLResponse {
data?: object;
Expand Down Expand Up @@ -94,9 +95,28 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {

const context = options.context || {};

let hasFormatError = false;

// If custom extension factories were provided, create per-request extension
// objects.
const extensions = options.extensions ? options.extensions.map(f => f()) : [];
const extensions = options.extensions
? options.extensions.map(f => {
const ext = f();
// If we've passed formatError as part of the options,
// ensure it overrides any FormatErrorExtension from the constructor.
if (options.formatError && !hasFormatError) {
if (ext instanceof FormatErrorExtension) {
hasFormatError = true;
return new FormatErrorExtension(options.formatError, debug);
}
}
return ext;
})
: [];

if (options.formatError && !hasFormatError) {
extensions.push(new FormatErrorExtension(options.formatError, debug));
}

// If you're running behind an engineproxy, set these options to turn on
// tracing and cache-control extensions.
Expand Down