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
use std::ops::Range;

pub use super::v2::{
    Definition, DirectiveId, Directives, Enum, EnumId, EnumValue, EnumValueId, EnumValues, FieldId, FieldProvides,
    FieldRequires, FieldSet, FieldSetItem, InputObject, InputObjectId, InputValueDefinitionId, InputValueDefinitions,
    InterfaceId, Key, ObjectId, Override, OverrideLabel, OverrideSource, RootOperationTypes, Scalar, ScalarId,
    StringId, Subgraph, SubgraphId, Union, UnionId, Value, NO_DIRECTIVES, NO_ENUM_VALUE, NO_INPUT_VALUE_DEFINITION,
};
pub use wrapping::Wrapping;

/// 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 FederatedGraphV3 {
    pub subgraphs: Vec<Subgraph>,
    pub root_operation_types: RootOperationTypes,
    pub objects: Vec<Object>,
    pub interfaces: Vec<Interface>,
    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 composed directive instances (not definitions) in a federated graph.
    pub directives: Vec<Directive>,
}

impl std::fmt::Debug for FederatedGraphV3 {
    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, PartialOrd)]
pub enum Directive {
    Authenticated,
    Deprecated {
        reason: Option<StringId>,
    },
    Inaccessible,
    Policy(Vec<Vec<StringId>>),
    RequiresScopes(Vec<Vec<StringId>>),

    Other {
        name: StringId,
        arguments: Vec<(StringId, Value)>,
    },
}

#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)]
pub struct Type {
    pub wrapping: Wrapping,
    pub definition: Definition,
}

#[derive(serde::Serialize, serde::Deserialize, Clone)]
pub struct Field {
    pub name: StringId,
    pub r#type: Type,

    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.
    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>,

    pub keys: Vec<Key>,

    /// All directives that made it through composition.
    pub composed_directives: Directives,

    pub fields: Fields,

    #[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.
    pub keys: Vec<Key>,

    /// All directives that made it through composition.
    pub composed_directives: Directives,

    pub fields: Fields,

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

#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)]
pub struct InputValueDefinition {
    pub name: StringId,
    pub r#type: Type,
    pub directives: Directives,
    pub description: Option<StringId>,
}

/// A (start, end) range in FederatedGraph::fields.
pub type Fields = Range<FieldId>;

pub const NO_FIELDS: Fields = Range {
    start: FieldId(0),
    end: FieldId(0),
};

impl From<super::v2::FederatedGraphV2> for FederatedGraphV3 {
    fn from(mut value: super::v2::FederatedGraphV2) -> Self {
        let mut fields: Vec<Field> = value
            .fields
            .iter()
            .map(|field| Field {
                name: field.name,
                r#type: (&value[field.field_type_id]).into(),
                arguments: field.arguments,
                resolvable_in: field.resolvable_in.clone(),
                provides: field.provides.clone(),
                requires: field.requires.clone(),
                overrides: field.overrides.clone(),
                composed_directives: field.composed_directives,
                description: field.description,
            })
            .collect();

        let input_value_definitions = value
            .input_value_definitions
            .iter()
            .map(|input_value| InputValueDefinition {
                name: input_value.name,
                r#type: (&value[input_value.type_id]).into(),
                directives: input_value.directives,
                description: input_value.description,
            })
            .collect();

        let mut object_fields: Vec<Option<Fields>> = vec![None; value.objects.len()];
        let mut interface_fields: Vec<Option<Fields>> = vec![None; value.interfaces.len()];

        for object_field in value.object_fields {
            match &mut object_fields[object_field.object_id.0] {
                entry @ None => {
                    *entry = Some(Range {
                        start: object_field.field_id,
                        end: FieldId(object_field.field_id.0 + 1),
                    });
                }
                Some(fields) => {
                    fields.end = FieldId(object_field.field_id.0 + 1);
                }
            }
        }

        for interface_field in value.interface_fields {
            match &mut interface_fields[interface_field.interface_id.0] {
                entry @ None => {
                    *entry = Some(Range {
                        start: interface_field.field_id,
                        end: FieldId(interface_field.field_id.0 + 1),
                    });
                }
                Some(fields) => {
                    fields.end = FieldId(interface_field.field_id.0 + 1);
                }
            }
        }

        // In FederatedGraphV3, we reserve two fields for __schema and __type
        // on the root Query type. The space for them needs to be created. This
        // block is responsible for it.
        {
            let new_fields = ["__schema", "__type"].map(|needle| {
                value
                    .strings
                    .iter()
                    .position(|string| string == needle)
                    .map(StringId)
                    .unwrap_or_else(|| {
                        let idx = value.strings.len();
                        value.strings.push((*needle).to_owned());
                        StringId(idx)
                    })
            });

            let query_object_id = value.root_operation_types.query;
            let original_start = {
                let query_object_fields = object_fields[query_object_id.0].as_mut().expect("Query to have fields");
                let original_start = query_object_fields.start.0;
                query_object_fields.end.0 += 2;

                original_start
            };

            fields.splice(
                original_start..original_start,
                new_fields.into_iter().map(|name| Field {
                    name,
                    r#type: Type {
                        wrapping: Wrapping::new(false),
                        definition: Definition::Object(query_object_id),
                    },
                    arguments: NO_INPUT_VALUE_DEFINITION,
                    resolvable_in: Vec::new(),
                    provides: Vec::new(),
                    requires: Vec::new(),
                    overrides: Vec::new(),
                    composed_directives: NO_DIRECTIVES,
                    description: None,
                }),
            );

            fn correct_fieldset(original_start: usize, fieldset: &mut FieldSet) {
                for item in fieldset {
                    if item.field.0 >= original_start {
                        item.field.0 += 2;
                    }
                    correct_fieldset(original_start, &mut item.subselection);
                }
            }

            for object in &mut value.objects {
                for key in &mut object.keys {
                    correct_fieldset(original_start, &mut key.fields);
                }
            }

            for interface in &mut value.interfaces {
                for key in &mut interface.keys {
                    correct_fieldset(original_start, &mut key.fields);
                }
            }

            for field in &mut fields {
                for provides in &mut field.provides {
                    correct_fieldset(original_start, &mut provides.fields);
                }
                for requires in &mut field.requires {
                    correct_fieldset(original_start, &mut requires.fields);
                }
            }
        };

        FederatedGraphV3 {
            subgraphs: value.subgraphs,
            root_operation_types: value.root_operation_types,
            objects: value
                .objects
                .into_iter()
                .enumerate()
                .map(|(idx, object)| Object {
                    name: object.name,
                    implements_interfaces: object.implements_interfaces,
                    keys: object.keys,
                    composed_directives: object.composed_directives,
                    fields: object_fields[idx].as_ref().cloned().unwrap_or(NO_FIELDS),
                    description: object.description,
                })
                .collect(),
            interfaces: value
                .interfaces
                .into_iter()
                .enumerate()
                .map(|(idx, iface)| Interface {
                    name: iface.name,
                    implements_interfaces: iface.implements_interfaces,
                    keys: iface.keys,
                    composed_directives: iface.composed_directives,
                    fields: interface_fields[idx].as_ref().cloned().unwrap_or(NO_FIELDS),
                    description: iface.description,
                })
                .collect(),
            fields,
            enums: value.enums,
            unions: value.unions,
            scalars: value.scalars,
            input_objects: value.input_objects,
            enum_values: value.enum_values,
            input_value_definitions,
            strings: value.strings,
            directives: value
                .directives
                .into_iter()
                .map(|directive| match directive {
                    super::v2::Directive::Inaccessible => Directive::Inaccessible,
                    super::v2::Directive::Deprecated { reason } => Directive::Deprecated { reason },
                    super::v2::Directive::Other { name, arguments } => Directive::Other { name, arguments },
                })
                .collect(),
        }
    }
}

impl From<&super::v2::FieldType> for Type {
    fn from(value: &super::v2::FieldType) -> Self {
        let mut wrapping = Wrapping::new(value.inner_is_required);

        for wrapper in &value.list_wrappers {
            wrapping = match wrapper {
                super::v2::ListWrapper::RequiredList => wrapping.wrapped_by_required_list(),
                super::v2::ListWrapper::NullableList => wrapping.wrapped_by_nullable_list(),
            }
        }

        Type {
            wrapping,
            definition: value.kind,
        }
    }
}

impl std::ops::Index<Directives> for FederatedGraphV3 {
    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 FederatedGraphV3 {
    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 FederatedGraphV3 {
    type Output = [EnumValue];

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

impl std::ops::Index<Fields> for FederatedGraphV3 {
    type Output = [Field];

    fn index(&self, index: Fields) -> &Self::Output {
        let Range {
            start: FieldId(start),
            end: FieldId(end),
        } = index;
        &self.fields[start..end]
    }
}

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

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

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

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

impl Default for FederatedGraphV3 {
    fn default() -> Self {
        FederatedGraphV3 {
            subgraphs: Vec::new(),
            root_operation_types: RootOperationTypes {
                query: ObjectId(0),
                mutation: None,
                subscription: None,
            },
            objects: vec![Object {
                name: StringId(0),
                implements_interfaces: Vec::new(),
                keys: Vec::new(),
                composed_directives: NO_DIRECTIVES,
                fields: FieldId(0)..FieldId(2),
                description: None,
            }],
            interfaces: Vec::new(),
            fields: vec![
                Field {
                    name: StringId(1),
                    r#type: Type {
                        wrapping: Default::default(),
                        definition: Definition::Scalar(ScalarId(0)),
                    },
                    arguments: NO_INPUT_VALUE_DEFINITION,
                    resolvable_in: Vec::new(),
                    provides: Vec::new(),
                    requires: Vec::new(),
                    overrides: Vec::new(),
                    composed_directives: NO_DIRECTIVES,
                    description: None,
                },
                Field {
                    name: StringId(2),
                    r#type: Type {
                        wrapping: Default::default(),
                        definition: Definition::Scalar(ScalarId(0)),
                    },
                    arguments: NO_INPUT_VALUE_DEFINITION,
                    resolvable_in: Vec::new(),
                    provides: Vec::new(),
                    requires: Vec::new(),
                    overrides: Vec::new(),
                    composed_directives: NO_DIRECTIVES,
                    description: None,
                },
            ],
            enums: Vec::new(),
            unions: Vec::new(),
            scalars: Vec::new(),
            input_objects: Vec::new(),
            enum_values: Vec::new(),
            input_value_definitions: Vec::new(),
            strings: ["Query", "__type", "__schema"]
                .into_iter()
                .map(|string| string.to_owned())
                .collect(),
            directives: Vec::new(),
        }
    }
}