Created
July 31, 2018 20:00
-
-
Save JakeDawkins/4b8b198ebd49f96a8f111421748ffbe2 to your computer and use it in GitHub Desktop.
how to e2e test an ApolloServer instance
This file contains hidden or 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
import { HttpLink } from 'apollo-link-http'; | |
import * as express from 'express'; | |
import fetch from 'node-fetch'; | |
import { AddressInfo } from 'net'; | |
import { execute } from 'apollo-link'; | |
export { toPromise } from 'apollo-link'; | |
import { ApolloServer } from './'; | |
export const startTestServer = async (server: ApolloServer) => { | |
const app = express(); | |
server.applyMiddleware({ app }); | |
const httpServer = await app.listen(0); | |
const link = new HttpLink({ | |
uri: `http://localhost:${ | |
(httpServer.address() as AddressInfo).port | |
}/graphql`, | |
fetch, | |
}); | |
const executeOperation = ({ | |
query, | |
variables = {}, | |
}: { | |
query: any; | |
variables: Record<string, any>; | |
}) => execute(link, { query, variables }); | |
return { link, stop: () => httpServer.close(), graphql: executeOperation }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment