@@ -45,25 +45,25 @@ import {
45
45
GraphQLScalarType ,
46
46
ScalarTypeDefinitionNode ,
47
47
StringValueNode ,
48
+ DefinitionNode ,
49
+ DocumentNode ,
48
50
} from 'graphql' ;
49
- import { PrintSchemaWithDirectivesOptions } from './types' ;
51
+ import { GetDocumentNodeFromSchemaOptions , PrintSchemaWithDirectivesOptions } from './types' ;
50
52
51
53
import { astFromType } from './astFromType' ;
52
54
import { getDirectivesInExtensions } from './get-directives' ;
53
55
import { astFromValueUntyped } from './astFromValueUntyped' ;
54
56
55
- // this approach uses the default schema printer rather than a custom solution, so may be more backwards compatible
56
- // currently does not allow customization of printSchema options having to do with comments.
57
- export function printSchemaWithDirectives (
57
+ export function getDocumentNodeFromSchema (
58
58
schema : GraphQLSchema ,
59
- options : PrintSchemaWithDirectivesOptions = { }
60
- ) : string {
59
+ options : GetDocumentNodeFromSchemaOptions = { }
60
+ ) : DocumentNode {
61
61
const pathToDirectivesInExtensions = options . pathToDirectivesInExtensions ;
62
62
63
63
const typesMap = schema . getTypeMap ( ) ;
64
64
65
65
const schemaNode = astFromSchema ( schema , pathToDirectivesInExtensions ) ;
66
- const result : Array < string > = schemaNode != null ? [ print ( schemaNode ) ] : [ ] ;
66
+ const definitions : Array < DefinitionNode > = schemaNode != null ? [ schemaNode ] : [ ] ;
67
67
68
68
for ( const typeName in typesMap ) {
69
69
const type = typesMap [ typeName ] ;
@@ -75,17 +75,17 @@ export function printSchemaWithDirectives(
75
75
}
76
76
77
77
if ( isObjectType ( type ) ) {
78
- result . push ( print ( astFromObjectType ( type , schema , pathToDirectivesInExtensions ) ) ) ;
78
+ definitions . push ( astFromObjectType ( type , schema , pathToDirectivesInExtensions ) ) ;
79
79
} else if ( isInterfaceType ( type ) ) {
80
- result . push ( print ( astFromInterfaceType ( type , schema , pathToDirectivesInExtensions ) ) ) ;
80
+ definitions . push ( astFromInterfaceType ( type , schema , pathToDirectivesInExtensions ) ) ;
81
81
} else if ( isUnionType ( type ) ) {
82
- result . push ( print ( astFromUnionType ( type , schema , pathToDirectivesInExtensions ) ) ) ;
82
+ definitions . push ( astFromUnionType ( type , schema , pathToDirectivesInExtensions ) ) ;
83
83
} else if ( isInputObjectType ( type ) ) {
84
- result . push ( print ( astFromInputObjectType ( type , schema , pathToDirectivesInExtensions ) ) ) ;
84
+ definitions . push ( astFromInputObjectType ( type , schema , pathToDirectivesInExtensions ) ) ;
85
85
} else if ( isEnumType ( type ) ) {
86
- result . push ( print ( astFromEnumType ( type , schema , pathToDirectivesInExtensions ) ) ) ;
86
+ definitions . push ( astFromEnumType ( type , schema , pathToDirectivesInExtensions ) ) ;
87
87
} else if ( isScalarType ( type ) ) {
88
- result . push ( print ( astFromScalarType ( type , schema , pathToDirectivesInExtensions ) ) ) ;
88
+ definitions . push ( astFromScalarType ( type , schema , pathToDirectivesInExtensions ) ) ;
89
89
} else {
90
90
throw new Error ( `Unknown type ${ type } .` ) ;
91
91
}
@@ -97,10 +97,23 @@ export function printSchemaWithDirectives(
97
97
continue ;
98
98
}
99
99
100
- result . push ( print ( astFromDirective ( directive , schema , pathToDirectivesInExtensions ) ) ) ;
100
+ definitions . push ( astFromDirective ( directive , schema , pathToDirectivesInExtensions ) ) ;
101
101
}
102
102
103
- return result . join ( '\n' ) ;
103
+ return {
104
+ kind : Kind . DOCUMENT ,
105
+ definitions,
106
+ } ;
107
+ }
108
+
109
+ // this approach uses the default schema printer rather than a custom solution, so may be more backwards compatible
110
+ // currently does not allow customization of printSchema options having to do with comments.
111
+ export function printSchemaWithDirectives (
112
+ schema : GraphQLSchema ,
113
+ options : PrintSchemaWithDirectivesOptions = { }
114
+ ) : string {
115
+ const documentNode = getDocumentNodeFromSchema ( schema , options ) ;
116
+ return print ( documentNode ) ;
104
117
}
105
118
106
119
export function astFromSchema (
@@ -431,7 +444,7 @@ export function astFromEnumType(
431
444
} ;
432
445
}
433
446
434
- function astFromScalarType (
447
+ export function astFromScalarType (
435
448
type : GraphQLScalarType ,
436
449
schema : GraphQLSchema ,
437
450
pathToDirectivesInExtensions : Array < string >
@@ -454,7 +467,7 @@ function astFromScalarType(
454
467
} ;
455
468
}
456
469
457
- function astFromField (
470
+ export function astFromField (
458
471
field : GraphQLField < any , any > ,
459
472
schema : GraphQLSchema ,
460
473
pathToDirectivesInExtensions : Array < string >
@@ -479,7 +492,7 @@ function astFromField(
479
492
} ;
480
493
}
481
494
482
- function astFromInputField (
495
+ export function astFromInputField (
483
496
field : GraphQLInputField ,
484
497
schema : GraphQLSchema ,
485
498
pathToDirectivesInExtensions : Array < string >
@@ -500,10 +513,11 @@ function astFromInputField(
500
513
} ,
501
514
type : astFromType ( field . type ) ,
502
515
directives : getDeprecatableDirectiveNodes ( field , schema , pathToDirectivesInExtensions ) ,
516
+ defaultValue : astFromValue ( field . defaultValue , field . type ) ,
503
517
} ;
504
518
}
505
519
506
- function astFromEnumValue (
520
+ export function astFromEnumValue (
507
521
value : GraphQLEnumValue ,
508
522
schema : GraphQLSchema ,
509
523
pathToDirectivesInExtensions : Array < string >
0 commit comments