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

apollo-server-core: pass req obj to context when invoking executeOperation #2478

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### vNEXT

- Allow `GraphQLRequestListener` callbacks in plugins to depend on `this`. [PR #2470](https://github.com/apollographql/apollo-server/pull/2470)
- `apollo-server-core`: Pass request object to `context` callback function when invoking the `executeOperation` method. [PR #2478](https://github.com/apollographql/apollo-server/pull/2478)

### v2.4.8

Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ export class ApolloServerBase {
let options;

try {
options = await this.graphQLServerOptions();
options = await this.graphQLServerOptions({ req: request });
} catch (e) {
e.message = `Invalid options provided to ApolloServer: ${e.message}`;
throw new Error(e);
Expand Down
40 changes: 40 additions & 0 deletions packages/apollo-server-core/src/__tests__/ApolloServer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import MockReq = require('mock-req');

import { ApolloServerBase } from '../ApolloServer';
import { GraphQLSchema, GraphQLObjectType, GraphQLString } from 'graphql';

const queryType = new GraphQLObjectType({
name: 'QueryType',
fields: {
testString: {
type: GraphQLString,
resolve() {
return 'it works';
},
},
},
});

describe('ApolloServer', () => {
describe('executeOperation', () => {
it('Passes the request object to the context callback', () => {
const schema = new GraphQLSchema({
query: queryType,
});
const contextMock = jest.fn();
const apolloServer = new ApolloServerBase({
schema,
context: contextMock,
});
const mockRequest = new MockReq();
const operation = {
query: '{ }',
http: mockRequest,
};

apolloServer.executeOperation(operation);

expect(contextMock).toHaveBeenCalledWith({ req: operation });
});
});
});