Skip to content

Commit

Permalink
rewrite tests to use makeExecutableSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
helfer committed Jul 8, 2016
1 parent 4cece79 commit d0695b7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 40 deletions.
4 changes: 2 additions & 2 deletions test/testDiscourseSchema.js
@@ -1,5 +1,5 @@
import { readFile } from 'fs';
import { generateSchema } from '../src/schemaGenerator.js';
import { makeExecutableSchema } from '../src/schemaGenerator.js';
import { assert } from 'chai';
import { graphql } from 'graphql';
import resolveFunctions from './discourse-api/schema.js';
Expand Down Expand Up @@ -52,7 +52,7 @@ describe('generating the discourse schema with resolvers', () => {
if (err) throw err;
// const rep = (key, val) => (typeof val === 'function') ? '[function]' : val;

const schema = generateSchema(data, resolveFunctions);
const schema = makeExecutableSchema({ typeDefs: data, resolvers: resolveFunctions });
const introspectionPromise = graphql(schema, introspectionQuery);
introspectionPromise.then((introspectionResult) => {
assert.deepEqual(introspectionResult, solution);
Expand Down
20 changes: 16 additions & 4 deletions test/testLogger.js
@@ -1,4 +1,4 @@
import { generateSchema } from '../src/schemaGenerator.js';
import { makeExecutableSchema } from '../src/schemaGenerator.js';
import { assert } from 'chai';
import { graphql } from 'graphql';
import { Logger } from '../src/Logger.js';
Expand Down Expand Up @@ -29,7 +29,11 @@ describe('Logger', () => {
},
};
const logger = new Logger();
const jsSchema = generateSchema(shorthand, resolve, logger);
const jsSchema = makeExecutableSchema({
typeDefs: shorthand,
resolvers: resolve,
logger,
});
// calling the mutation here so the erros will be ordered.
const testQuery = 'mutation { species, stuff }';
const expected0 = 'Error in resolver RootMutation.species\noops!';
Expand Down Expand Up @@ -60,7 +64,11 @@ describe('Logger', () => {
};
let loggedErr = null;
const logger = new Logger('LoggyMcLogface', (e) => { loggedErr = e; });
const jsSchema = generateSchema(shorthand, resolve, logger);
const jsSchema = makeExecutableSchema({
typeDefs: shorthand,
resolvers: resolve,
logger,
});
const testQuery = '{ species }';
graphql(jsSchema, testQuery).then(() => {
assert.equal(loggedErr, logger.errors[0]);
Expand Down Expand Up @@ -88,7 +96,11 @@ describe('Logger', () => {
},
};
const logger = new Logger();
const jsSchema = generateSchema(shorthand, resolve, logger);
const jsSchema = makeExecutableSchema({
typeDefs: shorthand,
resolvers: resolve,
logger,
});
const testQuery = '{ q: species, p: species(name: "Peter") }';
graphql(jsSchema, testQuery).then(() => {
const allErrors = logger.printAllErrors();
Expand Down
88 changes: 54 additions & 34 deletions test/testSchemaGenerator.js
@@ -1,10 +1,8 @@
// TODO: reduce code repetition in this file.
// see https://github.com/apollostack/graphql-tools/issues/26

// TODO rewrite tests to use makeExecutableSchema instead of generateSchema

import {
generateSchema,
makeExecutableSchema,
SchemaError,
addErrorLoggingToSchema,
Expand Down Expand Up @@ -63,19 +61,19 @@ const testConnectors = {

describe('generating schema from shorthand', () => {
it('throws an error if no schema is provided', () => {
assert.throw(generateSchema, SchemaError);
assert.throw(makeExecutableSchema, TypeError);
});

it('throws an error if no resolveFunctions are provided', () => {
assert.throw(generateSchema.bind(null, 'blah'), SchemaError);
assert.throw(() => makeExecutableSchema({ typeDefs: 'blah' }), SchemaError);
});

it('throws an error if typeDefinitions is neither string nor array', () => {
assert.throw(generateSchema.bind(null, {}, {}), SchemaError);
assert.throw(() => makeExecutableSchema({ typeDefs: {}, resolvers: {} }), SchemaError);
});

it('throws an error if typeDefinition array contains not only functions and strings', () => {
assert.throw(generateSchema.bind(null, [17], {}), SchemaError);
assert.throw(() => makeExecutableSchema({ typeDefs: [17], resolvers: {} }), SchemaError);
});

it('can generate a schema', (done) => {
Expand Down Expand Up @@ -195,7 +193,7 @@ describe('generating schema from shorthand', () => {
},
};

const jsSchema = generateSchema(shorthand, resolve);
const jsSchema = makeExecutableSchema({ typeDefs: shorthand, resolvers: resolve });
const resultPromise = graphql(jsSchema, introspectionQuery);
return resultPromise.then((result) => {
assert.deepEqual(result, solution);
Expand All @@ -214,7 +212,7 @@ describe('generating schema from shorthand', () => {
}
`];

const jsSchema = generateSchema(typeDefAry, {});
const jsSchema = makeExecutableSchema({ typeDefs: typeDefAry, resolvers: {} });
expect(jsSchema.getQueryType().name).to.equal('Query');
});
it('properly deduplicates the array of type definitions', () => {
Expand All @@ -232,7 +230,7 @@ describe('generating schema from shorthand', () => {
}
`];

const jsSchema = generateSchema(typeDefAry, {});
const jsSchema = makeExecutableSchema({ typeDefs: typeDefAry, resolvers: {} });
expect(jsSchema.getQueryType().name).to.equal('Query');
});

Expand All @@ -247,11 +245,11 @@ describe('generating schema from shorthand', () => {
}
`, TypeA];

const jsSchema = generateSchema(typeDefAry, {
const jsSchema = makeExecutableSchema({ typeDefs: typeDefAry, resolvers: {
Query: { foo: () => null },
TypeA: { b: () => null },
TypeB: { a: () => null },
});
} });
expect(jsSchema.getQueryType().name).to.equal('Query');
});

Expand Down Expand Up @@ -295,7 +293,7 @@ describe('generating schema from shorthand', () => {
],
},
};
const jsSchema = generateSchema(shorthand, resolveFunctions);
const jsSchema = makeExecutableSchema({ typeDefs: shorthand, resolvers: resolveFunctions });
const resultPromise = graphql(jsSchema, testQuery);
return resultPromise.then(result => assert.deepEqual(result, solution));
});
Expand Down Expand Up @@ -375,7 +373,7 @@ describe('generating schema from shorthand', () => {
},
};

const jsSchema = generateSchema(shorthand, resolveFunctions);
const jsSchema = makeExecutableSchema({ typeDefs: shorthand, resolvers: resolveFunctions });
const resultPromise = graphql(jsSchema, testQuery);
return resultPromise.then(result => assert.deepEqual(result, solution));
});
Expand Down Expand Up @@ -431,7 +429,7 @@ describe('generating schema from shorthand', () => {
},
};

const jsSchema = generateSchema(shorthand, resolveFunctions);
const jsSchema = makeExecutableSchema({ typeDefs: shorthand, resolvers: resolveFunctions });
const resultPromise = graphql(jsSchema, testQuery);
return resultPromise.then(result => assert.deepEqual(result, solution));
});
Expand All @@ -447,7 +445,7 @@ describe('generating schema from shorthand', () => {

const rf = { Query: {} };

assert.throws(generateSchema.bind(null, short, rf), SchemaError);
assert.throws(() => makeExecutableSchema({ typeDefs: short, resolvers: rf }), SchemaError);
});

it('does not throw an error if `resolverValidationOptions.requireResolversForArgs` is false', () => { // eslint-disable-line max-len
Expand Down Expand Up @@ -492,7 +490,7 @@ describe('generating schema from shorthand', () => {

const rf = { Query: { bird: 'NOT A FUNCTION' } };

assert.throws(generateSchema.bind(null, short, rf), SchemaError);
assert.throws(() => makeExecutableSchema({ typeDefs: short, resolvers: rf }), SchemaError);
});

it('throws an error if a field is not scalar, but has no resolve func', () => {
Expand All @@ -509,7 +507,7 @@ describe('generating schema from shorthand', () => {

const rf = {};

assert.throws(generateSchema.bind(null, short, rf), SchemaError);
assert.throws(() => makeExecutableSchema({ typeDefs: short, resolvers: rf }), SchemaError);
});

it('allows non-scalar field to use default resolve func if `resolverValidationOptions.requireResolversForNonScalar` = false', () => { // eslint-disable-line max-len
Expand Down Expand Up @@ -551,7 +549,10 @@ describe('generating schema from shorthand', () => {
}],
},
};
assert.throw(generateSchema.bind(null, shorthand, resolveFunctions), SchemaError);
assert.throw(
() => makeExecutableSchema({ typeDefs: shorthand, resolvers: resolveFunctions }),
SchemaError
);
done();
});
it('throws an error if a resolve type is not in schema', (done) => {
Expand All @@ -575,8 +576,10 @@ describe('generating schema from shorthand', () => {
}],
},
};
assert.throw(generateSchema.bind(null, shorthand, resolveFunctions), SchemaError);

assert.throw(
() => makeExecutableSchema({ typeDefs: shorthand, resolvers: resolveFunctions }),
SchemaError
);
done();
});
});
Expand All @@ -603,7 +606,11 @@ describe('providing useful errors from resolve functions', () => {
// TODO: Should use a spy here instead of logger class
// to make sure we don't duplicate tests from Logger.
const logger = new Logger();
const jsSchema = generateSchema(shorthand, resolve, logger);
const jsSchema = makeExecutableSchema({
typeDefs: shorthand,
resolvers: resolve,
logger,
});
const testQuery = '{ species }';
const expected = 'Error in resolver RootQuery.species\noops!';
graphql(jsSchema, testQuery).then(() => {
Expand Down Expand Up @@ -631,7 +638,12 @@ describe('providing useful errors from resolve functions', () => {
};

const logger = new Logger();
const jsSchema = generateSchema(shorthand, resolve, logger, false);
const jsSchema = makeExecutableSchema({
typeDefs: shorthand,
resolvers: resolve,
logger,
allowUndefinedInResolve: false,
});
const testQuery = '{ species, stuff }';
const expectedErr = /Resolve function for "RootQuery.species" returned undefined/;
const expectedResData = { species: null, stuff: 'stuff' };
Expand All @@ -656,7 +668,7 @@ describe('Add error logging to schema', () => {
describe('Attaching connectors to schema', () => {
describe('Schema level resolve function', () => {
it('actually runs', () => {
const jsSchema = generateSchema(testSchema, testResolvers);
const jsSchema = makeExecutableSchema({ typeDefs: testSchema, resolvers: testResolvers });
const rootResolver = () => ({ species: 'ROOT' });
addSchemaLevelResolveFunction(jsSchema, rootResolver);
const query = `{
Expand All @@ -668,7 +680,7 @@ describe('Attaching connectors to schema', () => {
});

it('can wrap fields that do not have a resolver defined', () => {
const jsSchema = generateSchema(testSchema, testResolvers);
const jsSchema = makeExecutableSchema({ typeDefs: testSchema, resolvers: testResolvers });
const rootResolver = () => ({ stuff: 'stuff' });
addSchemaLevelResolveFunction(jsSchema, rootResolver);
const query = `{
Expand All @@ -680,7 +692,15 @@ describe('Attaching connectors to schema', () => {
});

it('runs only once', () => {
const jsSchema = generateSchema(testSchema, testResolvers);
const simpleResolvers = {
RootQuery: {
usecontext: (r, a, ctx) => ctx.usecontext,
useTestConnector: (r, a, ctx) => ctx.connectors.TestConnector.get(),
useContextConnector: (r, a, ctx) => ctx.connectors.ContextConnector.get(),
species: (root, { name }) => root.species + name,
},
};
const jsSchema = makeExecutableSchema({ typeDefs: testSchema, resolvers: simpleResolvers });
let count = 0;
const rootResolver = () => {
if (count === 0) {
Expand All @@ -704,7 +724,7 @@ describe('Attaching connectors to schema', () => {
});

it('can attach things to context', () => {
const jsSchema = generateSchema(testSchema, testResolvers);
const jsSchema = makeExecutableSchema({ typeDefs: testSchema, resolvers: testResolvers });
const rootResolver = (o, a, ctx) => {
// eslint-disable-next-line no-param-reassign
ctx.usecontext = 'ABC';
Expand All @@ -722,7 +742,7 @@ describe('Attaching connectors to schema', () => {
});
});
it('actually attaches the connectors', () => {
const jsSchema = generateSchema(testSchema, testResolvers);
const jsSchema = makeExecutableSchema({ typeDefs: testSchema, resolvers: testResolvers });
attachConnectorsToContext(jsSchema, testConnectors);
const query = `{
useTestConnector
Expand All @@ -735,7 +755,7 @@ describe('Attaching connectors to schema', () => {
});
});
it('actually passes the context to the connector constructor', () => {
const jsSchema = generateSchema(testSchema, testResolvers);
const jsSchema = makeExecutableSchema({ typeDefs: testSchema, resolvers: testResolvers });
attachConnectorsToContext(jsSchema, testConnectors);
const query = `{
useContextConnector
Expand All @@ -748,14 +768,14 @@ describe('Attaching connectors to schema', () => {
});
});
it('throws error if trying to attach connectors twice', () => {
const jsSchema = generateSchema(testSchema, testResolvers);
const jsSchema = makeExecutableSchema({ typeDefs: testSchema, resolvers: testResolvers });
attachConnectorsToContext(jsSchema, testConnectors);
return expect(() => attachConnectorsToContext(jsSchema, testConnectors)).to.throw(
'Connectors already attached to context, cannot attach more than once'
);
});
it('throws error during execution if context is not an object', () => {
const jsSchema = generateSchema(testSchema, testResolvers);
const jsSchema = makeExecutableSchema({ typeDefs: testSchema, resolvers: testResolvers });
attachConnectorsToContext(jsSchema, { someConnector: {} });
const query = `{
useTestConnector
Expand All @@ -767,7 +787,7 @@ describe('Attaching connectors to schema', () => {
});
});
it('does not interfere with schema level resolve function', () => {
const jsSchema = generateSchema(testSchema, testResolvers);
const jsSchema = makeExecutableSchema({ typeDefs: testSchema, resolvers: testResolvers });
const rootResolver = () => ({ stuff: 'stuff', species: 'ROOT' });
addSchemaLevelResolveFunction(jsSchema, rootResolver);
attachConnectorsToContext(jsSchema, testConnectors);
Expand Down Expand Up @@ -800,19 +820,19 @@ describe('Attaching connectors to schema', () => {
);
});
it('throws error if connectors argument is an array', () => {
const jsSchema = generateSchema(testSchema, testResolvers);
const jsSchema = makeExecutableSchema({ typeDefs: testSchema, resolvers: testResolvers });
expect(() => attachConnectorsToContext(jsSchema, [1])).to.throw(
'Expected connectors to be of type object, got Array'
);
});
it('throws error if connectors argument is an empty object', () => {
const jsSchema = generateSchema(testSchema, testResolvers);
const jsSchema = makeExecutableSchema({ typeDefs: testSchema, resolvers: testResolvers });
return expect(() => attachConnectorsToContext(jsSchema, {})).to.throw(
'Expected connectors to not be an empty object'
);
});
it('throws error if connectors argument is not an object', () => {
const jsSchema = generateSchema(testSchema, testResolvers);
const jsSchema = makeExecutableSchema({ typeDefs: testSchema, resolvers: testResolvers });
return expect(() => attachConnectorsToContext(jsSchema, 'a')).to.throw(
'Expected connectors to be of type object, got string'
);
Expand Down

0 comments on commit d0695b7

Please sign in to comment.