Skip to content

Commit

Permalink
Merge c239ddd into 47d9328
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyaha committed Aug 4, 2016
2 parents 47d9328 + c239ddd commit 9ca55fd
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -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
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
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 9ca55fd

Please sign in to comment.