Skip to content

Commit

Permalink
Call formatError when passed in options to runQuery, fixes apollograp…
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser committed Sep 13, 2018
1 parent e49e172 commit 56eff90
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
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

0 comments on commit 56eff90

Please sign in to comment.