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
pub use super::v1::{
    Definition, EnumId, FieldId, FieldProvides, FieldRequires, FieldSet, FieldSetItem, FieldType, InputObjectId,
    InterfaceField, InterfaceId, Key, ListWrapper, ObjectField, ObjectId, Override, OverrideLabel, OverrideSource,
    RootOperationTypes, ScalarId, StringId, Subgraph, SubgraphId, TypeId, UnionId,
};

/// A composed federated graph.
///
/// ## API contract
///
/// Guarantees:
///
/// - All the identifiers are correct.
///
/// Does not guarantee:
///
/// - The ordering of items inside each `Vec`.
#[derive(serde::Serialize, serde::Deserialize, Clone)]
pub struct FederatedGraphV2 {
    pub subgraphs: Vec<Subgraph>,

    pub root_operation_types: RootOperationTypes,
    pub objects: Vec<Object>,
    pub object_fields: Vec<ObjectField>,

    pub interfaces: Vec<Interface>,
    pub interface_fields: Vec<InterfaceField>,

    pub fields: Vec<Field>,

    pub enums: Vec<Enum>,
    pub unions: Vec<Union>,
    pub scalars: Vec<Scalar>,
    pub input_objects: Vec<InputObject>,
    pub enum_values: Vec<EnumValue>,

    /// All [input value definitions](http://spec.graphql.org/October2021/#InputValueDefinition) in the federated graph. Concretely, these are arguments of output fields, and input object fields.
    pub input_value_definitions: Vec<InputValueDefinition>,

    /// All the strings in the federated graph, deduplicated.
    pub strings: Vec<String>,

    /// All the field types in the federated graph, deduplicated.
    pub field_types: Vec<FieldType>,

    /// All composed directive instances (not definitions) in a federated graph.
    pub directives: Vec<Directive>,
}

impl std::fmt::Debug for FederatedGraphV2 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct(std::any::type_name::<Self>()).finish_non_exhaustive()
    }
}

#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct InputValueDefinition {
    pub name: StringId,
    pub type_id: TypeId,
    pub directives: Directives,
    pub description: Option<StringId>,
}

#[derive(Default, serde::Serialize, serde::Deserialize, Clone, PartialEq, PartialOrd, Debug)]
pub enum Value {
    #[default]
    Null,
    String(StringId),
    Int(i64),
    Float(f64),
    Boolean(bool),
    /// Different from `String`.
    ///
    /// `@tag(name: "SOMETHING")` vs `@tag(name: SOMETHING)`
    EnumValue(StringId),
    Object(Box<[(StringId, Value)]>),
    List(Box<[Value]>),
}

impl Value {
    pub fn as_list(&self) -> Option<&[Value]> {
        if let Self::List(v) = self {
            Some(v)
        } else {
            None
        }
    }

    pub fn as_string(&self) -> Option<&StringId> {
        if let Self::String(v) = self {
            Some(v)
        } else {
            None
        }
    }

    pub fn is_list(&self) -> bool {
        matches!(self, Self::List(_))
    }

    pub fn is_null(&self) -> bool {
        matches!(self, Self::Null)
    }
}

#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, PartialOrd)]
pub enum Directive {
    Inaccessible,
    Deprecated {
        reason: Option<StringId>,
    },
    Other {
        name: StringId,
        arguments: Vec<(StringId, Value)>,
    },
}

#[derive(serde::Serialize, serde::Deserialize, Clone)]
pub struct Enum {
    pub name: StringId,
    pub values: EnumValues,

    /// All directives that made it through composition. Notably includes `@tag`.
    pub composed_directives: Directives,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<StringId>,
}

#[derive(serde::Serialize, serde::Deserialize, Clone)]
pub struct EnumValue {
    pub value: StringId,

    /// All directives that made it through composition. Notably includes `@tag`.
    pub composed_directives: Directives,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<StringId>,
}

#[derive(serde::Serialize, serde::Deserialize, Clone)]
pub struct Field {
    pub name: StringId,
    pub field_type_id: TypeId,

    pub arguments: InputValueDefinitions,

    /// This is populated only of fields of entities. The Vec includes all subgraphs the field can
    /// be resolved in. For a regular field of an entity, it will be one subgraph, the subgraph
    /// where the entity field is defined. For a shareable field in an entity, this contains the
    /// subgraphs where the shareable field is defined on the entity. It may not be all the
    /// subgraphs.
    ///
    /// On fields of value types and input types, this is empty.
    pub resolvable_in: Vec<SubgraphId>,

    /// See [FieldProvides].
    pub provides: Vec<FieldProvides>,

    /// See [FieldRequires]
    pub requires: Vec<FieldRequires>,

    /// See [Override].
    pub overrides: Vec<Override>,

    /// All directives that made it through composition. Notably includes `@tag`.
    pub composed_directives: Directives,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<StringId>,
}

#[derive(serde::Serialize, serde::Deserialize, Clone)]
pub struct Object {
    pub name: StringId,

    pub implements_interfaces: Vec<InterfaceId>,

    #[serde(rename = "resolvable_keys")]
    pub keys: Vec<Key>,

    /// All directives that made it through composition. Notably includes `@tag`.
    pub composed_directives: Directives,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<StringId>,
}

#[derive(serde::Serialize, serde::Deserialize, Clone)]
pub struct Interface {
    pub name: StringId,

    pub implements_interfaces: Vec<InterfaceId>,

    /// All keys, for entity interfaces.
    #[serde(rename = "resolvable_keys")]
    pub keys: Vec<Key>,

    /// All directives that made it through composition. Notably includes `@tag`.
    pub composed_directives: Directives,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<StringId>,
}

#[derive(serde::Serialize, serde::Deserialize, Clone)]
pub struct Scalar {
    pub name: StringId,

    /// All directives that made it through composition. Notably includes `@tag`.
    pub composed_directives: Directives,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<StringId>,
}

#[derive(serde::Serialize, serde::Deserialize, Clone)]
pub struct Union {
    pub name: StringId,
    pub members: Vec<ObjectId>,

    /// All directives that made it through composition. Notably includes `@tag`.
    pub composed_directives: Directives,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<StringId>,
}

#[derive(serde::Serialize, serde::Deserialize, Clone)]
pub struct InputObject {
    pub name: StringId,

    pub fields: InputValueDefinitions,

    /// All directives that made it through composition. Notably includes `@tag`.
    pub composed_directives: Directives,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<StringId>,
}

/// A (start, len) range in FederatedSchema.
pub type Directives = (DirectiveId, usize);

#[derive(serde::Serialize, serde::Deserialize, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct DirectiveId(pub usize);

impl From<DirectiveId> for usize {
    fn from(DirectiveId(index): DirectiveId) -> Self {
        index
    }
}
pub const NO_DIRECTIVES: Directives = (DirectiveId(0), 0);

#[derive(serde::Serialize, serde::Deserialize, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct InputValueDefinitionId(pub usize);

/// A (start, len) range in FederatedSchema.
pub type InputValueDefinitions = (InputValueDefinitionId, usize);

impl From<InputValueDefinitionId> for usize {
    fn from(InputValueDefinitionId(index): InputValueDefinitionId) -> Self {
        index
    }
}

impl From<usize> for InputValueDefinitionId {
    fn from(index: usize) -> Self {
        InputValueDefinitionId(index)
    }
}

pub const NO_INPUT_VALUE_DEFINITION: InputValueDefinitions = (InputValueDefinitionId(0), 0);

#[derive(serde::Serialize, serde::Deserialize, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct EnumValueId(pub usize);

impl From<EnumValueId> for usize {
    fn from(EnumValueId(index): EnumValueId) -> Self {
        index
    }
}

impl From<usize> for EnumValueId {
    fn from(index: usize) -> Self {
        EnumValueId(index)
    }
}

/// A (start, len) range in FederatedSchema.
pub type EnumValues = (EnumValueId, usize);

pub const NO_ENUM_VALUE: EnumValues = (EnumValueId(0), 0);

impl std::ops::Index<Directives> for FederatedGraphV2 {
    type Output = [Directive];

    fn index(&self, index: Directives) -> &Self::Output {
        let (DirectiveId(start), len) = index;
        &self.directives[start..(start + len)]
    }
}

impl std::ops::Index<InputValueDefinitions> for FederatedGraphV2 {
    type Output = [InputValueDefinition];

    fn index(&self, index: InputValueDefinitions) -> &Self::Output {
        let (InputValueDefinitionId(start), len) = index;
        &self.input_value_definitions[start..(start + len)]
    }
}

impl std::ops::Index<EnumValues> for FederatedGraphV2 {
    type Output = [EnumValue];

    fn index(&self, index: EnumValues) -> &Self::Output {
        let (EnumValueId(start), len) = index;
        &self.enum_values[start..(start + len)]
    }
}

macro_rules! id_newtypes {
    ($($name:ident + $storage:ident + $out:ident,)*) => {
        $(
            impl std::ops::Index<$name> for FederatedGraphV2 {
                type Output = $out;

                fn index(&self, index: $name) -> &$out {
                    &self.$storage[index.0]
                }
            }

            impl std::ops::IndexMut<$name> for FederatedGraphV2 {
                fn index_mut(&mut self, index: $name) -> &mut $out {
                    &mut self.$storage[index.0]
                }
            }
        )*
    }
}

id_newtypes! {
    EnumId + enums + Enum,
    FieldId + fields + Field,
    TypeId + field_types + FieldType,
    InputObjectId + input_objects + InputObject,
    InterfaceId + interfaces + Interface,
    ObjectId + objects + Object,
    ScalarId + scalars + Scalar,
    StringId + strings + String,
    SubgraphId + subgraphs + Subgraph,
    UnionId + unions + Union,
}

impl From<super::v1::FederatedGraphV1> for FederatedGraphV2 {
    fn from(
        super::v1::FederatedGraphV1 {
            subgraphs,
            root_operation_types,
            objects,
            object_fields,
            interfaces,
            interface_fields,
            fields,
            enums,
            unions,
            scalars,
            input_objects,
            strings,
            field_types,
        }: super::v1::FederatedGraphV1,
    ) -> Self {
        let mut directives = vec![];
        let mut input_value_definitions = vec![];
        let mut enum_values = vec![];

        let convert_directives = |original: Vec<super::v1::Directive>, directives: &mut Vec<Directive>| -> Directives {
            let start = directives.len();

            for directive in original {
                let directive = if strings[directive.name.0] == "inaccessible" {
                    Directive::Inaccessible
                } else {
                    Directive::Other {
                        name: directive.name,
                        arguments: directive
                            .arguments
                            .into_iter()
                            .map(|(name, value)| (name, (value, strings.as_slice()).into()))
                            .collect(),
                    }
                };

                directives.push(directive);
            }

            let end = directives.len();
            (DirectiveId(start), end - start)
        };

        let convert_arguments = |original: Vec<super::v1::FieldArgument>,
                                 input_value_definitions: &mut Vec<InputValueDefinition>,
                                 directives: &mut Vec<Directive>|
         -> InputValueDefinitions {
            let start = input_value_definitions.len();

            for super::v1::FieldArgument {
                name,
                type_id,
                composed_directives,
                description,
            } in original
            {
                input_value_definitions.push(InputValueDefinition {
                    name,
                    type_id,
                    directives: convert_directives(composed_directives, directives),
                    description,
                });
            }

            (InputValueDefinitionId(start), input_value_definitions.len() - start)
        };

        let convert_input_object_fields = |original: Vec<super::v1::InputObjectField>,
                                           input_value_definitions: &mut Vec<InputValueDefinition>,
                                           directives: &mut Vec<Directive>|
         -> InputValueDefinitions {
            let start = input_value_definitions.len();

            for super::v1::InputObjectField {
                name,
                field_type_id,
                composed_directives,
                description,
            } in original
            {
                input_value_definitions.push(InputValueDefinition {
                    name,
                    type_id: field_type_id,
                    directives: convert_directives(composed_directives, directives),
                    description,
                });
            }

            (InputValueDefinitionId(start), input_value_definitions.len() - start)
        };

        let convert_enum_values = |original: Vec<super::v1::EnumValue>,
                                   enum_values: &mut Vec<EnumValue>,
                                   directives: &mut Vec<Directive>|
         -> EnumValues {
            let start = enum_values.len();

            for super::v1::EnumValue {
                value,
                composed_directives,
                description,
            } in original
            {
                enum_values.push(EnumValue {
                    value,
                    composed_directives: convert_directives(composed_directives, directives),
                    description,
                })
            }

            (EnumValueId(start), enum_values.len() - start)
        };

        FederatedGraphV2 {
            subgraphs,
            root_operation_types,
            objects: objects
                .into_iter()
                .map(
                    |super::v1::Object {
                         name,
                         implements_interfaces,
                         keys,
                         composed_directives,
                         description,
                     }| Object {
                        name,
                        implements_interfaces,
                        keys,
                        composed_directives: convert_directives(composed_directives, &mut directives),
                        description,
                    },
                )
                .collect(),
            object_fields,
            interfaces: interfaces
                .into_iter()
                .map(
                    |super::v1::Interface {
                         name,
                         implements_interfaces,
                         keys,
                         composed_directives,
                         description,
                     }| Interface {
                        name,
                        implements_interfaces,
                        keys,
                        composed_directives: convert_directives(composed_directives, &mut directives),
                        description,
                    },
                )
                .collect(),
            interface_fields,
            fields: fields
                .into_iter()
                .map(
                    |super::v1::Field {
                         name,
                         field_type_id,
                         resolvable_in,
                         provides,
                         requires,
                         overrides,
                         arguments,
                         composed_directives,
                         description,
                     }| Field {
                        name,
                        field_type_id,
                        arguments: convert_arguments(arguments, &mut input_value_definitions, &mut directives),
                        resolvable_in,
                        provides,
                        requires,
                        overrides,
                        composed_directives: convert_directives(composed_directives, &mut directives),
                        description,
                    },
                )
                .collect(),
            enums: enums
                .into_iter()
                .map(
                    |super::v1::Enum {
                         name,
                         values,
                         composed_directives,
                         description,
                     }| Enum {
                        name,
                        values: convert_enum_values(values, &mut enum_values, &mut directives),
                        composed_directives: convert_directives(composed_directives, &mut directives),
                        description,
                    },
                )
                .collect(),
            unions: unions
                .into_iter()
                .map(
                    |super::v1::Union {
                         name,
                         members,
                         composed_directives,
                         description,
                     }| Union {
                        name,
                        members,
                        composed_directives: convert_directives(composed_directives, &mut directives),
                        description,
                    },
                )
                .collect(),
            scalars: scalars
                .into_iter()
                .map(
                    |super::v1::Scalar {
                         name,
                         composed_directives,
                         description,
                     }| Scalar {
                        name,
                        composed_directives: convert_directives(composed_directives, &mut directives),
                        description,
                    },
                )
                .collect(),
            input_objects: input_objects
                .into_iter()
                .map(
                    |super::v1::InputObject {
                         name,
                         fields,
                         composed_directives,
                         description,
                     }| InputObject {
                        name,
                        fields: convert_input_object_fields(fields, &mut input_value_definitions, &mut directives),
                        composed_directives: convert_directives(composed_directives, &mut directives),
                        description,
                    },
                )
                .collect(),
            input_value_definitions,
            strings,
            field_types,
            directives,
            enum_values,
        }
    }
}

impl From<(super::v1::Value, &[String])> for Value {
    fn from((value, strings): (super::v1::Value, &[String])) -> Self {
        match value {
            super::v1::Value::String(v) => Value::String(v),
            super::v1::Value::Int(v) => Value::Int(v),
            super::v1::Value::Float(v) => Value::Float(strings[v.0].parse().unwrap()),
            super::v1::Value::Boolean(v) => Value::Boolean(v),
            super::v1::Value::EnumValue(v) => Value::EnumValue(v),
            super::v1::Value::Object(v) => {
                Value::Object(v.into_iter().map(|(k, v)| (k, (v, strings).into())).collect())
            }
            super::v1::Value::List(v) => Value::List(v.into_iter().map(|v| (v, strings).into()).collect()),
        }
    }
}