@@ -13,9 +13,11 @@ import invariant from '../jsutils/invariant';
1313import keyValMap from '../jsutils/keyValMap' ;
1414import { valueFromAST } from './valueFromAST' ;
1515import { TokenKind } from '../language/lexer' ;
16+ import { getLocation } from '../language/location' ;
1617import { parse } from '../language/parser' ;
17- import type { Source } from '../language/source' ;
18+ import { Source } from '../language/source' ;
1819import { getArgumentValues } from '../execution/values' ;
20+ import { invariantError } from '../error/syntaxError' ;
1921
2022import {
2123 LIST_TYPE ,
@@ -135,7 +137,7 @@ function getNamedTypeNode(typeNode: TypeNode): NamedTypeNode {
135137 * Given that AST it constructs a GraphQLSchema. The resulting schema
136138 * has no resolve methods, so execution will use default resolvers.
137139 */
138- export function buildASTSchema ( ast : DocumentNode ) : GraphQLSchema {
140+ export function buildASTSchema ( ast : DocumentNode , source ?: Source ) : GraphQLSchema {
139141 if ( ! ast || ast . kind !== DOCUMENT ) {
140142 throw new Error ( 'Must provide a document ast.' ) ;
141143 }
@@ -300,25 +302,29 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
300302
301303 function produceInputType ( typeNode : TypeNode ) : GraphQLInputType {
302304 const type = produceType ( typeNode ) ;
303- invariant ( isInputType ( type ) , 'Expected Input type.' ) ;
305+ invariant ( isInputType ( type ) ,
306+ invariantError ( 'Expected Input type' , typeNode , source ) ) ;
304307 return ( type : any ) ;
305308 }
306309
307310 function produceOutputType ( typeNode : TypeNode ) : GraphQLOutputType {
308311 const type = produceType ( typeNode ) ;
309- invariant ( isOutputType ( type ) , 'Expected Output type.' ) ;
312+ invariant ( isOutputType ( type ) ,
313+ invariantError ( 'Expected Output type' , typeNode , source ) ) ;
310314 return ( type : any ) ;
311315 }
312316
313317 function produceObjectType ( typeNode : TypeNode ) : GraphQLObjectType {
314318 const type = produceType ( typeNode ) ;
315- invariant ( type instanceof GraphQLObjectType , 'Expected Object type.' ) ;
319+ invariant ( type instanceof GraphQLObjectType ,
320+ invariantError ( 'Expected Object type' , typeNode , source ) ) ;
316321 return type ;
317322 }
318323
319324 function produceInterfaceType ( typeNode : TypeNode ) : GraphQLInterfaceType {
320325 const type = produceType ( typeNode ) ;
321- invariant ( type instanceof GraphQLInterfaceType , 'Expected Interface type.' ) ;
326+ invariant ( type instanceof GraphQLInterfaceType ,
327+ invariantError ( 'Expected Interface type' , typeNode , source ) ) ;
322328 return type ;
323329 }
324330
@@ -518,7 +524,8 @@ export function getDescription(node: { loc?: Location }): ?string {
518524 * document.
519525 */
520526export function buildSchema ( source : string | Source ) : GraphQLSchema {
521- return buildASTSchema ( parse ( source ) ) ;
527+ const sourceObj = typeof source === 'string' ? new Source ( source ) : source ;
528+ return buildASTSchema ( parse ( sourceObj ) , sourceObj ) ;
522529}
523530
524531// Count the number of spaces on the starting side of a string.
0 commit comments