From 2b470e1c1177fe0d6343a81028a5cbe8a97cf762 Mon Sep 17 00:00:00 2001 From: Jesse Rosenberger Date: Thu, 27 Sep 2018 16:36:28 +0300 Subject: [PATCH] Revert "Pass the context request and response extension methods (#1547)" This reverts commit 408198e5ac63ce447137f3dcbeb359f634910fe4. --- CHANGELOG.md | 1 + .../apollo-engine-reporting/src/extension.ts | 4 ++-- .../src/__tests__/runQuery.test.ts | 22 ------------------- packages/apollo-server-core/src/formatters.ts | 6 ++--- packages/apollo-server-core/src/runQuery.ts | 6 +---- .../src/ApolloServer.ts | 14 ++++-------- packages/graphql-extensions/src/index.ts | 8 ++----- 7 files changed, 12 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 794870a2f89..543f98a362b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### vNEXT +- FIXME(@abernix): Allow context to be passed to all GraphQLExtension methods. - FIXME(@abernix): client info in traces. - Allow an optional function to resolve the `rootValue`, passing the `DocumentNode` AST to determine the value. [PR #1555](https://github.com/apollographql/apollo-server/pull/1555) - Follow-up on the work in [PR #1516](https://github.com/apollographql/apollo-server/pull/1516) to also fix missing insertion cursor/caret when a custom GraphQL configuration is specified which doesn't specify its own `cursorShape` property. [PR #1607](https://github.com/apollographql/apollo-server/pull/1607) diff --git a/packages/apollo-engine-reporting/src/extension.ts b/packages/apollo-engine-reporting/src/extension.ts index 2387b3d2553..30783da6e95 100644 --- a/packages/apollo-engine-reporting/src/extension.ts +++ b/packages/apollo-engine-reporting/src/extension.ts @@ -57,7 +57,7 @@ export class EngineReportingExtension request: Request; queryString?: string; parsedQuery?: DocumentNode; - variables?: Record; + variables: Record; persistedQueryHit?: boolean; persistedQueryRegister?: boolean; }): EndHandler { @@ -140,7 +140,7 @@ export class EngineReportingExtension } else { try { this.trace.details!.variablesJson![name] = JSON.stringify( - o.variables![name], + o.variables[name], ); } catch (e) { // This probably means that the value contains a circular reference, diff --git a/packages/apollo-server-core/src/__tests__/runQuery.test.ts b/packages/apollo-server-core/src/__tests__/runQuery.test.ts index 5052984d386..895d4295576 100644 --- a/packages/apollo-server-core/src/__tests__/runQuery.test.ts +++ b/packages/apollo-server-core/src/__tests__/runQuery.test.ts @@ -370,28 +370,6 @@ describe('runQuery', () => { }); }); }); - - it('runs willSendResponse with extensions context', async () => { - class CustomExtension implements GraphQLExtension { - willSendResponse(o: any) { - expect(o).toHaveProperty('context.baz', 'always here'); - return o; - } - } - - const queryString = `{ testString }`; - const expected = { testString: 'it works' }; - const extensions = [() => new CustomExtension()]; - return runQuery({ - schema, - queryString, - context: { baz: 'always here' }, - extensions, - request: new MockReq(), - }).then(res => { - expect(res.data).toEqual(expected); - }); - }); }); describe('async_hooks', () => { diff --git a/packages/apollo-server-core/src/formatters.ts b/packages/apollo-server-core/src/formatters.ts index 8cedf112d20..8e708b41393 100644 --- a/packages/apollo-server-core/src/formatters.ts +++ b/packages/apollo-server-core/src/formatters.ts @@ -1,7 +1,7 @@ import { GraphQLExtension, GraphQLResponse } from 'graphql-extensions'; import { formatApolloErrors } from 'apollo-server-errors'; -export class FormatErrorExtension extends GraphQLExtension { +export class FormatErrorExtension extends GraphQLExtension { private formatError: Function; private debug: boolean; @@ -13,11 +13,9 @@ export class FormatErrorExtension extends GraphQLExtension { public willSendResponse(o: { graphqlResponse: GraphQLResponse; - context: TContext; - }): void | { graphqlResponse: GraphQLResponse; context: TContext } { + }): void | { graphqlResponse: GraphQLResponse } { if (o.graphqlResponse.errors) { return { - ...o, graphqlResponse: { ...o.graphqlResponse, errors: formatApolloErrors(o.graphqlResponse.errors, { diff --git a/packages/apollo-server-core/src/runQuery.ts b/packages/apollo-server-core/src/runQuery.ts index 0b33e730c4f..baf005930ea 100644 --- a/packages/apollo-server-core/src/runQuery.ts +++ b/packages/apollo-server-core/src/runQuery.ts @@ -135,7 +135,6 @@ function doRunQuery(options: QueryOptions): Promise { variables: options.variables, persistedQueryHit: options.persistedQueryHit, persistedQueryRegister: options.persistedQueryRegister, - context, }); return Promise.resolve() .then( @@ -282,10 +281,7 @@ function doRunQuery(options: QueryOptions): Promise { throw err; }) .then((graphqlResponse: GraphQLResponse) => { - const response = extensionStack.willSendResponse({ - graphqlResponse, - context, - }); + const response = extensionStack.willSendResponse({ graphqlResponse }); requestDidEnd(); return response.graphqlResponse; }); diff --git a/packages/apollo-server-integration-testsuite/src/ApolloServer.ts b/packages/apollo-server-integration-testsuite/src/ApolloServer.ts index 488f2fad4d7..f566a970e27 100644 --- a/packages/apollo-server-integration-testsuite/src/ApolloServer.ts +++ b/packages/apollo-server-integration-testsuite/src/ApolloServer.ts @@ -526,11 +526,8 @@ export function testApolloServer( return error; }); - class Extension extends GraphQLExtension { - willSendResponse(o: { - graphqlResponse: GraphQLResponse; - context: TContext; - }) { + class Extension extends GraphQLExtension { + willSendResponse(o: { graphqlResponse: GraphQLResponse }) { expect(o.graphqlResponse.errors.length).toEqual(1); // formatError should be called after extensions expect(formatError).not.toBeCalled(); @@ -612,11 +609,8 @@ export function testApolloServer( return error; }); - class Extension extends GraphQLExtension { - willSendResponse(_o: { - graphqlResponse: GraphQLResponse; - context: TContext; - }) { + class Extension extends GraphQLExtension { + willSendResponse(_o: { graphqlResponse: GraphQLResponse }) { // formatError should be called after extensions expect(formatError).not.toBeCalled(); extension(); diff --git a/packages/graphql-extensions/src/index.ts b/packages/graphql-extensions/src/index.ts index b7f358f6da8..e09457768db 100644 --- a/packages/graphql-extensions/src/index.ts +++ b/packages/graphql-extensions/src/index.ts @@ -40,7 +40,6 @@ export class GraphQLExtension { variables?: { [key: string]: any }; persistedQueryHit?: boolean; persistedQueryRegister?: boolean; - context: TContext; }): EndHandler | void; public parsingDidStart?(o: { queryString: string }): EndHandler | void; public validationDidStart?(): EndHandler | void; @@ -50,8 +49,7 @@ export class GraphQLExtension { public willSendResponse?(o: { graphqlResponse: GraphQLResponse; - context: TContext; - }): void | { graphqlResponse: GraphQLResponse; context: TContext }; + }): void | { graphqlResponse: GraphQLResponse }; public willResolveField?( source: any, @@ -80,7 +78,6 @@ export class GraphQLExtensionStack { variables?: { [key: string]: any }; persistedQueryHit?: boolean; persistedQueryRegister?: boolean; - context: TContext; }): EndHandler { return this.handleDidStart( ext => ext.requestDidStart && ext.requestDidStart(o), @@ -107,8 +104,7 @@ export class GraphQLExtensionStack { public willSendResponse(o: { graphqlResponse: GraphQLResponse; - context: TContext; - }): { graphqlResponse: GraphQLResponse; context: TContext } { + }): { graphqlResponse: GraphQLResponse } { let reference = o; // Reverse the array, since this is functions as an end handler [...this.extensions].reverse().forEach(extension => {