Skip to content

Commit

Permalink
Call formatError when passed in options to runHttpQuery, fixes apollo…
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser committed Sep 13, 2018
1 parent 520f910 commit 28f5ae2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
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 to `runHttpQuery`.
- 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
16 changes: 1 addition & 15 deletions packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ import {
FileUploadOptions,
} from './types';

import { FormatErrorExtension } from './formatters';

import { gql } from './index';

import {
Expand Down Expand Up @@ -212,21 +210,9 @@ export class ApolloServerBase {
}

// Note: doRunQuery will add its own extensions if you set tracing,
// or cacheControl.
// formatError, or cacheControl.
this.extensions = [];

// Error formatting should happen after the engine reporting agent, so that
// engine gets the unmasked errors if necessary
if (this.requestOptions.formatError) {
this.extensions.push(
() =>
new FormatErrorExtension(
this.requestOptions.formatError!,
this.requestOptions.debug,
),
);
}

if (engine || (engine !== false && process.env.ENGINE_API_KEY)) {
this.engineReportingAgent = new EngineReportingAgent(
engine === true ? {} : engine,
Expand Down
16 changes: 16 additions & 0 deletions packages/apollo-server-core/src/__tests__/runQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,20 @@ 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);
});
});
});
});
8 changes: 8 additions & 0 deletions packages/apollo-server-core/src/runQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {
SyntaxError,
} from 'apollo-server-errors';

import { FormatErrorExtension } from './formatters';

export interface GraphQLResponse {
data?: object;
errors?: Array<GraphQLError & object>;
Expand Down Expand Up @@ -98,6 +100,12 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
// objects.
const extensions = options.extensions ? options.extensions.map(f => f()) : [];

// Error formatting should happen after the engine reporting agent, so that
// engine gets the unmasked errors if necessary
if (options.formatError) {
extensions.unshift(new FormatErrorExtension(options.formatError, debug));
}

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

0 comments on commit 28f5ae2

Please sign in to comment.