graphql_client_codegen/schema/
json_conversion.rs

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
use super::{Schema, TypeId};
use graphql_introspection_query::introspection_response::{
    FullType, IntrospectionResponse, Schema as JsonSchema, TypeRef, __TypeKind,
};

pub(super) fn build_schema(src: IntrospectionResponse) -> Schema {
    let mut src = src.into_schema().schema.expect("could not find schema");
    let mut schema = Schema::new();
    build_names_map(&mut src, &mut schema);
    convert(&mut src, &mut schema);

    schema
}

fn build_names_map(src: &mut JsonSchema, schema: &mut Schema) {
    let names = &mut schema.names;

    unions_mut(src)
        .map(|u| u.name.as_ref().expect("union name"))
        .enumerate()
        .for_each(|(idx, name)| {
            names.insert(name.clone(), TypeId::union(idx));
        });

    interfaces_mut(src)
        .map(|iface| iface.name.as_ref().expect("interface name"))
        .enumerate()
        .for_each(|(idx, name)| {
            names.insert(name.clone(), TypeId::interface(idx));
        });

    objects_mut(src)
        .map(|obj| obj.name.as_ref().expect("object name"))
        .enumerate()
        .for_each(|(idx, name)| {
            names.insert(name.clone(), TypeId::object(idx as u32));
        });

    inputs_mut(src)
        .map(|obj| obj.name.as_ref().expect("input name"))
        .enumerate()
        .for_each(|(idx, name)| {
            names.insert(name.clone(), TypeId::input(idx as u32));
        });
}

fn convert(src: &mut JsonSchema, schema: &mut Schema) {
    for scalar in scalars_mut(src) {
        ingest_scalar(schema, scalar);
    }

    for enm in enums_mut(src) {
        ingest_enum(schema, enm)
    }

    for interface in interfaces_mut(src) {
        ingest_interface(schema, interface);
    }

    for object in objects_mut(src) {
        ingest_object(schema, object);
    }

    for unn in unions_mut(src) {
        ingest_union(schema, unn)
    }

    for input in inputs_mut(src) {
        ingest_input(schema, input);
    }

    // Define the root operations.
    {
        schema.query_type = src
            .query_type
            .as_mut()
            .and_then(|n| n.name.as_mut())
            .and_then(|n| schema.names.get(n))
            .and_then(|id| id.as_object_id());
        schema.mutation_type = src
            .mutation_type
            .as_mut()
            .and_then(|n| n.name.as_mut())
            .and_then(|n| schema.names.get(n))
            .and_then(|id| id.as_object_id());
        schema.subscription_type = src
            .subscription_type
            .as_mut()
            .and_then(|n| n.name.as_mut())
            .and_then(|n| schema.names.get(n))
            .and_then(|id| id.as_object_id());
    }
}

fn types_mut(schema: &mut JsonSchema) -> impl Iterator<Item = &mut FullType> {
    schema
        .types
        .as_mut()
        .expect("schema.types.as_mut()")
        .iter_mut()
        .filter_map(|t| -> Option<&mut FullType> { t.as_mut().map(|f| &mut f.full_type) })
}

fn objects_mut(schema: &mut JsonSchema) -> impl Iterator<Item = &mut FullType> {
    types_mut(schema).filter(|t| t.kind == Some(__TypeKind::OBJECT))
}

fn enums_mut(schema: &mut JsonSchema) -> impl Iterator<Item = &mut FullType> {
    types_mut(schema).filter(|t| t.kind == Some(__TypeKind::ENUM))
}

fn interfaces_mut(schema: &mut JsonSchema) -> impl Iterator<Item = &mut FullType> {
    types_mut(schema).filter(|t| t.kind == Some(__TypeKind::INTERFACE))
}

fn unions_mut(schema: &mut JsonSchema) -> impl Iterator<Item = &mut FullType> {
    types_mut(schema).filter(|t| t.kind == Some(__TypeKind::UNION))
}

fn inputs_mut(schema: &mut JsonSchema) -> impl Iterator<Item = &mut FullType> {
    types_mut(schema).filter(|t| t.kind == Some(__TypeKind::INPUT_OBJECT))
}

fn scalars_mut(schema: &mut JsonSchema) -> impl Iterator<Item = &mut FullType> {
    types_mut(schema).filter(|t| {
        t.kind == Some(__TypeKind::SCALAR)
            && !super::DEFAULT_SCALARS.contains(&t.name.as_deref().expect("FullType.name"))
    })
}

fn ingest_scalar(schema: &mut Schema, scalar: &mut FullType) {
    let name: String = scalar.name.take().expect("scalar.name");
    let names_name = name.clone();

    let id = schema.push_scalar(super::StoredScalar { name });

    schema.names.insert(names_name, TypeId::Scalar(id));
}

fn ingest_enum(schema: &mut Schema, enm: &mut FullType) {
    let name = enm.name.take().expect("enm.name");
    let names_name = name.clone();

    let variants = enm
        .enum_values
        .as_mut()
        .expect("enm.enum_values.as_mut()")
        .iter_mut()
        .map(|v| {
            std::mem::take(
                v.name
                    .as_mut()
                    .take()
                    .expect("variant.name.as_mut().take()"),
            )
        })
        .collect();

    let enm = super::StoredEnum { name, variants };

    let id = schema.push_enum(enm);

    schema.names.insert(names_name, TypeId::Enum(id));
}

fn ingest_interface(schema: &mut Schema, iface: &mut FullType) {
    let interface_id = schema
        .find_type_id(iface.name.as_ref().expect("iface.name"))
        .as_interface_id()
        .expect("iface type id as interface id");
    let fields = iface.fields.as_mut().expect("interface.fields");
    let mut field_ids = Vec::with_capacity(fields.len());

    for field in fields.iter_mut() {
        let field = super::StoredField {
            parent: super::StoredFieldParent::Interface(interface_id),
            name: field.name.take().expect("take field name"),
            r#type: resolve_field_type(
                schema,
                &mut field.type_.as_mut().expect("take field type").type_ref,
            ),
            deprecation: if let Some(true) = field.is_deprecated {
                Some(field.deprecation_reason.clone())
            } else {
                None
            },
        };

        field_ids.push(schema.push_field(field));
    }

    let interface = super::StoredInterface {
        name: std::mem::take(iface.name.as_mut().expect("iface.name.as_mut")),
        fields: field_ids,
    };

    schema.push_interface(interface);
}

fn ingest_object(schema: &mut Schema, object: &mut FullType) {
    let object_id = schema
        .find_type_id(object.name.as_ref().expect("object.name"))
        .as_object_id()
        .expect("ingest_object > as_object_id");

    let fields = object.fields.as_mut().expect("object.fields.as_mut()");
    let mut field_ids = Vec::with_capacity(fields.len());

    for field in fields.iter_mut() {
        let field = super::StoredField {
            parent: super::StoredFieldParent::Object(object_id),
            name: field.name.take().expect("take field name"),
            r#type: resolve_field_type(
                schema,
                &mut field.type_.as_mut().expect("take field type").type_ref,
            ),
            deprecation: if let Some(true) = field.is_deprecated {
                Some(field.deprecation_reason.clone())
            } else {
                None
            },
        };

        field_ids.push(schema.push_field(field));
    }

    let object = super::StoredObject {
        name: object.name.take().expect("take object name"),
        implements_interfaces: object
            .interfaces
            .as_ref()
            .map(|ifaces| {
                ifaces
                    .iter()
                    .map(|iface| {
                        schema
                            .names
                            .get(iface.type_ref.name.as_ref().unwrap())
                            .and_then(|type_id| type_id.as_interface_id())
                            .ok_or_else(|| {
                                format!(
                                    "Unknown interface: {}",
                                    iface.type_ref.name.as_ref().unwrap()
                                )
                            })
                            .unwrap()
                    })
                    .collect()
            })
            .unwrap_or_else(Vec::new),
        fields: field_ids,
    };

    schema.push_object(object);
}

fn ingest_union(schema: &mut Schema, union: &mut FullType) {
    let variants = union
        .possible_types
        .as_ref()
        .expect("union.possible_types")
        .iter()
        .map(|variant| {
            schema.find_type_id(
                variant
                    .type_ref
                    .name
                    .as_ref()
                    .expect("variant.type_ref.name"),
            )
        })
        .collect();
    let un = super::StoredUnion {
        name: union.name.take().expect("union.name.take"),
        variants,
    };

    schema.stored_unions.push(un);
}

fn ingest_input(schema: &mut Schema, input: &mut FullType) {
    let mut fields = Vec::new();

    for field in input
        .input_fields
        .as_mut()
        .expect("Missing input_fields on input")
        .iter_mut()
    {
        fields.push((
            std::mem::take(&mut field.input_value.name),
            resolve_input_field_type(schema, &mut field.input_value.type_),
        ));
    }

    let input = super::StoredInputType {
        fields,
        name: input.name.take().expect("Input without a name"),
        // The one-of input spec is not stable yet, thus the introspection query does not have
        // `isOneOf`, so this is always false.
        is_one_of: false,
    };

    schema.stored_inputs.push(input);
}

fn resolve_field_type(schema: &mut Schema, typeref: &mut TypeRef) -> super::StoredFieldType {
    from_json_type_inner(schema, typeref)
}

fn resolve_input_field_type(
    schema: &mut Schema,
    typeref: &mut TypeRef,
) -> super::StoredInputFieldType {
    let field_type = from_json_type_inner(schema, typeref);

    super::StoredInputFieldType {
        id: field_type.id,
        qualifiers: field_type.qualifiers,
    }
}

fn json_type_qualifiers_depth(typeref: &mut TypeRef) -> usize {
    use graphql_introspection_query::introspection_response::*;

    match (typeref.kind.as_mut(), typeref.of_type.as_mut()) {
        (Some(__TypeKind::NON_NULL), Some(inner)) => 1 + json_type_qualifiers_depth(inner),
        (Some(__TypeKind::LIST), Some(inner)) => 1 + json_type_qualifiers_depth(inner),
        (Some(_), None) => 0,
        _ => panic!("Non-convertible type in JSON schema: {:?}", typeref),
    }
}

fn from_json_type_inner(schema: &mut Schema, inner: &mut TypeRef) -> super::StoredFieldType {
    use crate::type_qualifiers::GraphqlTypeQualifier;
    use graphql_introspection_query::introspection_response::*;

    let qualifiers_depth = json_type_qualifiers_depth(inner);
    let mut qualifiers = Vec::with_capacity(qualifiers_depth);

    let mut inner = inner;

    loop {
        match (
            inner.kind.as_mut(),
            inner.of_type.as_mut(),
            inner.name.as_mut(),
        ) {
            (Some(__TypeKind::NON_NULL), Some(new_inner), _) => {
                qualifiers.push(GraphqlTypeQualifier::Required);
                inner = new_inner.as_mut();
            }
            (Some(__TypeKind::LIST), Some(new_inner), _) => {
                qualifiers.push(GraphqlTypeQualifier::List);
                inner = new_inner.as_mut();
            }
            (Some(_), None, Some(name)) => {
                return super::StoredFieldType {
                    id: *schema.names.get(name).expect("schema.names.get(name)"),
                    qualifiers,
                }
            }
            _ => panic!("Non-convertible type in JSON schema"),
        }
    }
}