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