@@ -61,7 +61,7 @@ pub(crate) struct VariableId(u32);
61
61
62
62
pub ( crate ) fn resolve (
63
63
schema : & Schema ,
64
- query : & graphql_parser:: query:: Document ,
64
+ query : & graphql_parser:: query:: Document < ' static , String > ,
65
65
) -> Result < Query , QueryValidationError > {
66
66
let mut resolved_query: Query = Default :: default ( ) ;
67
67
@@ -98,22 +98,25 @@ pub(crate) fn resolve(
98
98
Ok ( resolved_query)
99
99
}
100
100
101
- fn create_roots (
101
+ fn create_roots < ' doc , T > (
102
102
resolved_query : & mut Query ,
103
- query : & graphql_parser:: query:: Document ,
103
+ query : & graphql_parser:: query:: Document < ' doc , T > ,
104
104
schema : & Schema ,
105
- ) -> Result < ( ) , QueryValidationError > {
105
+ ) -> Result < ( ) , QueryValidationError >
106
+ where
107
+ T : graphql_parser:: query:: Text < ' doc > ,
108
+ {
106
109
// First, give ids to all fragments and operations.
107
110
for definition in & query. definitions {
108
111
match definition {
109
112
graphql_parser:: query:: Definition :: Fragment ( fragment) => {
110
113
let graphql_parser:: query:: TypeCondition :: On ( on) = & fragment. type_condition ;
111
114
resolved_query. fragments . push ( ResolvedFragment {
112
- name : fragment. name . clone ( ) ,
113
- on : schema. find_type ( on) . ok_or_else ( || {
115
+ name : fragment. name . as_ref ( ) . into ( ) ,
116
+ on : schema. find_type ( on. as_ref ( ) ) . ok_or_else ( || {
114
117
QueryValidationError :: new ( format ! (
115
118
"Could not find type {} for fragment {} in schema." ,
116
- on, fragment. name
119
+ on. as_ref ( ) , fragment. name. as_ref ( )
117
120
) )
118
121
} ) ?,
119
122
selection_set : Vec :: new ( ) ,
@@ -130,7 +133,7 @@ fn create_roots(
130
133
} ) ?;
131
134
let resolved_operation: ResolvedOperation = ResolvedOperation {
132
135
object_id : on,
133
- name : m. name . as_ref ( ) . expect ( "mutation without name" ) . to_owned ( ) ,
136
+ name : m. name . as_ref ( ) . expect ( "mutation without name" ) . as_ref ( ) . into ( ) ,
134
137
_operation_type : operations:: OperationType :: Mutation ,
135
138
selection_set : Vec :: with_capacity ( m. selection_set . items . len ( ) ) ,
136
139
} ;
@@ -142,7 +145,7 @@ fn create_roots(
142
145
) => {
143
146
let on = schema. query_type ( ) ;
144
147
let resolved_operation: ResolvedOperation = ResolvedOperation {
145
- name : q. name . as_ref ( ) . expect ( "query without name" ) . to_owned ( ) ,
148
+ name : q. name . as_ref ( ) . expect ( "query without name" ) . as_ref ( ) . into ( ) ,
146
149
_operation_type : operations:: OperationType :: Query ,
147
150
object_id : on,
148
151
selection_set : Vec :: with_capacity ( q. selection_set . items . len ( ) ) ,
@@ -170,7 +173,8 @@ fn create_roots(
170
173
. name
171
174
. as_ref ( )
172
175
. expect ( "subscription without name" )
173
- . to_owned ( ) ,
176
+ . as_ref ( )
177
+ . into ( ) ,
174
178
_operation_type : operations:: OperationType :: Subscription ,
175
179
object_id : on,
176
180
selection_set : Vec :: with_capacity ( s. selection_set . items . len ( ) ) ,
@@ -191,25 +195,28 @@ fn create_roots(
191
195
Ok ( ( ) )
192
196
}
193
197
194
- fn resolve_fragment (
198
+ fn resolve_fragment < ' doc , T > (
195
199
query : & mut Query ,
196
200
schema : & Schema ,
197
- fragment_definition : & graphql_parser:: query:: FragmentDefinition ,
198
- ) -> Result < ( ) , QueryValidationError > {
201
+ fragment_definition : & graphql_parser:: query:: FragmentDefinition < ' doc , T > ,
202
+ ) -> Result < ( ) , QueryValidationError >
203
+ where
204
+ T : graphql_parser:: query:: Text < ' doc > ,
205
+ {
199
206
let graphql_parser:: query:: TypeCondition :: On ( on) = & fragment_definition. type_condition ;
200
- let on = schema. find_type ( on) . ok_or_else ( || {
207
+ let on = schema. find_type ( on. as_ref ( ) ) . ok_or_else ( || {
201
208
QueryValidationError :: new ( format ! (
202
209
"Could not find type `{}` referenced by fragment `{}`" ,
203
- on, fragment_definition. name
210
+ on. as_ref ( ) , fragment_definition. name. as_ref ( )
204
211
) )
205
212
} ) ?;
206
213
207
214
let ( id, _) = query
208
- . find_fragment ( & fragment_definition. name )
215
+ . find_fragment ( fragment_definition. name . as_ref ( ) )
209
216
. ok_or_else ( || {
210
217
QueryValidationError :: new ( format ! (
211
218
"Could not find fragment `{}`." ,
212
- fragment_definition. name
219
+ fragment_definition. name. as_ref ( )
213
220
) )
214
221
} ) ?;
215
222
@@ -224,17 +231,20 @@ fn resolve_fragment(
224
231
Ok ( ( ) )
225
232
}
226
233
227
- fn resolve_union_selection (
234
+ fn resolve_union_selection < ' doc , T > (
228
235
query : & mut Query ,
229
236
_union_id : UnionId ,
230
- selection_set : & graphql_parser:: query:: SelectionSet ,
237
+ selection_set : & graphql_parser:: query:: SelectionSet < ' doc , T > ,
231
238
parent : SelectionParent ,
232
239
schema : & Schema ,
233
- ) -> Result < ( ) , QueryValidationError > {
240
+ ) -> Result < ( ) , QueryValidationError >
241
+ where
242
+ T : graphql_parser:: query:: Text < ' doc > ,
243
+ {
234
244
for item in selection_set. items . iter ( ) {
235
245
match item {
236
246
graphql_parser:: query:: Selection :: Field ( field) => {
237
- if field. name == TYPENAME_FIELD {
247
+ if field. name . as_ref ( ) == TYPENAME_FIELD {
238
248
let id = query. push_selection ( Selection :: Typename , parent) ;
239
249
parent. add_to_selection_set ( query, id) ;
240
250
} else {
@@ -250,11 +260,11 @@ fn resolve_union_selection(
250
260
}
251
261
graphql_parser:: query:: Selection :: FragmentSpread ( fragment_spread) => {
252
262
let ( fragment_id, _fragment) = query
253
- . find_fragment ( & fragment_spread. fragment_name )
263
+ . find_fragment ( fragment_spread. fragment_name . as_ref ( ) )
254
264
. ok_or_else ( || {
255
265
QueryValidationError :: new ( format ! (
256
266
"Could not find fragment `{}` referenced by fragment spread." ,
257
- fragment_spread. fragment_name
267
+ fragment_spread. fragment_name. as_ref ( )
258
268
) )
259
269
} ) ?;
260
270
@@ -268,35 +278,38 @@ fn resolve_union_selection(
268
278
Ok ( ( ) )
269
279
}
270
280
271
- fn resolve_object_selection < ' a > (
281
+ fn resolve_object_selection < ' a , ' doc , T > (
272
282
query : & mut Query ,
273
283
object : & dyn crate :: schema:: ObjectLike ,
274
- selection_set : & graphql_parser:: query:: SelectionSet ,
284
+ selection_set : & graphql_parser:: query:: SelectionSet < ' doc , T > ,
275
285
parent : SelectionParent ,
276
286
schema : & ' a Schema ,
277
- ) -> Result < ( ) , QueryValidationError > {
287
+ ) -> Result < ( ) , QueryValidationError >
288
+ where
289
+ T : graphql_parser:: query:: Text < ' doc > ,
290
+ {
278
291
for item in selection_set. items . iter ( ) {
279
292
match item {
280
293
graphql_parser:: query:: Selection :: Field ( field) => {
281
- if field. name == TYPENAME_FIELD {
294
+ if field. name . as_ref ( ) == TYPENAME_FIELD {
282
295
let id = query. push_selection ( Selection :: Typename , parent) ;
283
296
parent. add_to_selection_set ( query, id) ;
284
297
continue ;
285
298
}
286
299
287
300
let ( field_id, schema_field) = object
288
- . get_field_by_name ( & field. name , schema)
301
+ . get_field_by_name ( field. name . as_ref ( ) , schema)
289
302
. ok_or_else ( || {
290
303
QueryValidationError :: new ( format ! (
291
304
"No field named {} on {}" ,
292
- & field. name,
305
+ field. name. as_ref ( ) ,
293
306
object. name( )
294
307
) )
295
308
} ) ?;
296
309
297
310
let id = query. push_selection (
298
311
Selection :: Field ( SelectedField {
299
- alias : field. alias . clone ( ) ,
312
+ alias : field. alias . as_ref ( ) . map ( |alias| alias . as_ref ( ) . into ( ) ) ,
300
313
field_id,
301
314
selection_set : Vec :: with_capacity ( selection_set. items . len ( ) ) ,
302
315
} ) ,
@@ -320,11 +333,11 @@ fn resolve_object_selection<'a>(
320
333
}
321
334
graphql_parser:: query:: Selection :: FragmentSpread ( fragment_spread) => {
322
335
let ( fragment_id, _fragment) = query
323
- . find_fragment ( & fragment_spread. fragment_name )
336
+ . find_fragment ( fragment_spread. fragment_name . as_ref ( ) )
324
337
. ok_or_else ( || {
325
338
QueryValidationError :: new ( format ! (
326
339
"Could not find fragment `{}` referenced by fragment spread." ,
327
- fragment_spread. fragment_name
340
+ fragment_spread. fragment_name. as_ref ( )
328
341
) )
329
342
} ) ?;
330
343
@@ -338,13 +351,16 @@ fn resolve_object_selection<'a>(
338
351
Ok ( ( ) )
339
352
}
340
353
341
- fn resolve_selection (
354
+ fn resolve_selection < ' doc , T > (
342
355
ctx : & mut Query ,
343
356
on : TypeId ,
344
- selection_set : & graphql_parser:: query:: SelectionSet ,
357
+ selection_set : & graphql_parser:: query:: SelectionSet < ' doc , T > ,
345
358
parent : SelectionParent ,
346
359
schema : & Schema ,
347
- ) -> Result < ( ) , QueryValidationError > {
360
+ ) -> Result < ( ) , QueryValidationError >
361
+ where
362
+ T : graphql_parser:: query:: Text < ' doc > ,
363
+ {
348
364
match on {
349
365
TypeId :: Object ( oid) => {
350
366
let object = schema. get_object ( oid) ;
@@ -370,20 +386,23 @@ fn resolve_selection(
370
386
Ok ( ( ) )
371
387
}
372
388
373
- fn resolve_inline_fragment (
389
+ fn resolve_inline_fragment < ' doc , T > (
374
390
query : & mut Query ,
375
391
schema : & Schema ,
376
- inline_fragment : & graphql_parser:: query:: InlineFragment ,
392
+ inline_fragment : & graphql_parser:: query:: InlineFragment < ' doc , T > ,
377
393
parent : SelectionParent ,
378
- ) -> Result < SelectionId , QueryValidationError > {
394
+ ) -> Result < SelectionId , QueryValidationError >
395
+ where
396
+ T : graphql_parser:: query:: Text < ' doc > ,
397
+ {
379
398
let graphql_parser:: query:: TypeCondition :: On ( on) = inline_fragment
380
399
. type_condition
381
400
. as_ref ( )
382
401
. expect ( "missing type condition on inline fragment" ) ;
383
- let type_id = schema. find_type ( on) . ok_or_else ( || {
402
+ let type_id = schema. find_type ( on. as_ref ( ) ) . ok_or_else ( || {
384
403
QueryValidationError :: new ( format ! (
385
404
"Could not find type `{}` referenced by inline fragment." ,
386
- on
405
+ on. as_ref ( )
387
406
) )
388
407
} ) ?;
389
408
@@ -409,7 +428,7 @@ fn resolve_inline_fragment(
409
428
fn resolve_operation (
410
429
query : & mut Query ,
411
430
schema : & Schema ,
412
- operation : & graphql_parser:: query:: OperationDefinition ,
431
+ operation : & graphql_parser:: query:: OperationDefinition < ' static , String > ,
413
432
) -> Result < ( ) , QueryValidationError > {
414
433
match operation {
415
434
graphql_parser:: query:: OperationDefinition :: Mutation ( m) => {
@@ -554,7 +573,7 @@ impl Query {
554
573
pub ( crate ) struct ResolvedVariable {
555
574
pub ( crate ) operation_id : OperationId ,
556
575
pub ( crate ) name : String ,
557
- pub ( crate ) default : Option < graphql_parser:: query:: Value > ,
576
+ pub ( crate ) default : Option < graphql_parser:: query:: Value < ' static , String > > ,
558
577
pub ( crate ) r#type : StoredFieldType ,
559
578
}
560
579
@@ -622,17 +641,20 @@ impl UsedTypes {
622
641
}
623
642
}
624
643
625
- fn resolve_variables (
644
+ fn resolve_variables < ' doc , T > (
626
645
query : & mut Query ,
627
- variables : & [ graphql_parser:: query:: VariableDefinition ] ,
646
+ variables : & [ graphql_parser:: query:: VariableDefinition < ' doc , T > ] ,
628
647
schema : & Schema ,
629
648
operation_id : OperationId ,
630
- ) {
649
+ )
650
+ where
651
+ T : graphql_parser:: query:: Text < ' doc > ,
652
+ {
631
653
for var in variables {
632
654
query. variables . push ( ResolvedVariable {
633
655
operation_id,
634
- name : var. name . clone ( ) ,
635
- default : var. default_value . clone ( ) ,
656
+ name : var. name . as_ref ( ) . into ( ) ,
657
+ default : var. default_value . as_ref ( ) . map ( |dflt| dflt . into_static ( ) ) ,
636
658
r#type : resolve_field_type ( schema, & var. var_type ) ,
637
659
} ) ;
638
660
}
0 commit comments