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