Skip to content
Closed
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions src/type/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,22 @@ export class GraphQLSchema {

type TypeMap = { [typeName: string]: GraphQLNamedType };

type GraphQLSchemaConfig = {
query: GraphQLObjectType;
type GraphQLSchemaConfigBase = {
mutation?: ?GraphQLObjectType;
subscription?: ?GraphQLObjectType;
types?: ?Array<GraphQLNamedType>;
directives?: ?Array<GraphQLDirective>;
astNode?: ?SchemaDefinitionNode;
};

type GraphQLSchemaConfig = GraphQLSchemaConfigBase & {
query: GraphQLObjectType;
};

export type GraphQLSchemaComponents = GraphQLSchemaConfigBase & {
query?: ?GraphQLObjectType;
};

function typeMapReducer(map: TypeMap, type: ?GraphQLType): TypeMap {
if (!type) {
return map;
Expand Down
18 changes: 17 additions & 1 deletion src/utilities/__tests__/buildASTSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { expect } from 'chai';
import { describe, it } from 'mocha';
import { parse, print } from '../../language';
import { printSchema } from '../schemaPrinter';
import { buildASTSchema, buildSchema } from '../buildASTSchema';
import {
buildASTSchemaComponents,
buildASTSchema,
buildSchema
} from '../buildASTSchema';
import dedent from '../../jsutils/dedent';
import {
graphql,
Expand All @@ -35,6 +39,18 @@ function cycleOutput(body) {

describe('Schema Builder', () => {

it('can build schema config for IDL with missing query type', async () => {
const config = buildASTSchemaComponents(parse(`
scalar TestType
directive @testDirective(testArg: TestType, testArg2: Float) on FIELD
`));

expect(config.types.length).to.equal(1);
expect(config.types[0].name).to.equal('TestType');
expect(config.directives.length).to.equal(1);
expect(config.directives[0].name).to.equal('testDirective');
});

it('can use built schema for limited execution', async () => {
const schema = buildASTSchema(parse(`
schema { query: Query }
Expand Down
68 changes: 45 additions & 23 deletions src/utilities/buildASTSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ import type {
} from '../language/ast';

import { GraphQLSchema } from '../type/schema';
import type {
GraphQLSchemaComponents,
} from '../type/schema';

import {
GraphQLString,
Expand Down Expand Up @@ -127,6 +130,43 @@ function getNamedTypeNode(typeNode: TypeNode): NamedTypeNode {
* has no resolve methods, so execution will use default resolvers.
*/
export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
const config: any = buildASTSchemaComponents(ast);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you can fix the any check with:

const config = buildASTSchemaComponents(ast);
if (!config.query) {
  throw new GraphQLError(
    'Must provide schema definition with query type or a type named Query.',
    ast,
  );
}
...
return new GraphQLSchema(config);

I believe the issue for flow is that config.query can be undefined in addition to null.

const directives = config.directives;

if (config.query === null) {
throw new Error(
'Must provide schema definition with query type or a type named Query.'
);
}

// If specified directives were not explicitly declared, add them.
if (!directives.some(directive => directive.name === 'skip')) {
directives.push(GraphQLSkipDirective);
}

if (!directives.some(directive => directive.name === 'include')) {
directives.push(GraphQLIncludeDirective);
}

if (!directives.some(directive => directive.name === 'deprecated')) {
directives.push(GraphQLDeprecatedDirective);
}

return new GraphQLSchema(config);
}

/**
* This takes the ast of a schema document produced by the parse function in
* src/language/parser.js.
*
* If no schema definition is provided, then it will look for types named Query
* and Mutation.
*
* Given that AST it return object that can be used to construct a GraphQLSchema
* instance.
*/
export function buildASTSchemaComponents(ast: DocumentNode):
GraphQLSchemaComponents {
if (!ast || ast.kind !== Kind.DOCUMENT) {
throw new Error('Must provide a document ast.');
}
Expand Down Expand Up @@ -214,12 +254,6 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
}
}

if (!queryTypeName) {
throw new Error(
'Must provide schema definition with query type or a type named Query.'
);
}

const innerTypeMap = {
String: GraphQLString,
Int: GraphQLInt,
Expand All @@ -237,24 +271,12 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
};

const types = typeDefs.map(def => typeDefNamed(def.name.value));

const directives = directiveDefs.map(getDirective);

// If specified directives were not explicitly declared, add them.
if (!directives.some(directive => directive.name === 'skip')) {
directives.push(GraphQLSkipDirective);
}

if (!directives.some(directive => directive.name === 'include')) {
directives.push(GraphQLIncludeDirective);
}

if (!directives.some(directive => directive.name === 'deprecated')) {
directives.push(GraphQLDeprecatedDirective);
}

return new GraphQLSchema({
query: getObjectType(nodeMap[queryTypeName]),
return {
query: queryTypeName ?
getObjectType(nodeMap[queryTypeName]) :
null,
mutation: mutationTypeName ?
getObjectType(nodeMap[mutationTypeName]) :
null,
Expand All @@ -264,7 +286,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
types,
directives,
astNode: schemaDef,
});
};

function getDirective(
directiveNode: DirectiveDefinitionNode
Expand Down