shape/
graphql.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
//! Helper functions and impls for creating [`Shape`]s from [`apollo_compiler`] types.

use crate::location::{Location, SourceId};
use crate::Shape;
use apollo_compiler::ast::{FieldDefinition, Type};
use apollo_compiler::collections::IndexMap;
use apollo_compiler::parser::SourceSpan;
use apollo_compiler::schema::{
    Component, EnumType, ExtendedType, InputObjectType, InterfaceType, ObjectType, UnionType,
};
use apollo_compiler::{Name, Node, Schema};

/// Get all the shapes for a GraphQL [`Schema`], keyed by their GraphQL [`Name`].
///
/// Locations will be automatically set with
#[must_use]
pub fn shapes_for_schema(schema: &Schema) -> IndexMap<&str, Shape> {
    schema
        .types
        .iter()
        .filter(|&(_name, ty)| !ty.is_built_in())
        .map(|(name, ty)| (name.as_str(), Shape::from(ty)))
        .collect()
}

#[must_use]
pub fn shape_for_arguments(field_definition: &Component<FieldDefinition>) -> Shape {
    // Accumulate the locations of all the arguments so they can be highlighted together
    let mut source_span = None;
    for next_source_span in field_definition.arguments.iter().map(Node::location) {
        source_span = SourceSpan::recompose(source_span, next_source_span);
    }
    Shape::record(
        field_definition
            .arguments
            .iter()
            .map(|arg| {
                (
                    arg.name.to_string(),
                    from_type(arg.ty.as_ref(), span(arg.location())),
                )
            })
            .collect(),
        span(source_span),
    )
}

impl From<&ExtendedType> for Shape {
    fn from(ext: &ExtendedType) -> Self {
        match ext {
            ExtendedType::Scalar(scalar) => Shape::unknown(span(scalar.location())),
            ExtendedType::Object(obj) => Self::from(obj),
            ExtendedType::Interface(intf) => Self::from(intf),
            ExtendedType::Union(union) => Self::from(union),
            ExtendedType::Enum(enm) => Self::from(enm),
            ExtendedType::InputObject(input) => Self::from(input),
        }
    }
}

impl From<&Name> for Shape {
    fn from(name: &Name) -> Self {
        let locations = span(name.location());
        match name.as_str() {
            "String" | "ID" => Self::string(locations),
            "Int" => Self::int(locations),
            "Float" => Self::float(locations),
            "Boolean" => Self::bool(locations),
            other => Self::name(other, locations),
        }
    }
}

impl From<&Node<ObjectType>> for Shape {
    fn from(object_type: &Node<ObjectType>) -> Self {
        record(&object_type.fields, span(object_type.location()))
    }
}

fn record(fields: &IndexMap<Name, Component<FieldDefinition>>, locations: Vec<Location>) -> Shape {
    let fields = fields
        .iter()
        .map(|(key, value)| (key.to_string(), Shape::from(value)))
        .collect();
    Shape::record(fields, locations)
}

fn nullable(shape: Shape, locations: Vec<Location>) -> Shape {
    Shape::one([shape, Shape::null(locations.clone())], locations)
}

impl From<&Component<FieldDefinition>> for Shape {
    fn from(field: &Component<FieldDefinition>) -> Self {
        let locations = span(field.location());
        from_type(&field.ty, locations)
    }
}

fn from_type(ty: &Type, locations: Vec<Location>) -> Shape {
    match ty {
        Type::Named(name) => nullable(Shape::from(name), locations),
        Type::NonNullNamed(name) => Shape::from(name),
        Type::List(ty) => nullable(
            Shape::list(from_type(ty.as_ref(), locations.clone()), locations.clone()),
            locations,
        ),
        Type::NonNullList(ty) => Shape::list(from_type(ty.as_ref(), locations.clone()), locations),
    }
}

impl From<&Node<InterfaceType>> for Shape {
    fn from(interface_type: &Node<InterfaceType>) -> Self {
        record(&interface_type.fields, span(interface_type.location()))
    }
}

impl From<&Node<UnionType>> for Shape {
    fn from(union_type: &Node<UnionType>) -> Self {
        Self::one(
            union_type
                .members
                .iter()
                .map(|member| Self::from(&member.name)),
            span(union_type.location()),
        )
    }
}

impl From<&Node<EnumType>> for Shape {
    fn from(enum_type: &Node<EnumType>) -> Self {
        Shape::one(
            enum_type
                .values
                .iter()
                .map(|(name, _)| Shape::string_value(name.as_str(), span(name.location()))),
            span(enum_type.location()),
        )
    }
}

impl From<&Node<InputObjectType>> for Shape {
    fn from(input_object_type: &Node<InputObjectType>) -> Self {
        Self::record(
            input_object_type
                .fields
                .iter()
                .map(|(name, input)| {
                    (
                        name.to_string(),
                        from_type(input.ty.as_ref(), span(input.location())),
                    )
                })
                .collect(),
            span(input_object_type.location()),
        )
    }
}

fn span(source_span: Option<SourceSpan>) -> Vec<Location> {
    source_span.into_iter().map(Location::from).collect()
}

impl From<SourceSpan> for Location {
    fn from(span: SourceSpan) -> Self {
        Location {
            source_id: SourceId::GraphQL(span.file_id()),
            span: span.offset()..span.end_offset(),
        }
    }
}