Skip to content

Commit

Permalink
add apollo-server-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeDawkins committed Nov 1, 2018
1 parent 9deb176 commit 3537173
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 70 deletions.
38 changes: 36 additions & 2 deletions packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { formatApolloErrors } from 'apollo-server-errors';
import {
GraphQLServerOptions as GraphQLOptions,
PersistedQueryOptions,
resolveGraphqlOptions,
} from './graphqlOptions';

import {
Expand All @@ -44,7 +45,14 @@ import {
PlaygroundRenderPageOptions,
} from './playground';

// import { processGraphQLRequest } from './requestPipeline';
import {
processGraphQLRequest,
GraphQLRequestPipelineConfig,
GraphQLRequestContext,
GraphQLRequest,
} from './requestPipeline';

import { Headers } from "apollo-server-env";

const NoIntrospection = (context: ValidationContext) => ({
Field(node: FieldDefinitionNode) {
Expand Down Expand Up @@ -490,5 +498,31 @@ export class ApolloServerBase {
} as GraphQLOptions;
}

// public async executeOperation() {}
public async executeOperation(request: GraphQLRequest, contextArgs) {
let options;

try {
options = await this.graphQLServerOptions(contextArgs);
} catch (e) {
e.message = `Invalid options provided to ApolloServer: ${e.message}`;
throw new Error(e);
}

if (typeof options.context === 'function') {
options.context = (options.context as () => never)();
}

const requestCtx: GraphQLRequestContext = {
request,
context: options.context || {}
cache: options.cache!,
response: {
http: {
headers: new Headers(),
},
},
};

return processGraphQLRequest(options, requestCtx);
}
}
68 changes: 0 additions & 68 deletions packages/apollo-server-core/src/__tests__/test-utils.test.ts

This file was deleted.

5 changes: 5 additions & 0 deletions packages/apollo-server-testing/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

### vNEXT

* `apollo-server-testing`: Added createTestClient function
7 changes: 7 additions & 0 deletions packages/apollo-server-testing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# apollo-server-testing

[![npm version](https://badge.fury.io/js/apollo-server-testing.svg)](https://badge.fury.io/js/apollo-server-testing)
[![Build Status](https://circleci.com/gh/apollographql/apollo-server.svg?style=svg)](https://circleci.com/gh/apollographql/apollo-server)

This is the testing module of the Apollo community GraphQL Server. [Read the docs.](https://www.apollographql.com/docs/apollo-server/)
[Read the CHANGELOG.](https://github.com/apollographql/apollo-server/blob/master/CHANGELOG.md)
3 changes: 3 additions & 0 deletions packages/apollo-server-testing/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const config = require('../../jest.config.base');

module.exports = Object.assign(Object.create(null), config);
32 changes: 32 additions & 0 deletions packages/apollo-server-testing/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "apollo-server-testing",
"version": "2.2.0-alpha.0",
"description": "Test utils for apollo-server",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/apollographql/apollo-server/tree/master/packages/apollo-server-testing"
},
"keywords": [
"GraphQL",
"Apollo",
"Server",
"Javascript"
],
"author": "Jonas Helfer <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/apollographql/apollo-server/issues"
},
"homepage": "https://github.com/apollographql/apollo-server#readme",
"engines": {
"node": ">=6"
},
"dependencies": {
"apollo-server-core": "file:../apollo-server-core"
},
"peerDependencies": {
"graphql": "^0.12.0 || ^0.13.0 || ^14.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const { ApolloServerBase, gql } = require('apollo-server-core');
const createTestClient = require('../createTestClient');

describe('createTestClient', () => {
const typeDefs = gql`
type Query {
test(echo: String): String
# this resolver uses context
hello: String
}
type Mutation {
increment: Int!
}
`;

let num = 0;
const resolvers = {
Query: {
test: (_, { echo }) => echo,
hello: (_, __, { person }) => {
return `hello ${person}`;
},
},
Mutation: {
increment: () => ++num,
},
};

const myTestServer = new ApolloServerBase({
typeDefs,
context: () => ({ person: 'tom' }),
resolvers,
});

it('allows queries', async () => {
const query = `{ test(echo: "foo") }`;
const client = createTestClient(myTestServer);
const res = await client.query({ query });
expect(res.data).toEqual({ test: 'foo' });
});

it('allows mutations', async () => {
const mutation = `mutation increment { increment }`;
const client = createTestClient(myTestServer);
const res = await client.mutate({ mutation });
expect(res.data).toEqual({ increment: 1 });
});

it('allows variables to be passed', async () => {
const query = `query test($echo: String){ test(echo: $echo) }`;
const client = createTestClient(myTestServer);
const res = await client.query({ query, variables: { echo: 'wow' } });
expect(res.data).toEqual({ test: 'wow' });
});

it('uses default context function if not overwritten', async () => {
const query = `{ hello }`;
const client = createTestClient(myTestServer);
const res = await client.query({ query });
expect(res.data).toEqual({ hello: 'hello tom' });
});

it('allows mocking of context', async () => {
const query = `{ hello }`;
const client = createTestClient(myTestServer, () => ({ person: 'mary' }));
const res = await client.query({ query });
expect(res.data).toEqual({ hello: 'hello mary' });
});
});
5 changes: 5 additions & 0 deletions packages/apollo-server-testing/src/__tests__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "../../../../tsconfig.test.base",
"include": ["**/*"],
"references": []
}
26 changes: 26 additions & 0 deletions packages/apollo-server-testing/src/createTestClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ApolloServerBase, gql } from 'apollo-server-core';
import { print } from 'graphql';

const createTestClient = (server: ApolloServerBase, ctxFn: any) => {
// if a context fn is required, overwrite the old one
// allows easy mocking of the context
if (ctxFn) server.context = ctxFn;

const executeOperation = server.executeOperation.bind(server);
return {
query: ({ query, ...rest }) =>
executeOperation({
// print the query document if it isn't a string
query: typeof query === 'string' ? query : print(query),
...rest,
}),
mutate: ({ mutation, ...rest }) =>
executeOperation({
// print the query document if it isn't a string
query: typeof mutation === 'string' ? mutation : print(mutation),
...rest,
}),
};
};

module.exports = createTestClient;
5 changes: 5 additions & 0 deletions packages/apollo-server-testing/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const createTestClient = require('./createTestClient');

module.exports = {
createTestClient,
};
12 changes: 12 additions & 0 deletions packages/apollo-server-testing/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist"
},
"include": ["src/**/*"],
"exclude": ["**/__tests__", "**/__mocks__"],
"references": [
{ "path": "../apollo-server-core" }
]
}

0 comments on commit 3537173

Please sign in to comment.