@@ -17,8 +17,11 @@ import {
17
17
GraphQLNonNull
18
18
} from './definition' ;
19
19
import type { GraphQLType } from './definition' ;
20
- import { GraphQLIncludeDirective , GraphQLSkipDirective } from './directives' ;
21
- import type { GraphQLDirective } from './directives' ;
20
+ import {
21
+ GraphQLDirective ,
22
+ GraphQLIncludeDirective ,
23
+ GraphQLSkipDirective
24
+ } from './directives' ;
22
25
import { __Schema } from './introspection' ;
23
26
import find from '../jsutils/find' ;
24
27
import invariant from '../jsutils/invariant' ;
@@ -40,30 +43,51 @@ import invariant from '../jsutils/invariant';
40
43
*
41
44
*/
42
45
export class GraphQLSchema {
43
- _schemaConfig : GraphQLSchemaConfig ;
44
- _typeMap: TypeMap ;
46
+ _queryType : GraphQLObjectType ;
47
+ _mutationType: ?GraphQLObjectType ;
48
+ _subscriptionType: ?GraphQLObjectType ;
45
49
_directives: Array < GraphQLDirective > ;
50
+ _typeMap: TypeMap ;
46
51
47
52
constructor ( config : GraphQLSchemaConfig ) {
48
53
invariant (
49
54
typeof config === 'object' ,
50
55
'Must provide configuration object.'
51
56
) ;
57
+
52
58
invariant (
53
59
config . query instanceof GraphQLObjectType ,
54
60
`Schema query must be Object Type but got: ${ config . query } .`
55
61
) ;
62
+ this . _queryType = config . query ;
63
+
56
64
invariant (
57
65
! config . mutation || config . mutation instanceof GraphQLObjectType ,
58
66
`Schema mutation must be Object Type if provided but ` +
59
67
`got: ${ config . mutation } .`
60
68
) ;
69
+ this . _mutationType = config . mutation ;
70
+
61
71
invariant (
62
72
! config . subscription || config . subscription instanceof GraphQLObjectType ,
63
73
`Schema subscription must be Object Type if provided but ` +
64
74
`got: ${ config . subscription } .`
65
75
) ;
66
- this . _schemaConfig = config ;
76
+ this . _subscriptionType = config . subscription ;
77
+
78
+ invariant (
79
+ ! config . directives ||
80
+ Array . isArray ( config . directives ) && config . directives . every (
81
+ directive => directive instanceof GraphQLDirective
82
+ ) ,
83
+ `Schema directives must be Array<GraphQLDirective> if provided but ` +
84
+ `got: ${ config . directives } .`
85
+ ) ;
86
+ // Provide `@include() and `@skip()` directives by default.
87
+ this . _directives = config . directives || [
88
+ GraphQLIncludeDirective ,
89
+ GraphQLSkipDirective
90
+ ] ;
67
91
68
92
// Build type map now to detect any errors within this schema.
69
93
this . _typeMap = [
@@ -85,15 +109,15 @@ export class GraphQLSchema {
85
109
}
86
110
87
111
getQueryType ( ) : GraphQLObjectType {
88
- return this . _schemaConfig . query ;
112
+ return this . _queryType ;
89
113
}
90
114
91
115
getMutationType ( ) : ?GraphQLObjectType {
92
- return this . _schemaConfig . mutation ;
116
+ return this . _mutationType ;
93
117
}
94
118
95
119
getSubscriptionType ( ) : ?GraphQLObjectType {
96
- return this . _schemaConfig . subscription ;
120
+ return this . _subscriptionType ;
97
121
}
98
122
99
123
getTypeMap ( ) : TypeMap {
@@ -105,10 +129,7 @@ export class GraphQLSchema {
105
129
}
106
130
107
131
getDirectives ( ) : Array < GraphQLDirective > {
108
- return this . _directives || ( this . _directives = [
109
- GraphQLIncludeDirective ,
110
- GraphQLSkipDirective
111
- ] ) ;
132
+ return this . _directives ;
112
133
}
113
134
114
135
getDirective ( name : string ) : ?GraphQLDirective {
@@ -122,6 +143,7 @@ type GraphQLSchemaConfig = {
122
143
query : GraphQLObjectType ;
123
144
mutation ?: ?GraphQLObjectType ;
124
145
subscription ?: ?GraphQLObjectType ;
146
+ directives ?: ?Array < GraphQLDirective > ;
125
147
}
126
148
127
149
function typeMapReducer ( map : TypeMap , type : ?GraphQLType ) : TypeMap {
0 commit comments