@@ -6,205 +6,215 @@ import { GraphQLError } from '../../error/GraphQLError';
6
6
import type { ExecutableDefinitionNode , FieldNode } from '../../language/ast' ;
7
7
import { parse } from '../../language/parser' ;
8
8
9
- import { GraphQLNonNull , GraphQLObjectType , GraphQLSemanticNonNull , GraphQLSemanticNullable } from '../../type/definition' ;
9
+ import {
10
+ GraphQLNonNull ,
11
+ GraphQLObjectType ,
12
+ GraphQLSemanticNonNull ,
13
+ GraphQLSemanticNullable ,
14
+ } from '../../type/definition' ;
10
15
import { GraphQLString } from '../../type/scalars' ;
11
16
import { GraphQLSchema } from '../../type/schema' ;
12
17
13
18
import { execute } from '../execute' ;
14
19
15
20
describe ( 'Execute: Handles Semantic Nullability' , ( ) => {
16
- const DeepDataType = new GraphQLObjectType ( {
17
- name : 'DeepDataType' ,
18
- fields : {
19
- f : { type : new GraphQLNonNull ( GraphQLString ) }
20
- } ,
21
- } ) ;
22
-
23
- const DataType : GraphQLObjectType = new GraphQLObjectType ( {
24
- name : 'DataType' ,
25
- fields : ( ) => ( {
26
- a : { type : new GraphQLSemanticNullable ( GraphQLString ) } ,
27
- b : { type : new GraphQLSemanticNonNull ( GraphQLString ) } ,
28
- c : { type : new GraphQLNonNull ( GraphQLString ) } ,
29
- d : { type : new GraphQLSemanticNonNull ( DeepDataType ) }
30
- } ) ,
31
- } ) ;
32
-
33
- it ( 'SemanticNonNull throws error on null without error' , async ( ) => {
34
- const data = {
35
- a : ( ) => 'Apple' ,
36
- b : ( ) => null ,
37
- c : ( ) => 'Cookie'
38
- } ;
39
-
40
- const document = parse ( `
21
+ const DeepDataType = new GraphQLObjectType ( {
22
+ name : 'DeepDataType' ,
23
+ fields : {
24
+ f : { type : new GraphQLNonNull ( GraphQLString ) } ,
25
+ } ,
26
+ } ) ;
27
+
28
+ const DataType : GraphQLObjectType = new GraphQLObjectType ( {
29
+ name : 'DataType' ,
30
+ fields : ( ) => ( {
31
+ a : { type : new GraphQLSemanticNullable ( GraphQLString ) } ,
32
+ b : { type : new GraphQLSemanticNonNull ( GraphQLString ) } ,
33
+ c : { type : new GraphQLNonNull ( GraphQLString ) } ,
34
+ d : { type : new GraphQLSemanticNonNull ( DeepDataType ) } ,
35
+ } ) ,
36
+ } ) ;
37
+
38
+ it ( 'SemanticNonNull throws error on null without error' , async ( ) => {
39
+ const data = {
40
+ a : ( ) => 'Apple' ,
41
+ b : ( ) => null ,
42
+ c : ( ) => 'Cookie' ,
43
+ } ;
44
+
45
+ const document = parse ( `
41
46
query {
42
47
b
43
48
}
44
49
` ) ;
45
-
46
- const result = await execute ( {
47
- schema : new GraphQLSchema ( { query : DataType } ) ,
48
- document,
49
- rootValue : data ,
50
- } ) ;
51
-
52
- const executable = document . definitions ?. values ( ) . next ( ) . value as ExecutableDefinitionNode ;
53
- const selectionSet = executable . selectionSet . selections . values ( ) . next ( ) . value ;
54
-
55
- expect ( result ) . to . deep . equal ( {
56
- data : {
57
- b : null
58
- } ,
59
- errors : [
60
- new GraphQLError (
61
- 'Cannot return null for semantic-non-nullable field DataType.b.' ,
62
- {
63
- nodes : selectionSet ,
64
- path : [ 'b' ]
65
- }
66
- )
67
- ]
68
- } ) ;
50
+
51
+ const result = await execute ( {
52
+ schema : new GraphQLSchema ( { query : DataType } ) ,
53
+ document,
54
+ rootValue : data ,
55
+ } ) ;
56
+
57
+ const executable = document . definitions ?. values ( ) . next ( )
58
+ . value as ExecutableDefinitionNode ;
59
+ const selectionSet = executable . selectionSet . selections
60
+ . values ( )
61
+ . next ( ) . value ;
62
+
63
+ expect ( result ) . to . deep . equal ( {
64
+ data : {
65
+ b : null ,
66
+ } ,
67
+ errors : [
68
+ new GraphQLError (
69
+ 'Cannot return null for semantic-non-nullable field DataType.b.' ,
70
+ {
71
+ nodes : selectionSet ,
72
+ path : [ 'b' ] ,
73
+ } ,
74
+ ) ,
75
+ ] ,
69
76
} ) ;
70
-
71
- it ( 'SemanticNonNull succeeds on null with error' , async ( ) => {
72
- const data = {
73
- a : ( ) => 'Apple' ,
74
- b : ( ) => { throw new Error (
75
- 'Something went wrong' ,
76
- ) ; } ,
77
- c : ( ) => 'Cookie'
78
- } ;
79
-
80
- const document = parse ( `
77
+ } ) ;
78
+
79
+ it ( 'SemanticNonNull succeeds on null with error' , async ( ) => {
80
+ const data = {
81
+ a : ( ) => 'Apple' ,
82
+ b : ( ) => {
83
+ throw new Error ( 'Something went wrong' ) ;
84
+ } ,
85
+ c : ( ) => 'Cookie' ,
86
+ } ;
87
+
88
+ const document = parse ( `
81
89
query {
82
90
b
83
91
}
84
92
` ) ;
85
-
86
- const executable = document . definitions ?. values ( ) . next ( ) . value as ExecutableDefinitionNode ;
87
- const selectionSet = executable . selectionSet . selections . values ( ) . next ( ) . value ;
88
-
89
- const result = await execute ( {
90
- schema : new GraphQLSchema ( { query : DataType } ) ,
91
- document,
92
- rootValue : data ,
93
- } ) ;
94
-
95
- expect ( result ) . to . deep . equal ( {
96
- data : {
97
- b : null
98
- } ,
99
- errors : [
100
- new GraphQLError (
101
- 'Something went wrong' ,
102
- {
103
- nodes : selectionSet ,
104
- path : [ 'b' ]
105
- }
106
- )
107
- ]
108
- } ) ;
93
+
94
+ const executable = document . definitions ?. values ( ) . next ( )
95
+ . value as ExecutableDefinitionNode ;
96
+ const selectionSet = executable . selectionSet . selections
97
+ . values ( )
98
+ . next ( ) . value ;
99
+
100
+ const result = await execute ( {
101
+ schema : new GraphQLSchema ( { query : DataType } ) ,
102
+ document,
103
+ rootValue : data ,
104
+ } ) ;
105
+
106
+ expect ( result ) . to . deep . equal ( {
107
+ data : {
108
+ b : null ,
109
+ } ,
110
+ errors : [
111
+ new GraphQLError ( 'Something went wrong' , {
112
+ nodes : selectionSet ,
113
+ path : [ 'b' ] ,
114
+ } ) ,
115
+ ] ,
109
116
} ) ;
110
-
111
- it ( 'SemanticNonNull halts null propagation' , async ( ) => {
112
- const deepData = {
113
- f : ( ) => null
114
- } ;
115
-
116
- const data = {
117
- a : ( ) => 'Apple' ,
118
- b : ( ) => null ,
119
- c : ( ) => 'Cookie' ,
120
- d : ( ) => deepData
121
- } ;
122
-
123
-
124
- const document = parse ( `
117
+ } ) ;
118
+
119
+ it ( 'SemanticNonNull halts null propagation' , async ( ) => {
120
+ const deepData = {
121
+ f : ( ) => null ,
122
+ } ;
123
+
124
+ const data = {
125
+ a : ( ) => 'Apple' ,
126
+ b : ( ) => null ,
127
+ c : ( ) => 'Cookie' ,
128
+ d : ( ) => deepData ,
129
+ } ;
130
+
131
+ const document = parse ( `
125
132
query {
126
133
d {
127
134
f
128
135
}
129
136
}
130
137
` ) ;
131
-
132
- const result = await execute ( {
133
- schema : new GraphQLSchema ( { query : DataType } ) ,
134
- document,
135
- rootValue : data ,
136
- } ) ;
137
-
138
- const executable = document . definitions ?. values ( ) . next ( ) . value as ExecutableDefinitionNode ;
139
- const dSelectionSet = executable . selectionSet . selections . values ( ) . next ( ) . value as FieldNode ;
140
- const fSelectionSet = dSelectionSet . selectionSet ?. selections . values ( ) . next ( ) . value ;
141
-
142
- expect ( result ) . to . deep . equal ( {
143
- data : {
144
- d : null
145
- } ,
146
- errors : [
147
- new GraphQLError (
148
- 'Cannot return null for non-nullable field DeepDataType.f.' ,
149
- {
150
- nodes : fSelectionSet ,
151
- path : [ 'd' , 'f' ]
152
- }
153
- )
154
- ]
155
- } ) ;
138
+
139
+ const result = await execute ( {
140
+ schema : new GraphQLSchema ( { query : DataType } ) ,
141
+ document,
142
+ rootValue : data ,
143
+ } ) ;
144
+
145
+ const executable = document . definitions ?. values ( ) . next ( )
146
+ . value as ExecutableDefinitionNode ;
147
+ const dSelectionSet = executable . selectionSet . selections . values ( ) . next ( )
148
+ . value as FieldNode ;
149
+ const fSelectionSet = dSelectionSet . selectionSet ?. selections
150
+ . values ( )
151
+ . next ( ) . value ;
152
+
153
+ expect ( result ) . to . deep . equal ( {
154
+ data : {
155
+ d : null ,
156
+ } ,
157
+ errors : [
158
+ new GraphQLError (
159
+ 'Cannot return null for non-nullable field DeepDataType.f.' ,
160
+ {
161
+ nodes : fSelectionSet ,
162
+ path : [ 'd' , 'f' ] ,
163
+ } ,
164
+ ) ,
165
+ ] ,
156
166
} ) ;
157
-
158
- it ( 'SemanticNullable allows null values' , async ( ) => {
159
- const data = {
160
- a : ( ) => null ,
161
- b : ( ) => null ,
162
- c : ( ) => 'Cookie'
163
- } ;
164
-
165
- const document = parse ( `
167
+ } ) ;
168
+
169
+ it ( 'SemanticNullable allows null values' , async ( ) => {
170
+ const data = {
171
+ a : ( ) => null ,
172
+ b : ( ) => null ,
173
+ c : ( ) => 'Cookie' ,
174
+ } ;
175
+
176
+ const document = parse ( `
166
177
query {
167
178
a
168
179
}
169
180
` ) ;
170
-
171
- const result = await execute ( {
172
- schema : new GraphQLSchema ( { query : DataType } ) ,
173
- document,
174
- rootValue : data ,
175
- } ) ;
176
-
177
- expect ( result ) . to . deep . equal ( {
178
- data : {
179
- a : null
180
- }
181
- } ) ;
181
+
182
+ const result = await execute ( {
183
+ schema : new GraphQLSchema ( { query : DataType } ) ,
184
+ document,
185
+ rootValue : data ,
182
186
} ) ;
183
-
184
- it ( 'SemanticNullable allows non-null values' , async ( ) => {
185
- const data = {
186
- a : ( ) => 'Apple' ,
187
- b : ( ) => null ,
188
- c : ( ) => 'Cookie'
189
- } ;
190
-
191
- const document = parse ( `
187
+
188
+ expect ( result ) . to . deep . equal ( {
189
+ data : {
190
+ a : null ,
191
+ } ,
192
+ } ) ;
193
+ } ) ;
194
+
195
+ it ( 'SemanticNullable allows non-null values' , async ( ) => {
196
+ const data = {
197
+ a : ( ) => 'Apple' ,
198
+ b : ( ) => null ,
199
+ c : ( ) => 'Cookie' ,
200
+ } ;
201
+
202
+ const document = parse ( `
192
203
query {
193
204
a
194
205
}
195
206
` ) ;
196
-
197
- const result = await execute ( {
198
- schema : new GraphQLSchema ( { query : DataType } ) ,
199
- document,
200
- rootValue : data ,
201
- } ) ;
202
-
203
- expect ( result ) . to . deep . equal ( {
204
- data : {
205
- a : 'Apple'
206
- }
207
- } ) ;
207
+
208
+ const result = await execute ( {
209
+ schema : new GraphQLSchema ( { query : DataType } ) ,
210
+ document,
211
+ rootValue : data ,
212
+ } ) ;
213
+
214
+ expect ( result ) . to . deep . equal ( {
215
+ data : {
216
+ a : 'Apple' ,
217
+ } ,
208
218
} ) ;
209
219
} ) ;
210
-
220
+ } ) ;
0 commit comments