@@ -3,6 +3,15 @@ import { expectType } from "@saiichihashimoto/test-utils";
3
3
import { evaluate , parse } from "groq-js" ;
4
4
import type { WritableDeep } from "type-fest" ;
5
5
6
+ import {
7
+
8
+ defineArrayMember ,
9
+ defineConfig ,
10
+ defineField ,
11
+ defineType
12
+ } from "@sanity-typed/types" ;
13
+ import type { InferSchemaValues } from "@sanity-typed/types" ;
14
+
6
15
import type { ExecuteQuery , Parse } from "." ;
7
16
import type { ScopeFromPartialContext } from "./internal" ;
8
17
@@ -95,4 +104,197 @@ describe("specific issues", () => {
95
104
>
96
105
> ( ) . toStrictEqual < WritableDeep < typeof expectedResult > > ( ) ;
97
106
} ) ;
107
+
108
+ it ( "#641 FIXME" , async ( ) => {
109
+ // https://github.com/saiichihashimoto/sanity-typed/issues/641
110
+ const query = `
111
+ *[_type == "foo" && type == "links"]{
112
+ _id,
113
+ title,
114
+ links[]{
115
+ _key,
116
+ title,
117
+ url,
118
+ target
119
+ }
120
+ }
121
+ ` ;
122
+
123
+ const tree = parse ( query ) ;
124
+
125
+ const expectedTree = {
126
+ base : {
127
+ base : { type : "Everything" } ,
128
+ expr : {
129
+ left : {
130
+ left : { name : "_type" , type : "AccessAttribute" } ,
131
+ op : "==" ,
132
+ right : { type : "Value" , value : "foo" } ,
133
+ type : "OpCall" ,
134
+ } ,
135
+ right : {
136
+ left : { name : "type" , type : "AccessAttribute" } ,
137
+ op : "==" ,
138
+ right : { type : "Value" , value : "links" } ,
139
+ type : "OpCall" ,
140
+ } ,
141
+ type : "And" ,
142
+ } ,
143
+ type : "Filter" ,
144
+ } ,
145
+ expr : {
146
+ base : { type : "This" } ,
147
+ expr : {
148
+ attributes : [
149
+ {
150
+ name : "_id" ,
151
+ type : "ObjectAttributeValue" ,
152
+ value : { name : "_id" , type : "AccessAttribute" } ,
153
+ } ,
154
+ {
155
+ name : "title" ,
156
+ type : "ObjectAttributeValue" ,
157
+ value : { name : "title" , type : "AccessAttribute" } ,
158
+ } ,
159
+ {
160
+ name : "links" ,
161
+ type : "ObjectAttributeValue" ,
162
+ value : {
163
+ base : {
164
+ base : { name : "links" , type : "AccessAttribute" } ,
165
+ type : "ArrayCoerce" ,
166
+ } ,
167
+ expr : {
168
+ base : { type : "This" } ,
169
+ expr : {
170
+ attributes : [
171
+ {
172
+ name : "_key" ,
173
+ type : "ObjectAttributeValue" ,
174
+ value : { name : "_key" , type : "AccessAttribute" } ,
175
+ } ,
176
+ {
177
+ name : "title" ,
178
+ type : "ObjectAttributeValue" ,
179
+ value : { name : "title" , type : "AccessAttribute" } ,
180
+ } ,
181
+ {
182
+ name : "url" ,
183
+ type : "ObjectAttributeValue" ,
184
+ value : { name : "url" , type : "AccessAttribute" } ,
185
+ } ,
186
+ {
187
+ name : "target" ,
188
+ type : "ObjectAttributeValue" ,
189
+ value : { name : "target" , type : "AccessAttribute" } ,
190
+ } ,
191
+ ] ,
192
+ type : "Object" ,
193
+ } ,
194
+ type : "Projection" ,
195
+ } ,
196
+ type : "Map" ,
197
+ } ,
198
+ } ,
199
+ ] ,
200
+ type : "Object" ,
201
+ } ,
202
+ type : "Projection" ,
203
+ } ,
204
+ type : "Map" ,
205
+ } as const ;
206
+
207
+ expect ( tree ) . toStrictEqual ( expectedTree ) ;
208
+ expectType < Parse < typeof query > > ( ) . toStrictEqual <
209
+ WritableDeep < typeof expectedTree >
210
+ > ( ) ;
211
+
212
+ const config = defineConfig ( {
213
+ dataset : "dataset" ,
214
+ projectId : "projectId" ,
215
+ schema : {
216
+ types : [
217
+ defineType ( {
218
+ name : "foo" ,
219
+ type : "document" ,
220
+ fields : [
221
+ defineField ( {
222
+ name : "type" ,
223
+ type : "string" ,
224
+ options : {
225
+ list : [
226
+ { title : "About" , value : "about" } ,
227
+ { title : "Social" , value : "social" } ,
228
+ { title : "Links" , value : "links" } ,
229
+ ] ,
230
+ } ,
231
+ validation : ( Rule ) => Rule . required ( ) ,
232
+ } ) ,
233
+ defineField ( {
234
+ name : "title" ,
235
+ type : "string" ,
236
+ validation : ( Rule ) => Rule . required ( ) ,
237
+ } ) ,
238
+ defineField ( {
239
+ name : "links" ,
240
+ type : "array" ,
241
+ of : [
242
+ defineArrayMember ( {
243
+ type : "object" ,
244
+ fields : [
245
+ defineField ( {
246
+ name : "title" ,
247
+ type : "string" ,
248
+ } ) ,
249
+ defineField ( {
250
+ name : "url" ,
251
+ type : "string" ,
252
+ } ) ,
253
+ defineField ( {
254
+ name : "target" ,
255
+ type : "string" ,
256
+ options : {
257
+ list : [
258
+ { title : "Self" , value : "_self" } ,
259
+ { title : "Blank" , value : "_blank" } ,
260
+ ] ,
261
+ } ,
262
+ } ) ,
263
+ ] ,
264
+ } ) ,
265
+ ] ,
266
+ } ) ,
267
+ ] ,
268
+ } ) ,
269
+ ] ,
270
+ } ,
271
+ } ) ;
272
+
273
+ const dataset : InferSchemaValues < typeof config > [ "foo" ] [ ] = [ ] ;
274
+
275
+ const result = await ( await evaluate ( tree , { dataset } ) ) . get ( ) ;
276
+
277
+ const expectedResult = [ ] as {
278
+ _id : string ;
279
+ links :
280
+ | {
281
+ _key : string ;
282
+ target : "_blank" | "_self" | null ;
283
+ title : string | null ;
284
+ url : string | null ;
285
+ } [ ]
286
+ | null ;
287
+ title : string ;
288
+ } [ ] ;
289
+
290
+ expect ( result ) . toStrictEqual ( expectedResult ) ;
291
+ expectType <
292
+ ExecuteQuery <
293
+ typeof query ,
294
+ ScopeFromPartialContext < {
295
+ dataset : WritableDeep < typeof dataset > ;
296
+ } >
297
+ >
298
+ > ( ) . toStrictEqual < WritableDeep < typeof expectedResult > > ( ) ;
299
+ } ) ;
98
300
} ) ;
0 commit comments