Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Use custom one-off schemas for tests that need them #485

Merged
merged 1 commit into from
May 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 57 additions & 61 deletions src/__tests__/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import connect from 'connect';
import express from 'express';
import restify from 'restify';
import {
buildSchema,
GraphQLSchema,
GraphQLObjectType,
GraphQLNonNull,
Expand All @@ -41,37 +42,16 @@ const QueryRootType = new GraphQLObjectType({
test: {
type: GraphQLString,
args: {
who: {
type: GraphQLString,
},
who: { type: GraphQLString },
},
resolve: (root, { who }) => 'Hello ' + ((who: any) || 'World'),
},
nonNullThrower: {
type: new GraphQLNonNull(GraphQLString),
resolve: () => {
throw new Error('Throws!');
},
},
thrower: {
type: GraphQLString,
resolve: () => {
resolve() {
throw new Error('Throws!');
},
},
context: {
type: GraphQLString,
resolve: (obj, args, context) => context,
},
contextDotFoo: {
type: GraphQLString,
resolve: (obj, args, context) => {
return (context: any).foo;
},
},
missingResolver: {
type: GraphQLString,
},
},
});

Expand Down Expand Up @@ -349,67 +329,85 @@ describe('test harness', () => {
});

it('Allows passing in a context', async () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
test: {
type: GraphQLString,
resolve: (obj, args, context) => context,
},
},
}),
});
const app = server();

get(
app,
urlString(),
graphqlHTTP({
schema: TestSchema,
schema,
context: 'testValue',
}),
);

const response = await request(app).get(
urlString({
operationName: 'TestQuery',
query: `
query TestQuery { context }
`,
query: '{ test }',
}),
);

expect(response.status).to.equal(200);
expect(JSON.parse(response.text)).to.deep.equal({
data: {
context: 'testValue',
test: 'testValue',
},
});
});

it('Allows passing in a fieldResolver', async () => {
const schema = buildSchema(`
type Query {
test: String
}
`);
const app = server();

get(
app,
urlString(),
graphqlHTTP({
schema: TestSchema,
context: 'testValue',
fieldResolver() {
return 'fieldResolver data';
},
schema,
fieldResolver: () => 'fieldResolver data',
}),
);

const response = await request(app).get(
urlString({
operationName: 'TestQuery',
query: `
query TestQuery { missingResolver }
`,
query: '{ test }',
}),
);

expect(response.status).to.equal(200);
expect(JSON.parse(response.text)).to.deep.equal({
data: {
missingResolver: 'fieldResolver data',
test: 'fieldResolver data',
},
});
});

it('Uses request as context by default', async () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
test: {
type: GraphQLString,
resolve: (obj, args, context) => (context: any).foo,
},
},
}),
});
const app = server();

// Middleware that adds req.foo to every request
Expand All @@ -418,27 +416,18 @@ describe('test harness', () => {
next();
});

get(
app,
urlString(),
graphqlHTTP({
schema: TestSchema,
}),
);
get(app, urlString(), graphqlHTTP({ schema }));

const response = await request(app).get(
urlString({
operationName: 'TestQuery',
query: `
query TestQuery { contextDotFoo }
`,
query: '{ test }',
}),
);

expect(response.status).to.equal(200);
expect(JSON.parse(response.text)).to.deep.equal({
data: {
contextDotFoo: 'bar',
test: 'bar',
},
});
});
Expand Down Expand Up @@ -1172,19 +1161,26 @@ describe('test harness', () => {
});

it('handles query errors from non-null top field errors', async () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
test: {
type: new GraphQLNonNull(GraphQLString),
resolve() {
throw new Error('Throws!');
},
},
},
}),
});
const app = server();

get(
app,
urlString(),
graphqlHTTP({
schema: TestSchema,
}),
);
get(app, urlString(), graphqlHTTP({ schema }));

const response = await request(app).get(
urlString({
query: '{nonNullThrower}',
query: '{ test }',
}),
);

Expand All @@ -1194,8 +1190,8 @@ describe('test harness', () => {
errors: [
{
message: 'Throws!',
locations: [{ line: 1, column: 2 }],
path: ['nonNullThrower'],
locations: [{ line: 1, column: 3 }],
path: ['test'],
},
],
});
Expand Down