Skip to content

Commit

Permalink
Added extend support to schemaGenerator (#90)
Browse files Browse the repository at this point in the history
* Added `extend` support to schemaGenerator

* Added `extend` support to schemaGenerator

* Changelog update

* Removed never-called branch of code. This is so due to parse function throwing an error or creating an AST document.

* Also changed version on changelog to vNEXT as required
  • Loading branch information
davidyaha authored and helfer committed Aug 5, 2016
1 parent 47d9328 commit 145801c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
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

0 comments on commit 145801c

Please sign in to comment.