Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added extend support to schemaGenerator #90

Merged
merged 3 commits into from
Aug 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

### vNEXT
* Added support for `extend` keyword to schemaGenerator https://github.com/apollostack/graphql-tools/pull/90

### v0.5.2

* Add addSchemaLevelResolveFunction to exports
Expand Down
25 changes: 22 additions & 3 deletions src/schemaGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// TODO: document each function clearly in the code: what arguments it accepts
// and what it outputs.

import { parse } from 'graphql/language';
import { parse, Kind } from 'graphql/language';
import uniq from 'lodash.uniq';
import { buildASTSchema } from 'graphql/utilities';
import { buildASTSchema, extendSchema } from 'graphql/utilities';
import {
GraphQLScalarType,
getNamedType,
Expand Down Expand Up @@ -121,7 +121,26 @@ function buildSchemaFromTypeDefinitions(typeDefinitions) {
}
myDefinitions = concatenateTypeDefs(myDefinitions);
}
return buildASTSchema(parse(myDefinitions));

const astDocument = parse(myDefinitions);
let schema = buildASTSchema(astDocument);

const extensionsAst = extractExtensionDefinitions(astDocument);
if (extensionsAst.definitions.length > 0) {
schema = extendSchema(schema, extensionsAst);
}

return schema;
}

function extractExtensionDefinitions(ast) {
const extensionDefs =
ast.definitions.filter((def) => def.kind === Kind.TYPE_EXTENSION_DEFINITION);

return {
...ast,
definitions: extensionDefs,
};
}

function forEachField(schema, fn) {
Expand Down
78 changes: 78 additions & 0 deletions test/testSchemaGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '../src/schemaGenerator.js';
import { assert, expect } from 'chai';
import { graphql, GraphQLInt, GraphQLObjectType, GraphQLSchema } from 'graphql';
import { printSchema } from 'graphql/utilities';
import { Logger } from '../src/Logger.js';
import TypeA from './circularSchemaA';

Expand Down Expand Up @@ -223,6 +224,27 @@ describe('generating schema from shorthand', () => {
const jsSchema = makeExecutableSchema({ typeDefs: typeDefAry, resolvers: {} });
expect(jsSchema.getQueryType().name).to.equal('Query');
});

it('can generate a schema from an array of types with extensions', () => {
const typeDefAry = [`
type Query {
foo: String
}
`, `
schema {
query: Query
}
`, `
extend type Query {
bar: String
}
`];

const jsSchema = makeExecutableSchema({ typeDefs: typeDefAry, resolvers: {} });
expect(jsSchema.getQueryType().name).to.equal('Query');
expect(jsSchema.getQueryType()._fields).to.have.all.keys('foo', 'bar');
});

it('properly deduplicates the array of type definitions', () => {
const typeDefAry = [`
type Query {
Expand Down Expand Up @@ -306,6 +328,62 @@ describe('generating schema from shorthand', () => {
return resultPromise.then(result => assert.deepEqual(result, solution));
});

it('can generate a schema with extensions that can use resolvers', () => {
const shorthand = `
type BirdSpecies {
name: String!,
wingspan: Int
}
type RootQuery {
species(name: String!): [BirdSpecies]
}
schema {
query: RootQuery
}
extend type BirdSpecies {
height: Float
}
`;

const resolveFunctions = {
RootQuery: {
species: (root, { name }) => [{
name: `Hello ${name}!`,
wingspan: 200,
height: 30.2,
}],
},
BirdSpecies: {
name: (bird) => bird.name,
wingspan: (bird) => bird.wingspan,
height: (bird) => bird.height,
},
};

const testQuery = `{
species(name: "BigBird"){
name
wingspan
height
}
}`;

const solution = {
data: {
species: [
{
name: 'Hello BigBird!',
wingspan: 200,
height: 30.2,
},
],
},
};
const jsSchema = makeExecutableSchema({ typeDefs: shorthand, resolvers: resolveFunctions });
const resultPromise = graphql(jsSchema, testQuery);
return resultPromise.then(result => assert.deepEqual(result, solution));
});

it('supports resolveType for unions', () => {
const shorthand = `
union Searchable = Person | Location
Expand Down