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

Pass the context along to all the extension methods #1547

Merged
merged 5 commits into from
Sep 8, 2018
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All of the packages in the `apollo-server` repo are released with the same versi

### vNEXT

- Core: Allow context to be passed to all GraphQLExtension methods. [PR #1547](https://github.com/apollographql/apollo-server/pull/1547)

### v2.0.7

- Fix [#1581](https://github.com/apollographql/apollo-server/issues/1581) `apollo-server-micro` top level error response [#1619](https://github.com/apollographql/apollo-server/pull/1619)
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-engine-reporting/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class EngineReportingExtension<TContext = any>
request: Request;
queryString?: string;
parsedQuery?: DocumentNode;
variables: Record<string, any>;
variables?: Record<string, any>;
persistedQueryHit?: boolean;
persistedQueryRegister?: boolean;
}): EndHandler {
Expand Down Expand Up @@ -139,7 +139,7 @@ export class EngineReportingExtension<TContext = any>
this.trace.details!.variablesJson![name] = '';
} else {
this.trace.details!.variablesJson![name] = JSON.stringify(
o.variables[name],
o.variables![name],
);
}
});
Expand Down
22 changes: 22 additions & 0 deletions packages/apollo-server-core/src/__tests__/runQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,28 @@ describe('runQuery', () => {
});
});
});

it('runs willSendResponse with extensions context', async () => {
class CustomExtension implements GraphQLExtension<any> {
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', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/apollo-server-core/src/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GraphQLExtension, GraphQLResponse } from 'graphql-extensions';
import { formatApolloErrors } from 'apollo-server-errors';

export class FormatErrorExtension extends GraphQLExtension {
export class FormatErrorExtension<TContext = any> extends GraphQLExtension {
private formatError: Function;
private debug: boolean;

Expand All @@ -13,9 +13,11 @@ export class FormatErrorExtension extends GraphQLExtension {

public willSendResponse(o: {
graphqlResponse: GraphQLResponse;
}): void | { graphqlResponse: GraphQLResponse } {
context: TContext;
}): void | { graphqlResponse: GraphQLResponse; context: TContext } {
if (o.graphqlResponse.errors) {
return {
...o,
graphqlResponse: {
...o.graphqlResponse,
errors: formatApolloErrors(o.graphqlResponse.errors, {
Expand Down
6 changes: 5 additions & 1 deletion packages/apollo-server-core/src/runQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
variables: options.variables,
persistedQueryHit: options.persistedQueryHit,
persistedQueryRegister: options.persistedQueryRegister,
context,
});
return Promise.resolve()
.then(
Expand Down Expand Up @@ -278,7 +279,10 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
throw err;
})
.then((graphqlResponse: GraphQLResponse) => {
const response = extensionStack.willSendResponse({ graphqlResponse });
const response = extensionStack.willSendResponse({
graphqlResponse,
context,
});
requestDidEnd();
return response.graphqlResponse;
});
Expand Down
14 changes: 10 additions & 4 deletions packages/apollo-server-integration-testsuite/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,11 @@ export function testApolloServer<AS extends ApolloServerBase>(
return error;
});

class Extension extends GraphQLExtension {
willSendResponse(o: { graphqlResponse: GraphQLResponse }) {
class Extension<TContext = any> extends GraphQLExtension {
willSendResponse(o: {
graphqlResponse: GraphQLResponse;
context: TContext;
}) {
expect(o.graphqlResponse.errors.length).toEqual(1);
// formatError should be called after extensions
expect(formatError).not.toBeCalled();
Expand Down Expand Up @@ -609,8 +612,11 @@ export function testApolloServer<AS extends ApolloServerBase>(
return error;
});

class Extension extends GraphQLExtension {
willSendResponse(_o: { graphqlResponse: GraphQLResponse }) {
class Extension<TContext = any> extends GraphQLExtension {
willSendResponse(_o: {
graphqlResponse: GraphQLResponse;
context: TContext;
}) {
// formatError should be called after extensions
expect(formatError).not.toBeCalled();
extension();
Expand Down
8 changes: 6 additions & 2 deletions packages/graphql-extensions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class GraphQLExtension<TContext = any> {
variables?: { [key: string]: any };
persistedQueryHit?: boolean;
persistedQueryRegister?: boolean;
context: TContext;
}): EndHandler | void;
public parsingDidStart?(o: { queryString: string }): EndHandler | void;
public validationDidStart?(): EndHandler | void;
Expand All @@ -49,7 +50,8 @@ export class GraphQLExtension<TContext = any> {

public willSendResponse?(o: {
graphqlResponse: GraphQLResponse;
}): void | { graphqlResponse: GraphQLResponse };
context: TContext;
}): void | { graphqlResponse: GraphQLResponse; context: TContext };

public willResolveField?(
source: any,
Expand Down Expand Up @@ -78,6 +80,7 @@ export class GraphQLExtensionStack<TContext = any> {
variables?: { [key: string]: any };
persistedQueryHit?: boolean;
persistedQueryRegister?: boolean;
context: TContext;
}): EndHandler {
return this.handleDidStart(
ext => ext.requestDidStart && ext.requestDidStart(o),
Expand All @@ -104,7 +107,8 @@ export class GraphQLExtensionStack<TContext = any> {

public willSendResponse(o: {
graphqlResponse: GraphQLResponse;
}): { graphqlResponse: GraphQLResponse } {
context: TContext;
}): { graphqlResponse: GraphQLResponse; context: TContext } {
let reference = o;
// Reverse the array, since this is functions as an end handler
[...this.extensions].reverse().forEach(extension => {
Expand Down