@@ -14,8 +14,9 @@ import keyValMap from '../jsutils/keyValMap';
14
14
import { valueFromAST } from './valueFromAST' ;
15
15
import { TokenKind } from '../language/lexer' ;
16
16
import { parse } from '../language/parser' ;
17
- import type { Source } from '../language/source' ;
17
+ import { Source } from '../language/source' ;
18
18
import { getArgumentValues } from '../execution/values' ;
19
+ import { invariantError } from '../error/syntaxError' ;
19
20
20
21
import {
21
22
LIST_TYPE ,
@@ -135,7 +136,10 @@ function getNamedTypeNode(typeNode: TypeNode): NamedTypeNode {
135
136
* Given that AST it constructs a GraphQLSchema. The resulting schema
136
137
* has no resolve methods, so execution will use default resolvers.
137
138
*/
138
- export function buildASTSchema ( ast : DocumentNode ) : GraphQLSchema {
139
+ export function buildASTSchema (
140
+ ast : DocumentNode ,
141
+ source ?: Source
142
+ ) : GraphQLSchema {
139
143
if ( ! ast || ast . kind !== DOCUMENT ) {
140
144
throw new Error ( 'Must provide a document ast.' ) ;
141
145
}
@@ -300,25 +304,29 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
300
304
301
305
function produceInputType ( typeNode : TypeNode ) : GraphQLInputType {
302
306
const type = produceType ( typeNode ) ;
303
- invariant ( isInputType ( type ) , 'Expected Input type.' ) ;
307
+ invariant ( isInputType ( type ) ,
308
+ invariantError ( 'Expected Input type' , typeNode , source ) ) ;
304
309
return ( type : any ) ;
305
310
}
306
311
307
312
function produceOutputType ( typeNode : TypeNode ) : GraphQLOutputType {
308
313
const type = produceType ( typeNode ) ;
309
- invariant ( isOutputType ( type ) , 'Expected Output type.' ) ;
314
+ invariant ( isOutputType ( type ) ,
315
+ invariantError ( 'Expected Output type' , typeNode , source ) ) ;
310
316
return ( type : any ) ;
311
317
}
312
318
313
319
function produceObjectType ( typeNode : TypeNode ) : GraphQLObjectType {
314
320
const type = produceType ( typeNode ) ;
315
- invariant ( type instanceof GraphQLObjectType , 'Expected Object type.' ) ;
321
+ invariant ( type instanceof GraphQLObjectType ,
322
+ invariantError ( 'Expected Object type' , typeNode , source ) ) ;
316
323
return type ;
317
324
}
318
325
319
326
function produceInterfaceType ( typeNode : TypeNode ) : GraphQLInterfaceType {
320
327
const type = produceType ( typeNode ) ;
321
- invariant ( type instanceof GraphQLInterfaceType , 'Expected Interface type.' ) ;
328
+ invariant ( type instanceof GraphQLInterfaceType ,
329
+ invariantError ( 'Expected Interface type' , typeNode , source ) ) ;
322
330
return type ;
323
331
}
324
332
@@ -518,7 +526,8 @@ export function getDescription(node: { loc?: Location }): ?string {
518
526
* document.
519
527
*/
520
528
export function buildSchema ( source : string | Source ) : GraphQLSchema {
521
- return buildASTSchema ( parse ( source ) ) ;
529
+ const sourceObj = typeof source === 'string' ? new Source ( source ) : source ;
530
+ return buildASTSchema ( parse ( sourceObj ) , sourceObj ) ;
522
531
}
523
532
524
533
// Count the number of spaces on the starting side of a string.
0 commit comments