Skip to content

Instantly share code, notes, and snippets.

@JakeDawkins
Created July 31, 2018 20:00
Show Gist options
  • Save JakeDawkins/4b8b198ebd49f96a8f111421748ffbe2 to your computer and use it in GitHub Desktop.
Save JakeDawkins/4b8b198ebd49f96a8f111421748ffbe2 to your computer and use it in GitHub Desktop.
how to e2e test an ApolloServer instance
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