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. However, `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, since it seems like that is the class that is used by default in `apollo-server`.
- Loading branch information
Showing
3 changed files
with
44 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 }); | ||
}); | ||
}); | ||
}); |
d931cea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, did you a pull request in apollo server repo? I would like to be able to test my auth flow using this feature 👍
d931cea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Gasperan I ended up discarding this change for the reasons described here. I did create a new package specifically for testing with
apollo-server-express
though, and that works for my use case. I'll see if I can publish it to NPM this week if you'd like to try it :)d931cea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vitorbal What is the package that you pushed to NPM, can you point me towards that?
d931cea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ShashwatMittal sorry, I haven't actually gotten around to publishing this as an open-source package yet. Were you planning on using it if I published it? I don't have much bandwidth right now but I can see about publishing it as-is, at least for now, if that would help.
d931cea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vitorbal Yes, I need to write integrated tests for my GraphQL server and that is dependent on my context, or else you may say I need to set the headers for my request object.
d931cea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ShashwatMittal @Gasperan sorry for the lull, I've been pretty busy. I just published the package to NPM:
https://github.com/zapier/apollo-server-integration-testing
It mimics the apollo-server-testing API, but allows for passing in a mock req object (or sets one for you automatically, with sensible defaults).
We've been using it successfully at my company for the last 6 months to write real integration tests. In case you're still interested in giving it a try :)
d931cea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vitorbal Thanks for this. We did manage to use apollo-server-testing class without the header data for now. But I will definitely give this a try.