1
1
import { Graph , Id } from '@graphprotocol/grc-20' ;
2
- import { Entity , type Mapping , Type } from '@graphprotocol/hypergraph' ;
2
+ import { type Entity , EntitySchema , type Mapping , Type } from '@graphprotocol/hypergraph' ;
3
+ import type * as Schema from 'effect/Schema' ;
3
4
import { describe , expect , it } from 'vitest' ;
4
5
import { translateFilterToGraphql } from '../../src/internal/translate-filter-to-graphql.js' ;
5
6
6
- export class Todo extends Entity . Class < Todo > ( 'Todo' ) ( {
7
- name : Type . String ,
8
- completed : Type . Boolean ,
9
- priority : Type . Number ,
10
- } ) { }
7
+ export const Todo = EntitySchema (
8
+ {
9
+ name : Type . String ,
10
+ completed : Type . Boolean ,
11
+ priority : Type . Number ,
12
+ } ,
13
+ {
14
+ types : [ Id ( 'a288444f-06a3-4037-9ace-66fe325864d0' ) ] ,
15
+ properties : {
16
+ name : Id ( 'a126ca53-0c8e-48d5-b888-82c734c38935' ) ,
17
+ completed : Id ( 'd2d64cd3-a337-4784-9e30-25bea0349471' ) ,
18
+ priority : Id ( 'ee920534-42ce-4113-a63b-8f3c889dd772' ) ,
19
+ } ,
20
+ } ,
21
+ ) ;
11
22
12
23
const mapping : Mapping . Mapping = {
13
24
Todo : {
@@ -20,13 +31,15 @@ const mapping: Mapping.Mapping = {
20
31
} ,
21
32
} ;
22
33
34
+ type TodoFilter = Entity . EntityFilter < Schema . Schema . Type < typeof Todo > > ;
35
+
23
36
describe ( 'translateFilterToGraphql string filters' , ( ) => {
24
37
it ( 'should translate string `is` filter correctly' , ( ) => {
25
- const filter : Entity . EntityFilter < Todo > = {
38
+ const filter : TodoFilter = {
26
39
name : { is : 'test' } ,
27
40
} ;
28
41
29
- const result = translateFilterToGraphql ( filter , Todo , mapping ) ;
42
+ const result = translateFilterToGraphql ( filter , Todo ) ;
30
43
31
44
expect ( result ) . toEqual ( {
32
45
values : {
@@ -39,11 +52,11 @@ describe('translateFilterToGraphql string filters', () => {
39
52
} ) ;
40
53
41
54
it ( 'should translate string `contains` filter correctly' , ( ) => {
42
- const filter : Entity . EntityFilter < Todo > = {
55
+ const filter : TodoFilter = {
43
56
name : { contains : 'test' } ,
44
57
} ;
45
58
46
- const result = translateFilterToGraphql ( filter , Todo , mapping ) ;
59
+ const result = translateFilterToGraphql ( filter , Todo ) ;
47
60
48
61
expect ( result ) . toEqual ( {
49
62
values : {
@@ -56,11 +69,11 @@ describe('translateFilterToGraphql string filters', () => {
56
69
} ) ;
57
70
58
71
it ( 'should translate string `startsWith` filter correctly' , ( ) => {
59
- const filter : Entity . EntityFilter < Todo > = {
72
+ const filter : TodoFilter = {
60
73
name : { startsWith : 'test' } ,
61
74
} ;
62
75
63
- const result = translateFilterToGraphql ( filter , Todo , mapping ) ;
76
+ const result = translateFilterToGraphql ( filter , Todo ) ;
64
77
65
78
expect ( result ) . toEqual ( {
66
79
values : {
@@ -73,11 +86,11 @@ describe('translateFilterToGraphql string filters', () => {
73
86
} ) ;
74
87
75
88
it ( 'should translate string `endsWith` filter correctly' , ( ) => {
76
- const filter : Entity . EntityFilter < Todo > = {
89
+ const filter : TodoFilter = {
77
90
name : { endsWith : 'test' } ,
78
91
} ;
79
92
80
- const result = translateFilterToGraphql ( filter , Todo , mapping ) ;
93
+ const result = translateFilterToGraphql ( filter , Todo ) ;
81
94
82
95
expect ( result ) . toEqual ( {
83
96
values : {
@@ -92,11 +105,11 @@ describe('translateFilterToGraphql string filters', () => {
92
105
93
106
describe ( 'translateFilterToGraphql boolean filters' , ( ) => {
94
107
it ( 'should translate boolean `is` filter correctly' , ( ) => {
95
- const filter : Entity . EntityFilter < Todo > = {
108
+ const filter : TodoFilter = {
96
109
completed : { is : true } ,
97
110
} ;
98
111
99
- const result = translateFilterToGraphql ( filter , Todo , mapping ) ;
112
+ const result = translateFilterToGraphql ( filter , Todo ) ;
100
113
101
114
expect ( result ) . toEqual ( {
102
115
values : {
@@ -111,11 +124,11 @@ describe('translateFilterToGraphql boolean filters', () => {
111
124
112
125
describe ( 'translateFilterToGraphql number filters' , ( ) => {
113
126
it ( 'should translate number `is` filter correctly' , ( ) => {
114
- const filter : Entity . EntityFilter < Todo > = {
127
+ const filter : TodoFilter = {
115
128
priority : { is : 1 } ,
116
129
} ;
117
130
118
- const result = translateFilterToGraphql ( filter , Todo , mapping ) ;
131
+ const result = translateFilterToGraphql ( filter , Todo ) ;
119
132
120
133
expect ( result ) . toEqual ( {
121
134
values : {
@@ -128,11 +141,11 @@ describe('translateFilterToGraphql number filters', () => {
128
141
} ) ;
129
142
130
143
it ( 'should translate number `greaterThan` filter correctly' , ( ) => {
131
- const filter : Entity . EntityFilter < Todo > = {
144
+ const filter : TodoFilter = {
132
145
priority : { greaterThan : 1 } ,
133
146
} ;
134
147
135
- const result = translateFilterToGraphql ( filter , Todo , mapping ) ;
148
+ const result = translateFilterToGraphql ( filter , Todo ) ;
136
149
137
150
expect ( result ) . toEqual ( {
138
151
values : {
@@ -147,13 +160,13 @@ describe('translateFilterToGraphql number filters', () => {
147
160
148
161
describe ( 'translateFilterToGraphql multiple filters' , ( ) => {
149
162
it ( 'should translate multiple filters correctly' , ( ) => {
150
- const filter : Entity . EntityFilter < Todo > = {
163
+ const filter : TodoFilter = {
151
164
name : { is : 'test' } ,
152
165
completed : { is : true } ,
153
166
priority : { greaterThan : 1 } ,
154
167
} ;
155
168
156
- const result = translateFilterToGraphql ( filter , Todo , mapping ) ;
169
+ const result = translateFilterToGraphql ( filter , Todo ) ;
157
170
158
171
expect ( result ) . toEqual ( {
159
172
and : [
@@ -188,11 +201,11 @@ describe('translateFilterToGraphql multiple filters', () => {
188
201
189
202
describe ( 'translateFilterToGraphql with OR operator' , ( ) => {
190
203
it ( 'should translate OR operator in nested filter array' , ( ) => {
191
- const filter : Entity . EntityFilter < Todo > = {
204
+ const filter : TodoFilter = {
192
205
or : [ { name : { is : 'test' } } , { name : { is : 'test2' } } ] ,
193
206
} ;
194
207
195
- const result = translateFilterToGraphql ( filter , Todo , mapping ) ;
208
+ const result = translateFilterToGraphql ( filter , Todo ) ;
196
209
197
210
expect ( result ) . toEqual ( {
198
211
or : [
@@ -207,11 +220,11 @@ describe('translateFilterToGraphql with OR operator', () => {
207
220
} ) ;
208
221
209
222
it ( 'should translate OR operator in nested filter array' , ( ) => {
210
- const filter : Entity . EntityFilter < Todo > = {
223
+ const filter : TodoFilter = {
211
224
or : [ { name : { is : 'test' } } , { completed : { is : true } } ] ,
212
225
} ;
213
226
214
- const result = translateFilterToGraphql ( filter , Todo , mapping ) ;
227
+ const result = translateFilterToGraphql ( filter , Todo ) ;
215
228
216
229
expect ( result ) . toEqual ( {
217
230
or : [
@@ -228,23 +241,23 @@ describe('translateFilterToGraphql with OR operator', () => {
228
241
229
242
describe ( 'translateFilterToGraphql with NOT operator' , ( ) => {
230
243
it ( 'should translate NOT operator' , ( ) => {
231
- const filter : Entity . EntityFilter < Todo > = {
244
+ const filter : TodoFilter = {
232
245
not : { name : { is : 'test' } } ,
233
246
} ;
234
247
235
- const result = translateFilterToGraphql ( filter , Todo , mapping ) ;
248
+ const result = translateFilterToGraphql ( filter , Todo ) ;
236
249
237
250
expect ( result ) . toEqual ( {
238
251
not : { values : { some : { propertyId : { is : 'a126ca53-0c8e-48d5-b888-82c734c38935' } , string : { is : 'test' } } } } ,
239
252
} ) ;
240
253
} ) ;
241
254
242
255
it ( 'should translate NOT operator with multiple filters' , ( ) => {
243
- const filter : Entity . EntityFilter < Todo > = {
256
+ const filter : TodoFilter = {
244
257
not : { name : { is : 'test' } , completed : { is : true } } ,
245
258
} ;
246
259
247
- const result = translateFilterToGraphql ( filter , Todo , mapping ) ;
260
+ const result = translateFilterToGraphql ( filter , Todo ) ;
248
261
249
262
expect ( result ) . toEqual ( {
250
263
not : {
@@ -259,11 +272,11 @@ describe('translateFilterToGraphql with NOT operator', () => {
259
272
260
273
describe ( 'translateFilterToGraphql with complex nested filters' , ( ) => {
261
274
it ( 'should translate complex nested filters with or and not' , ( ) => {
262
- const filter : Entity . EntityFilter < Todo > = {
275
+ const filter : TodoFilter = {
263
276
or : [ { not : { name : { is : 'Jane Doe' } } } , { not : { name : { is : 'John Doe' } } } ] ,
264
277
} ;
265
278
266
- const result = translateFilterToGraphql ( filter , Todo , mapping ) ;
279
+ const result = translateFilterToGraphql ( filter , Todo ) ;
267
280
268
281
expect ( result ) . toEqual ( {
269
282
or : [
@@ -286,7 +299,7 @@ describe('translateFilterToGraphql with complex nested filters', () => {
286
299
} ) ;
287
300
288
301
it ( 'should translate complex nested filters with and, or and not' , ( ) => {
289
- const filter : Entity . EntityFilter < Todo > = {
302
+ const filter : TodoFilter = {
290
303
priority : {
291
304
is : 42 ,
292
305
} ,
@@ -296,7 +309,7 @@ describe('translateFilterToGraphql with complex nested filters', () => {
296
309
} ,
297
310
} ;
298
311
299
- const result = translateFilterToGraphql ( filter , Todo , mapping ) ;
312
+ const result = translateFilterToGraphql ( filter , Todo ) ;
300
313
301
314
expect ( result ) . toEqual ( {
302
315
and : [
0 commit comments