forked from apollographql/apollo-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apollo-server-core: pass req obj to context when invoking executeOper…
…ation Fixes apollographql#2277. `apollo-server-testing` relies on `apollo-server-core` to execute queries for integration tests, via the `executeOperation` method. However, when executing a query via `executeOperation`, `apollo-server-core` was not passing the `req` object to the `context` callback function used to create the server. This is fine if you're using a mock `context` object directly in your tests, but for tests that want to run a `context` callback that contains logic that depends on that `req` object, it would fail. Note that long-term the best fix for this is that `apollo-server-testing` should use the `ApolloServer` class from `apollo-server-express` internally, instead of `ApolloServerBase` from `apollo-server-core`, since it seems like that is the class that is used by default in `apollo-server`.
- Loading branch information
Showing
3 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/apollo-server-core/src/__tests__/ApolloServer.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); | ||
}); | ||
}); |