1
1
import {
2
2
GraphQLID ,
3
+ GraphQLInputObjectType ,
3
4
GraphQLInt ,
4
5
GraphQLList ,
5
6
GraphQLNonNull ,
6
7
GraphQLObjectType ,
7
8
GraphQLString ,
8
9
} from 'graphql' ;
9
- import getSchemaFromData from './getSchemaFromData' ;
10
10
import type { ObjMap } from 'graphql/jsutils/ObjMap' ;
11
+ import { expect , test } from 'vitest' ;
12
+ import getSchemaFromData from './getSchemaFromData' ;
11
13
12
14
const data = {
13
15
posts : [
@@ -105,36 +107,38 @@ const QueryType = new GraphQLObjectType({
105
107
106
108
test ( 'creates one type per data type' , ( ) => {
107
109
const typeMap = getSchemaFromData (
108
- data ,
110
+ data
109
111
) . getTypeMap ( ) as ObjMap < GraphQLObjectType > ;
110
112
expect ( typeMap . Post . name ) . toEqual ( PostType . name ) ;
111
113
expect ( Object . keys ( typeMap . Post . getFields ( ) ) ) . toEqual (
112
- Object . keys ( PostType . getFields ( ) ) ,
114
+ Object . keys ( PostType . getFields ( ) )
113
115
) ;
114
116
expect ( typeMap . User . name ) . toEqual ( UserType . name ) ;
115
117
expect ( Object . keys ( typeMap . User . getFields ( ) ) ) . toEqual (
116
- Object . keys ( UserType . getFields ( ) ) ,
118
+ Object . keys ( UserType . getFields ( ) )
117
119
) ;
118
120
} ) ;
119
121
120
122
test ( 'creates one field per relationship' , ( ) => {
121
123
const typeMap = getSchemaFromData (
122
- data ,
124
+ data
123
125
) . getTypeMap ( ) as ObjMap < GraphQLObjectType > ;
124
126
expect ( Object . keys ( typeMap . Post . getFields ( ) ) ) . toContain ( 'User' ) ;
125
127
} ) ;
126
128
127
129
test ( 'creates one field per reverse relationship' , ( ) => {
128
130
const typeMap = getSchemaFromData (
129
- data ,
131
+ data
130
132
) . getTypeMap ( ) as ObjMap < GraphQLObjectType > ;
131
133
expect ( Object . keys ( typeMap . User . getFields ( ) ) ) . toContain ( 'Posts' ) ;
132
134
} ) ;
133
135
134
136
test ( 'creates three query fields per data type' , ( ) => {
135
137
// biome-ignore lint/style/noNonNullAssertion: It's only a test
136
138
const queries = getSchemaFromData ( data ) . getQueryType ( ) ! . getFields ( ) ;
137
- expect ( queries . Post . type . name ) . toEqual ( PostType . name ) ;
139
+ expect ( ( queries . Post . type as GraphQLObjectType ) . name ) . toEqual (
140
+ PostType . name
141
+ ) ;
138
142
expect ( queries . Post . args ) . toEqual ( [
139
143
expect . objectContaining ( {
140
144
name : 'id' ,
@@ -154,7 +158,9 @@ test('creates three query fields per data type', () => {
154
158
expect ( queries . allPosts . args [ 4 ] . type . toString ( ) ) . toEqual ( 'PostFilter' ) ;
155
159
expect ( queries . _allPostsMeta . type . toString ( ) ) . toEqual ( 'ListMetadata' ) ;
156
160
157
- expect ( queries . User . type . name ) . toEqual ( UserType . name ) ;
161
+ expect ( ( queries . User . type as GraphQLObjectType ) . name ) . toEqual (
162
+ UserType . name
163
+ ) ;
158
164
expect ( queries . User . args ) . toEqual ( [
159
165
expect . objectContaining ( {
160
166
name : 'id' ,
@@ -178,7 +184,9 @@ test('creates three query fields per data type', () => {
178
184
test ( 'creates three mutation fields per data type' , ( ) => {
179
185
// biome-ignore lint/style/noNonNullAssertion: It's only a test
180
186
const mutations = getSchemaFromData ( data ) . getMutationType ( ) ! . getFields ( ) ;
181
- expect ( mutations . createPost . type . name ) . toEqual ( PostType . name ) ;
187
+ expect ( ( mutations . createPost . type as GraphQLObjectType ) . name ) . toEqual (
188
+ PostType . name
189
+ ) ;
182
190
expect ( mutations . createPost . args ) . toEqual ( [
183
191
expect . objectContaining ( {
184
192
name : 'title' ,
@@ -193,7 +201,9 @@ test('creates three mutation fields per data type', () => {
193
201
type : new GraphQLNonNull ( GraphQLID ) ,
194
202
} ) ,
195
203
] ) ;
196
- expect ( mutations . updatePost . type . name ) . toEqual ( PostType . name ) ;
204
+ expect ( ( mutations . updatePost . type as GraphQLObjectType ) . name ) . toEqual (
205
+ PostType . name
206
+ ) ;
197
207
expect ( mutations . updatePost . args ) . toEqual ( [
198
208
expect . objectContaining ( {
199
209
name : 'id' ,
@@ -212,21 +222,27 @@ test('creates three mutation fields per data type', () => {
212
222
type : GraphQLID ,
213
223
} ) ,
214
224
] ) ;
215
- expect ( mutations . removePost . type . name ) . toEqual ( PostType . name ) ;
225
+ expect ( ( mutations . removePost . type as GraphQLObjectType ) . name ) . toEqual (
226
+ PostType . name
227
+ ) ;
216
228
expect ( mutations . removePost . args ) . toEqual ( [
217
229
expect . objectContaining ( {
218
230
name : 'id' ,
219
231
type : new GraphQLNonNull ( GraphQLID ) ,
220
232
} ) ,
221
233
] ) ;
222
- expect ( mutations . createUser . type . name ) . toEqual ( UserType . name ) ;
234
+ expect ( ( mutations . createUser . type as GraphQLObjectType ) . name ) . toEqual (
235
+ UserType . name
236
+ ) ;
223
237
expect ( mutations . createUser . args ) . toEqual ( [
224
238
expect . objectContaining ( {
225
239
name : 'name' ,
226
240
type : new GraphQLNonNull ( GraphQLString ) ,
227
241
} ) ,
228
242
] ) ;
229
- expect ( mutations . updateUser . type . name ) . toEqual ( UserType . name ) ;
243
+ expect ( ( mutations . updateUser . type as GraphQLObjectType ) . name ) . toEqual (
244
+ UserType . name
245
+ ) ;
230
246
expect ( mutations . updateUser . args ) . toEqual ( [
231
247
expect . objectContaining ( {
232
248
name : 'id' ,
@@ -237,7 +253,9 @@ test('creates three mutation fields per data type', () => {
237
253
type : GraphQLString ,
238
254
} ) ,
239
255
] ) ;
240
- expect ( mutations . removeUser . type . name ) . toEqual ( UserType . name ) ;
256
+ expect ( ( mutations . removeUser . type as GraphQLObjectType ) . name ) . toEqual (
257
+ UserType . name
258
+ ) ;
241
259
expect ( mutations . removeUser . args ) . toEqual ( [
242
260
expect . objectContaining ( {
243
261
name : 'id' ,
@@ -251,20 +269,22 @@ test('creates the mutation *Input type for createMany', () => {
251
269
const mutations = getSchemaFromData ( data ) . getMutationType ( ) ! . getFields ( ) ;
252
270
const createManyPostInputType = mutations . createManyPost . args [ 0 ] . type ;
253
271
expect ( createManyPostInputType . toString ( ) ) . toEqual ( '[PostInput]' ) ;
254
- expect ( createManyPostInputType . ofType . getFields ( ) ) . toEqual ( {
255
- title : expect . objectContaining ( {
256
- type : new GraphQLNonNull ( GraphQLString ) ,
257
- name : 'title' ,
258
- } ) ,
259
- views : expect . objectContaining ( {
260
- type : new GraphQLNonNull ( GraphQLInt ) ,
261
- name : 'views' ,
262
- } ) ,
263
- user_id : expect . objectContaining ( {
264
- type : new GraphQLNonNull ( GraphQLID ) ,
265
- name : 'user_id' ,
266
- } ) ,
267
- } ) ;
272
+ expect ( ( createManyPostInputType as GraphQLList < any > ) . ofType . getFields ( ) ) . toEqual (
273
+ {
274
+ title : expect . objectContaining ( {
275
+ type : new GraphQLNonNull ( GraphQLString ) ,
276
+ name : 'title' ,
277
+ } ) ,
278
+ views : expect . objectContaining ( {
279
+ type : new GraphQLNonNull ( GraphQLInt ) ,
280
+ name : 'views' ,
281
+ } ) ,
282
+ user_id : expect . objectContaining ( {
283
+ type : new GraphQLNonNull ( GraphQLID ) ,
284
+ name : 'user_id' ,
285
+ } ) ,
286
+ }
287
+ ) ;
268
288
} ) ;
269
289
270
290
test ( 'pluralizes and capitalizes correctly' , ( ) => {
0 commit comments