diff --git a/CHANGELOG.md b/CHANGELOG.md index abe87e7f253..41241f3e505 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/apollo-server-core/src/ApolloServer.ts b/packages/apollo-server-core/src/ApolloServer.ts index cfed6b76d19..04cc06b32a6 100644 --- a/packages/apollo-server-core/src/ApolloServer.ts +++ b/packages/apollo-server-core/src/ApolloServer.ts @@ -33,8 +33,6 @@ import { FileUploadOptions, } from './types'; -import { FormatErrorExtension } from './formatters'; - import { gql } from './index'; import { @@ -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, diff --git a/packages/apollo-server-core/src/__tests__/runQuery.test.ts b/packages/apollo-server-core/src/__tests__/runQuery.test.ts index f2b6729eda4..ee27654370f 100644 --- a/packages/apollo-server-core/src/__tests__/runQuery.test.ts +++ b/packages/apollo-server-core/src/__tests__/runQuery.test.ts @@ -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); + }); + }); + }); }); diff --git a/packages/apollo-server-core/src/runQuery.ts b/packages/apollo-server-core/src/runQuery.ts index 516750dfd78..c41b9dd40e4 100644 --- a/packages/apollo-server-core/src/runQuery.ts +++ b/packages/apollo-server-core/src/runQuery.ts @@ -33,6 +33,8 @@ import { SyntaxError, } from 'apollo-server-errors'; +import { FormatErrorExtension } from './formatters'; + export interface GraphQLResponse { data?: object; errors?: Array; @@ -98,6 +100,12 @@ function doRunQuery(options: QueryOptions): Promise { // 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) {