Skip to content

Commit

Permalink
Enable Apollo Server 2 to have async context + test (#1129)
Browse files Browse the repository at this point in the history
* core: add test for context as async function

* core,express: add support and test of context as async function
  • Loading branch information
evans authored Jun 4, 2018
1 parent 5c65742 commit 1e70a44
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
40 changes: 38 additions & 2 deletions packages/apollo-server-core/src/ApolloServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { expect } from 'chai';
import { stub } from 'sinon';
import * as http from 'http';
import * as net from 'net';
import * as MockReq from 'mock-req';
import 'mocha';

import {
Expand All @@ -25,6 +24,7 @@ Object.assign(global, {
import { createApolloFetch } from 'apollo-fetch';
import { ApolloServerBase } from './ApolloServer';
import { AuthenticationError } from './errors';
import { convertNodeHttpToRequest } from './nodeHttpToRequest';
import { runHttpQuery } from './runHttpQuery';
import gqlTag from 'graphql-tag';

Expand Down Expand Up @@ -78,7 +78,7 @@ function createHttpServer(server) {
method: req.method,
options: server.graphQLServerOptionsForRequest(req as any),
query: JSON.parse(body),
request: new MockReq(),
request: convertNodeHttpToRequest(req),
})
.then(gqlResponse => {
res.setHeader('Content-Type', 'application/json');
Expand Down Expand Up @@ -367,6 +367,42 @@ describe('ApolloServerBase', () => {
await server.stop();
});

it('allows context to be async function', async () => {
const uniqueContext = { key: 'major' };
const spy = stub().returns('hi');
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: (_parent, _args, context) => {
expect(context).to.equal(uniqueContext);
return spy();
},
},
};
const server = new ApolloServerBase({
typeDefs,
resolvers,
context: async () => uniqueContext,
});
const httpServer = createHttpServer(server);
server.use({
getHttp: () => httpServer,
path: '/',
});

const { url: uri } = await server.listen();
const apolloFetch = createApolloFetch({ uri });

expect(spy.notCalled).true;
await apolloFetch({ query: '{hello}' });
expect(spy.calledOnce).true;
await server.stop();
});

it('returns thrown context error as a valid graphql result', async () => {
const nodeEnv = process.env.NODE_ENV;
delete process.env.NODE_ENV;
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,13 @@ export class ApolloServerBase<Request = RequestInit> {
}
}

graphQLServerOptionsForRequest(request: Request) {
async graphQLServerOptionsForRequest(request: Request) {
let context: Context = this.context ? this.context : { request };

try {
context =
typeof this.context === 'function'
? this.context({ req: request })
? await this.context({ req: request })
: context;
} catch (error) {
//Defer context error resolution to inside of runQuery
Expand Down
20 changes: 8 additions & 12 deletions packages/apollo-server-express/src/ApolloServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ const resolvers = {
describe('apollo-server-express', () => {
//to remove the circular dependency, we reference it directly
const ApolloServer = require('../../apollo-server/dist/index').ApolloServer;
let server: ApolloServerBase<express.Request>;
let app: express.Application;

afterEach(async () => {
if (server) await server.stop();
});

describe('', () => {
it('accepts typeDefs and resolvers', () => {
Expand All @@ -44,12 +50,6 @@ describe('apollo-server-express', () => {
});

describe('registerServer', () => {
let server: ApolloServerBase<express.Request>;
let app: express.Application;
afterEach(async () => {
await server.stop();
});

it('can be queried', async () => {
server = new ApolloServer({
typeDefs,
Expand Down Expand Up @@ -377,14 +377,13 @@ describe('apollo-server-express', () => {
expect(e.extensions.exception.stacktrace).to.exist;

process.env.NODE_ENV = nodeEnv;
await server.stop();
});

it('propogates error codes in dev mode', async () => {
const nodeEnv = process.env.NODE_ENV;
delete process.env.NODE_ENV;

const server = new ApolloServer({
server = new ApolloServer({
typeDefs: gql`
type Query {
error: String
Expand Down Expand Up @@ -416,7 +415,6 @@ describe('apollo-server-express', () => {
expect(result.errors[0].extensions.exception.stacktrace).to.exist;

process.env.NODE_ENV = nodeEnv;
await server.stop();
});

it('propogates error codes in production', async () => {
Expand Down Expand Up @@ -454,14 +452,13 @@ describe('apollo-server-express', () => {
expect(result.errors[0].extensions.exception).not.to.exist;

process.env.NODE_ENV = nodeEnv;
await server.stop();
});

it('propogates error codes with null response in production', async () => {
const nodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';

const server = new ApolloServer({
server = new ApolloServer({
typeDefs: gql`
type Query {
error: String!
Expand Down Expand Up @@ -491,7 +488,6 @@ describe('apollo-server-express', () => {
expect(result.errors[0].extensions.exception).not.to.exist;

process.env.NODE_ENV = nodeEnv;
await server.stop();
});
});
});
Expand Down

0 comments on commit 1e70a44

Please sign in to comment.