hive-router 0.2.0

GraphQL router for Federation, part of the Hive platform
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
use std::borrow::Cow;
use std::sync::Arc;

use graphql_tools::parser::query::Value as QueryValue;
use graphql_tools::static_graphql::schema::{
    Definition, Directive, DirectiveDefinition, Document, EnumValue, Field, InputValue, Type,
    TypeDefinition,
};

use crate::query_planner::ast::{
    operation::OperationDefinition,
    selection_item::SelectionItem,
    selection_set::{FieldSelection, SelectionSet},
    value::Value as AstValue,
};
use sonic_rs::JsonValueTrait;

use crate::executor::execution::plan::CoerceVariablesPayload;
use crate::executor::introspection::schema::SchemaMetadata;
use crate::executor::response::value::Value;

pub struct IntrospectionContext {
    pub query: Option<Arc<OperationDefinition>>,
    pub schema: Arc<Document>,
    pub metadata: Arc<SchemaMetadata>,
    pub variables: Arc<CoerceVariablesPayload>,
}

fn resolve_boolean_variable(
    var_name: &str,
    variables: &Arc<CoerceVariablesPayload>,
) -> Option<bool> {
    variables
        .variables_map
        .as_ref()
        .and_then(|map| map.get(var_name))
        .and_then(|value| value.as_bool())
}

fn resolve_str_variable<'a>(
    var_name: &str,
    variables: &'a Arc<CoerceVariablesPayload>,
) -> Option<&'a str> {
    variables
        .variables_map
        .as_ref()
        .and_then(|map| map.get(var_name))
        .and_then(|value| value.as_str())
}

fn get_deprecation_reason(directives: &[Directive]) -> Option<&str> {
    directives
        .iter()
        .find(|d| d.name == "deprecated")
        .and_then(|d| {
            d.arguments
                .iter()
                .find(|(name, _)| name.as_str() == "reason")
        })
        .and_then(|(_, value)| {
            if let QueryValue::String(s) = value {
                Some(s.as_str())
            } else {
                None
            }
        })
}

fn is_deprecated(directives: &[Directive]) -> bool {
    directives.iter().any(|d| d.name == "deprecated")
}

fn is_deprecated_enum(enum_val: &EnumValue) -> bool {
    is_deprecated(&enum_val.directives)
}

fn get_specified_by_url(directives: &[Directive]) -> Option<&str> {
    directives
        .iter()
        .find(|d| d.name == "specifiedBy")
        .and_then(|d| d.arguments.iter().find(|(name, _)| name.as_str() == "url"))
        .and_then(|(_, value)| {
            if let QueryValue::String(s) = value {
                Some(s.as_str())
            } else {
                None
            }
        })
}

fn is_one_of(directives: &[Directive]) -> bool {
    directives.iter().any(|d| d.name == "oneOf")
}

fn kind_to_str<'exec>(type_def: &'exec TypeDefinition) -> Cow<'exec, str> {
    (match type_def {
        TypeDefinition::Scalar(_) => "SCALAR",
        TypeDefinition::Object(_) => "OBJECT",
        TypeDefinition::Interface(_) => "INTERFACE",
        TypeDefinition::Union(_) => "UNION",
        TypeDefinition::Enum(_) => "ENUM",
        TypeDefinition::InputObject(_) => "INPUT_OBJECT",
    })
    .into()
}

fn resolve_input_value<'exec>(
    iv: &'exec InputValue,
    selections: &'exec SelectionSet,
    ctx: &'exec IntrospectionContext,
) -> Value<'exec> {
    let mut iv_data = resolve_input_value_selections(iv, &selections.items, ctx);
    iv_data.sort_by_key(|(k, _)| *k);
    Value::Object(iv_data)
}

fn resolve_input_value_selections<'exec>(
    iv: &'exec InputValue,
    selection_items: &'exec Vec<SelectionItem>,
    ctx: &'exec IntrospectionContext,
) -> Vec<(&'exec str, Value<'exec>)> {
    let mut iv_data: Vec<(&str, Value<'_>)> = Vec::with_capacity(selection_items.len());
    for item in selection_items {
        if let SelectionItem::Field(field) = item {
            let value = match field.name.as_str() {
                "name" => Value::String(iv.name.as_str().into()),
                "description" => iv
                    .description
                    .as_ref()
                    .map_or(Value::Null, |s| Value::String(s.into())),
                "type" => resolve_type(&iv.value_type, &field.selections, ctx),
                "defaultValue" => iv
                    .default_value
                    .as_ref()
                    .map_or_else(|| Value::Null, |ast| Value::String(ast.to_string().into())), // TODO: support default values
                "isDeprecated" => Value::Bool(is_deprecated(&iv.directives)),
                "deprecationReason" => get_deprecation_reason(&iv.directives)
                    .map_or(Value::Null, |s| Value::String(s.into())),
                "__typename" => Value::String("__InputValue".into()),
                _ => Value::Null,
            };
            iv_data.push((field.selection_identifier(), value));
        } else if let SelectionItem::InlineFragment(_) = item {
            let selection_items = item.selections();
            if let Some(selection_items) = selection_items {
                let new_data = resolve_input_value_selections(iv, selection_items, ctx);
                iv_data.extend(new_data);
            }
        }
    }
    iv_data
}

fn resolve_field<'exec>(
    f: &'exec Field,
    selections: &'exec SelectionSet,
    ctx: &'exec IntrospectionContext,
) -> Value<'exec> {
    let mut field_data = resolve_field_selections(f, &selections.items, ctx);
    field_data.sort_by_key(|(k, _)| *k);
    Value::Object(field_data)
}

fn resolve_field_selections<'exec>(
    f: &'exec Field,
    selection_items: &'exec Vec<SelectionItem>,
    ctx: &'exec IntrospectionContext,
) -> Vec<(&'exec str, Value<'exec>)> {
    let mut field_data = Vec::with_capacity(selection_items.len());
    for item in selection_items {
        if let SelectionItem::Field(field) = item {
            let value = match field.name.as_str() {
                "name" => Value::String(f.name.as_str().into()),
                "description" => f
                    .description
                    .as_ref()
                    .map_or(Value::Null, |s| Value::String(s.into())),
                "args" => {
                    let args: Vec<_> = f
                        .arguments
                        .iter()
                        .map(|arg| resolve_input_value(arg, &field.selections, ctx))
                        .collect();
                    Value::Array(args)
                }
                "type" => resolve_type(&f.field_type, &field.selections, ctx),
                "isDeprecated" => Value::Bool(is_deprecated(&f.directives)),
                "deprecationReason" => get_deprecation_reason(&f.directives)
                    .map_or(Value::Null, |s| Value::String(s.into())),
                "__typename" => Value::String("__Field".into()),
                _ => Value::Null,
            };
            field_data.push((field.selection_identifier(), value));
        } else if let SelectionItem::InlineFragment(_) = item {
            let selection_items = item.selections();
            if let Some(selection_items) = selection_items {
                let new_data = resolve_field_selections(f, selection_items, ctx);
                field_data.extend(new_data);
            }
        }
    }
    field_data
}

fn resolve_enum_value<'exec>(
    ev: &'exec EnumValue,
    selections: &'exec SelectionSet,
) -> Value<'exec> {
    let mut ev_data = resolve_enum_value_selections(ev, &selections.items);
    ev_data.sort_by_key(|(k, _)| *k);
    Value::Object(ev_data)
}

fn resolve_enum_value_selections<'exec>(
    ev: &'exec EnumValue,
    selection_items: &'exec Vec<SelectionItem>,
) -> Vec<(&'exec str, Value<'exec>)> {
    let mut ev_data = Vec::with_capacity(selection_items.len());
    for item in selection_items {
        if let SelectionItem::Field(field) = item {
            let value = match field.name.as_str() {
                "name" => Value::String(ev.name.as_str().into()),
                "description" => ev
                    .description
                    .as_ref()
                    .map_or(Value::Null, |s| Value::String(s.into())),
                "isDeprecated" => Value::Bool(is_deprecated_enum(ev)),
                "deprecationReason" => get_deprecation_reason(&ev.directives)
                    .map_or(Value::Null, |s| Value::String(s.into())),
                "__typename" => Value::String("__EnumValue".into()),
                _ => Value::Null,
            };
            ev_data.push((field.selection_identifier(), value));
        } else if let SelectionItem::InlineFragment(_) = item {
            let selection_items = item.selections();
            if let Some(selection_items) = selection_items {
                let new_data = resolve_enum_value_selections(ev, selection_items);
                ev_data.extend(new_data);
            }
        }
    }
    ev_data
}

fn resolve_type_definition<'exec>(
    type_def: &'exec TypeDefinition,
    selections: &'exec SelectionSet,
    ctx: &'exec IntrospectionContext,
) -> Value<'exec> {
    let mut type_data = resolve_type_definition_selections(type_def, &selections.items, ctx);
    type_data.sort_by_key(|(k, _)| *k);
    Value::Object(type_data)
}

fn resolve_type_definition_selections<'exec>(
    type_def: &'exec TypeDefinition,
    selection_items: &'exec Vec<SelectionItem>,
    ctx: &'exec IntrospectionContext,
) -> Vec<(&'exec str, Value<'exec>)> {
    let mut type_data = Vec::with_capacity(selection_items.len());

    for item in selection_items {
        if let SelectionItem::Field(field) = item {
            let value = match field.name.as_str() {
                "kind" => Value::String(kind_to_str(type_def)),
                "name" => match type_def {
                    TypeDefinition::Scalar(s) => Some(&s.name),
                    TypeDefinition::Object(o) => Some(&o.name),
                    TypeDefinition::Interface(i) => Some(&i.name),
                    TypeDefinition::Union(u) => Some(&u.name),
                    TypeDefinition::Enum(e) => Some(&e.name),
                    TypeDefinition::InputObject(io) => Some(&io.name),
                }
                .map(|s| Value::String(s.into()))
                .unwrap_or(Value::Null),
                "description" => match type_def {
                    TypeDefinition::Scalar(s) => s.description.as_ref(),
                    TypeDefinition::Object(o) => o.description.as_ref(),
                    TypeDefinition::Interface(i) => i.description.as_ref(),
                    TypeDefinition::Union(u) => u.description.as_ref(),
                    TypeDefinition::Enum(e) => e.description.as_ref(),
                    TypeDefinition::InputObject(io) => io.description.as_ref(),
                }
                .map_or(Value::Null, |s| Value::String(s.into())),
                "specifiedByURL" => {
                    if let TypeDefinition::Scalar(scalar) = type_def {
                        get_specified_by_url(&scalar.directives)
                            .map_or(Value::Null, |url| Value::String(url.into()))
                    } else {
                        Value::Null
                    }
                }
                "isOneOf" => {
                    if let TypeDefinition::InputObject(type_def) = type_def {
                        Value::Bool(is_one_of(&type_def.directives))
                    } else {
                        Value::Null
                    }
                }
                "fields" => {
                    let fields = match type_def {
                        TypeDefinition::Object(o) => Some(&o.fields),
                        TypeDefinition::Interface(i) => Some(&i.fields),
                        _ => None,
                    };
                    if let Some(fields) = fields {
                        let include_deprecated = field
                            .arguments
                            .as_ref()
                            .and_then(|a| a.get_argument("includeDeprecated"))
                            .and_then(|v| match v {
                                AstValue::Boolean(b) => Some(*b),
                                AstValue::Variable(var_name) => {
                                    resolve_boolean_variable(var_name.as_str(), &ctx.variables)
                                }
                                _ => None,
                            })
                            .unwrap_or(false);

                        let fields_values: Vec<Value<'exec>> = fields
                            .iter()
                            .filter(|f| {
                                !f.name.starts_with("__")
                                    && (include_deprecated || !is_deprecated(&f.directives))
                            })
                            .map(|f| resolve_field(f, &field.selections, ctx))
                            .collect();
                        Value::Array(fields_values)
                    } else {
                        Value::Null
                    }
                }
                "interfaces" => {
                    if let TypeDefinition::Object(obj) = type_def {
                        let interface_values: Vec<_> = obj
                            .implements_interfaces
                            .iter()
                            .filter_map(|iface_name| ctx.schema.type_by_name(iface_name))
                            .map(|t| resolve_type_definition(t, &field.selections, ctx))
                            .collect();
                        Value::Array(interface_values)
                    } else {
                        Value::Null
                    }
                }
                "possibleTypes" => {
                    if let TypeDefinition::Interface(_) | TypeDefinition::Union(_) = type_def {
                        let possible_types: Vec<Value<'exec>> = ctx
                            .metadata
                            .possible_types
                            .get_possible_types(type_def.name())
                            .into_iter()
                            .filter(|v| v != type_def.name())
                            .filter_map(|name| ctx.schema.type_by_name(name.as_str()))
                            .map(|t| resolve_type_definition(t, &field.selections, ctx))
                            .collect();
                        Value::Array(possible_types)
                    } else {
                        Value::Null
                    }
                }
                "enumValues" => {
                    if let TypeDefinition::Enum(enum_type) = type_def {
                        let include_deprecated = field
                            .arguments
                            .as_ref()
                            .and_then(|a| a.get_argument("includeDeprecated"))
                            .and_then(|v| match v {
                                AstValue::Boolean(b) => Some(*b),
                                AstValue::Variable(var_name) => {
                                    resolve_boolean_variable(var_name.as_str(), &ctx.variables)
                                }
                                _ => None,
                            })
                            .unwrap_or(false);

                        let enum_values: Vec<_> = enum_type
                            .values
                            .iter()
                            .filter(|v| include_deprecated || !is_deprecated_enum(v))
                            .map(|v| resolve_enum_value(v, &field.selections))
                            .collect();
                        Value::Array(enum_values)
                    } else {
                        Value::Null
                    }
                }
                "inputFields" => match type_def {
                    TypeDefinition::InputObject(io) => {
                        let fields_values: Vec<_> = io
                            .fields
                            .iter()
                            .map(|f| resolve_input_value(f, &field.selections, ctx))
                            .collect();
                        Value::Array(fields_values)
                    }
                    _ => Value::Null,
                },
                "ofType" => Value::Null,
                "__typename" => Value::String("__Type".into()),
                _ => Value::Null,
            };
            type_data.push((field.selection_identifier(), value));
        } else if let SelectionItem::InlineFragment(_) = item {
            let selection_items = item.selections();
            if let Some(selection_items) = selection_items {
                let new_data = resolve_type_definition_selections(type_def, selection_items, ctx);
                type_data.extend(new_data);
            }
        }
    }
    type_data
}
fn resolve_wrapper_type<'exec>(
    kind: &'exec str,
    inner_type: &'exec Type,
    selections: &'exec SelectionSet,
    ctx: &'exec IntrospectionContext,
) -> Value<'exec> {
    let mut type_data = resolve_wrapper_type_selections(kind, inner_type, &selections.items, ctx);
    type_data.sort_by_key(|(k, _)| *k);
    Value::Object(type_data)
}

fn resolve_wrapper_type_selections<'exec>(
    kind: &'exec str,
    inner_type: &'exec Type,
    selection_items: &'exec Vec<SelectionItem>,
    ctx: &'exec IntrospectionContext,
) -> Vec<(&'exec str, Value<'exec>)> {
    let mut type_data = Vec::with_capacity(selection_items.len());
    for item in selection_items {
        if let SelectionItem::Field(field) = item {
            let value = match field.name.as_str() {
                "kind" => Value::String(kind.into()),
                "name" => Value::Null,
                "ofType" => resolve_type(inner_type, &field.selections, ctx),
                "__typename" => Value::String("__Type".into()),
                _ => Value::Null,
            };
            type_data.push((field.selection_identifier(), value));
        } else if let SelectionItem::InlineFragment(_) = item {
            let selection_items = item.selections();
            if let Some(selection_items) = selection_items {
                let new_data =
                    resolve_wrapper_type_selections(kind, inner_type, selection_items, ctx);
                type_data.extend(new_data);
            }
        }
    }
    type_data
}

fn resolve_type<'exec>(
    t: &'exec Type,
    selections: &'exec SelectionSet,
    ctx: &'exec IntrospectionContext,
) -> Value<'exec> {
    match t {
        Type::NamedType(name) => {
            let type_def = ctx.schema.type_by_name(name).unwrap_or_else(|| {
                panic!(
                    "Type '{}' not found in the schema unexpectedly during introspection",
                    name
                );
            });
            resolve_type_definition(type_def, selections, ctx)
        }
        Type::ListType(inner_t) => resolve_wrapper_type("LIST", inner_t, selections, ctx),
        Type::NonNullType(inner_t) => resolve_wrapper_type("NON_NULL", inner_t, selections, ctx),
    }
}

fn resolve_directive<'exec>(
    d: &'exec DirectiveDefinition,
    selections: &'exec SelectionSet,
    ctx: &'exec IntrospectionContext,
) -> Value<'exec> {
    let mut directive_data = resolve_directive_selections(d, &selections.items, ctx);
    directive_data.sort_by_key(|(k, _)| *k);
    Value::Object(directive_data)
}

fn resolve_directive_selections<'exec>(
    d: &'exec DirectiveDefinition,
    selection_items: &'exec Vec<SelectionItem>,
    ctx: &'exec IntrospectionContext,
) -> Vec<(&'exec str, Value<'exec>)> {
    let mut directive_data = Vec::with_capacity(selection_items.len());
    for item in selection_items {
        if let SelectionItem::Field(field) = item {
            let value = match field.name.as_str() {
                "name" => Value::String(d.name.as_str().into()),
                "description" => d
                    .description
                    .as_ref()
                    .map_or(Value::Null, |s| Value::String(s.into())),
                "locations" => {
                    let locs: Vec<_> = d
                        .locations
                        .iter()
                        .map(|l| Value::String(l.as_str().into()))
                        .collect();
                    Value::Array(locs)
                }
                "args" => {
                    let args: Vec<_> = d
                        .arguments
                        .iter()
                        .map(|arg| resolve_input_value(arg, &field.selections, ctx))
                        .collect();
                    Value::Array(args)
                }
                "isRepeatable" => Value::Bool(d.repeatable),
                "__typename" => Value::String("__Directive".into()),
                _ => Value::Null,
            };
            directive_data.push((field.selection_identifier(), value));
        } else if let SelectionItem::InlineFragment(_) = item {
            let selection_items = item.selections();
            if let Some(selection_items) = selection_items {
                let new_data = resolve_directive_selections(d, selection_items, ctx);
                directive_data.extend(new_data);
            }
        }
    }
    directive_data
}

fn resolve_schema_field<'exec>(
    field: &'exec FieldSelection,
    ctx: &'exec IntrospectionContext,
) -> Value<'exec> {
    let mut schema_data = resolve_schema_selections(&field.selections.items, ctx);

    schema_data.sort_by_key(|(k, _)| *k);
    Value::Object(schema_data)
}

fn resolve_schema_selections<'exec>(
    items: &'exec Vec<SelectionItem>,
    ctx: &'exec IntrospectionContext,
) -> Vec<(&'exec str, Value<'exec>)> {
    let mut schema_data = Vec::with_capacity(items.len());

    for item in items {
        if let SelectionItem::Field(inner_field) = item {
            let value = match inner_field.name.as_str() {
                "description" => Value::Null,
                "types" => {
                    let types = ctx
                        .schema
                        .type_map()
                        .values()
                        .map(|t| resolve_type_definition(t, &inner_field.selections, ctx))
                        .collect();
                    Value::Array(types)
                }
                "queryType" => {
                    let query_type = ctx
                        .metadata
                        .query_type_name
                        .as_ref()
                        .and_then(|name| ctx.schema.type_by_name(name))
                        // SAFETY: The query type is guaranteed to exist,
                        // every schema has a query type.
                        .expect("invariant violation: query type is guaranteed to exist because every schema must have a query type");
                    resolve_type_definition(query_type, &inner_field.selections, ctx)
                }
                "mutationType" => ctx
                    .schema
                    .mutation_type_name()
                    .and_then(|name| ctx.schema.type_by_name(name))
                    .map_or(Value::Null, |t| {
                        resolve_type_definition(t, &inner_field.selections, ctx)
                    }),
                "subscriptionType" => ctx
                    .schema
                    .subscription_type_name()
                    .and_then(|name| ctx.schema.type_by_name(name))
                    .map_or(Value::Null, |t| {
                        resolve_type_definition(t, &inner_field.selections, ctx)
                    }),
                "directives" => {
                    let directives = ctx
                        .schema
                        .definitions
                        .iter()
                        .filter_map(|d| match d {
                            Definition::DirectiveDefinition(d) => Some(d),
                            _ => None,
                        })
                        .map(|d| resolve_directive(d, &inner_field.selections, ctx))
                        .collect();
                    Value::Array(directives)
                }
                "__typename" => Value::String("__Schema".into()),
                _ => Value::Null,
            };
            schema_data.push((inner_field.selection_identifier(), value));
        } else if let SelectionItem::FragmentSpread(_) = item {
            let selection_items = item.selections();
            if let Some(selection_items) = selection_items {
                let new_data = resolve_schema_selections(selection_items, ctx);
                schema_data.extend(new_data);
            }
        }
    }
    schema_data
}

pub fn resolve_introspection<'exec>(
    operation_definition: &'exec OperationDefinition,
    ctx: &'exec IntrospectionContext,
) -> Value<'exec> {
    let root_selection_set = &operation_definition.selection_set;
    let root_type_name = ctx
        .metadata
        .expect_root_type_name(operation_definition.operation_kind.as_ref());

    let mut data =
        resolve_root_introspection_selections(root_type_name, &root_selection_set.items, ctx);

    data.sort_by_key(|(k, _)| *k);
    Value::Object(data)
}

fn resolve_root_introspection_selections<'exec>(
    root_type_name: &'exec str,
    items: &'exec Vec<SelectionItem>,
    ctx: &'exec IntrospectionContext,
) -> Vec<(&'exec str, Value<'exec>)> {
    let mut data = Vec::with_capacity(items.len());
    for item in items {
        if let SelectionItem::Field(field) = item {
            let value = match field.name.as_str() {
                "__schema" => resolve_schema_field(field, ctx),
                "__type" => {
                    if let Some(args) = &field.arguments {
                        let type_value = match args.get_argument("name") {
                            Some(AstValue::String(type_name)) => {
                                ctx.schema.type_by_name(type_name).map_or(Value::Null, |t| {
                                    resolve_type_definition(t, &field.selections, ctx)
                                })
                            }
                            Some(AstValue::Variable(var_name)) => {
                                if let Some(var_value) =
                                    resolve_str_variable(var_name.as_str(), &ctx.variables)
                                {
                                    ctx.schema.type_by_name(var_value).map_or(Value::Null, |t| {
                                        resolve_type_definition(t, &field.selections, ctx)
                                    })
                                } else {
                                    Value::Null
                                }
                            }

                            _ => Value::Null,
                        };

                        type_value
                    } else {
                        Value::Null
                    }
                }
                "__typename" => Value::String(root_type_name.into()),
                _ => Value::Null,
            };
            data.push((field.selection_identifier(), value));
        } else if let SelectionItem::InlineFragment(_) = item {
            let selection_items = item.selections();
            if let Some(selection_items) = selection_items {
                let new_data =
                    resolve_root_introspection_selections(root_type_name, selection_items, ctx);
                data.extend(new_data);
            }
        }
    }
    data
}