apollo-federation 2.13.1

Apollo Federation
Documentation
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
//! Parsing and validation for `@connect(http:)`

use std::fmt::Display;
use std::str::FromStr;

use apollo_compiler::Name;
use apollo_compiler::Node;
use apollo_compiler::ast::Value;
use multi_try::MultiTry;
use shape::Shape;

use crate::connectors::HTTPMethod;
use crate::connectors::Namespace;
use crate::connectors::SourceName;
use crate::connectors::spec::connect::CONNECT_BODY_ARGUMENT_NAME;
use crate::connectors::spec::connect::CONNECT_SOURCE_ARGUMENT_NAME;
use crate::connectors::spec::http::HTTP_ARGUMENT_NAME;
use crate::connectors::string_template;
use crate::connectors::string_template::Part;
use crate::connectors::string_template::StringTemplate;
use crate::connectors::validation::Code;
use crate::connectors::validation::Message;
use crate::connectors::validation::coordinates::ConnectDirectiveCoordinate;
use crate::connectors::validation::coordinates::ConnectHTTPCoordinate;
use crate::connectors::validation::coordinates::HttpHeadersCoordinate;
use crate::connectors::validation::coordinates::HttpMethodCoordinate;
use crate::connectors::validation::expression;
use crate::connectors::validation::expression::Context;
use crate::connectors::validation::expression::MappingArgument;
use crate::connectors::validation::expression::parse_mapping_argument;
use crate::connectors::validation::expression::scalars;
use crate::connectors::validation::graphql::SchemaInfo;
use crate::connectors::validation::graphql::subslice_location;
use crate::connectors::validation::http::UrlProperties;
use crate::connectors::validation::http::headers::Headers;
use crate::connectors::validation::http::url::validate_url_scheme;

/// A valid, parsed (but not type-checked) `@connect(http:)`.
///
/// TODO: Use this when creating a `HttpJsonTransport` as well.
pub(super) struct Http<'schema> {
    transport: Transport<'schema>,
    body: Option<Body<'schema>>,
    headers: Headers<'schema>,
}

impl<'schema> Http<'schema> {
    /// Parse the `@connect(http:)` argument and run just enough checks to be able to use the
    /// argument at runtime. More advanced checks are done in [`Self::type_check`].
    ///
    /// Three sub-pieces are always parsed, and the errors from _all_ of those pieces are returned
    /// together in the event of failure:
    /// 1. `http.body` with [`Body::parse`]
    /// 2. `http.headers` with [`Headers::parse`]
    /// 3. `http.<METHOD>` (for example, `http.GET`) with [`Transport::parse`]
    ///
    /// The order these pieces run in doesn't matter and shouldn't affect the output.
    pub(super) fn parse(
        coordinate: ConnectDirectiveCoordinate<'schema>,
        source_name: Option<&SourceName>,
        schema: &'schema SchemaInfo,
    ) -> Result<Self, Vec<Message>> {
        let Some((http_arg, http_arg_node)) = coordinate
            .directive
            .specified_argument_by_name(&HTTP_ARGUMENT_NAME)
            .and_then(|arg| Some((arg.as_object()?, arg)))
        else {
            return Err(vec![Message {
                code: Code::GraphQLError,
                message: format!("{coordinate} must have a `{HTTP_ARGUMENT_NAME}` argument."),
                locations: coordinate
                    .directive
                    .line_column_range(&schema.sources)
                    .into_iter()
                    .collect(),
            }]);
        };

        Body::parse(http_arg, coordinate, schema)
            .map_err(|err| vec![err])
            .and_try(Headers::parse(
                http_arg,
                HttpHeadersCoordinate::Connect {
                    connect: coordinate,
                },
                schema,
            ))
            .and_try(Transport::parse(
                http_arg,
                ConnectHTTPCoordinate::from(coordinate),
                http_arg_node,
                source_name,
                schema,
            ))
            .map_err(|nested| nested.into_iter().flatten().collect())
            .map(|(body, headers, transport)| Self {
                body,
                headers,
                transport,
            })
    }

    /// Type-check the `@connect(http:)` directive.
    ///
    /// Does things like ensuring that every accessed variable actually exists and that expressions
    /// used in the URL and headers result in scalars.
    ///
    /// TODO: Return some type checking results, like extracted keys?
    pub(super) fn type_check(self, schema: &SchemaInfo) -> Result<(), Vec<Message>> {
        let Self {
            transport,
            body,
            headers,
        } = self;

        let mut errors = Vec::new();
        if let Some(body) = body {
            errors.extend(body.type_check(schema).err());
        }

        errors.extend(headers.type_check(schema).err().into_iter().flatten());
        errors.extend(transport.type_check(schema));

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    pub(super) fn variables(&self) -> impl Iterator<Item = Namespace> + '_ {
        self.transport
            .url
            .expressions()
            .flat_map(|e| {
                e.expression
                    .variable_references()
                    .map(|var_ref| var_ref.namespace.namespace)
            })
            .chain(self.body.as_ref().into_iter().flat_map(|b| {
                b.mapping
                    .variable_references()
                    .map(|var_ref| var_ref.namespace.namespace)
            }))
    }
}

struct Body<'schema> {
    mapping: MappingArgument,
    coordinate: BodyCoordinate<'schema>,
}

impl<'schema> Body<'schema> {
    pub(super) fn parse(
        http_arg: &'schema [(Name, Node<Value>)],
        connect: ConnectDirectiveCoordinate<'schema>,
        schema: &'schema SchemaInfo,
    ) -> Result<Option<Self>, Message> {
        let Some((_, value)) = http_arg
            .iter()
            .find(|(name, _)| name == &CONNECT_BODY_ARGUMENT_NAME)
        else {
            return Ok(None);
        };
        let coordinate = BodyCoordinate { connect };

        let mapping = parse_mapping_argument(value, coordinate, Code::InvalidBody, schema)?;

        Ok(Some(Self {
            mapping,
            coordinate,
        }))
    }

    /// Check that the selection of the body matches the inputs at this location.
    ///
    /// TODO: check keys here?
    pub(super) fn type_check(self, schema: &SchemaInfo) -> Result<(), Message> {
        let Self {
            mapping,
            coordinate,
        } = self;
        expression::validate(
            &mapping.expression,
            &Context::for_connect_request(
                schema,
                coordinate.connect,
                &mapping.node,
                Code::InvalidBody,
            ),
            &Shape::unknown([]),
        )
        .map_err(|mut message| {
            message.message = format!("In {coordinate}: {message}", message = message.message);
            message
        })
    }
}

#[derive(Clone, Copy)]
struct BodyCoordinate<'schema> {
    connect: ConnectDirectiveCoordinate<'schema>,
}

impl Display for BodyCoordinate<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "`@{connect_directive_name}({HTTP_ARGUMENT_NAME}: {{{CONNECT_BODY_ARGUMENT_NAME}:}})` on `{element}`",
            connect_directive_name = self.connect.directive.name,
            element = self.connect.element
        )
    }
}

/// The `@connect(http.<METHOD>:)` arg
struct Transport<'schema> {
    // TODO: once this is shared with `HttpJsonTransport`, this will be used
    #[allow(dead_code)]
    method: HTTPMethod,
    url: StringTemplate,
    coordinate: HttpMethodCoordinate<'schema>,

    url_properties: UrlProperties<'schema>,
}

impl<'schema> Transport<'schema> {
    fn parse(
        http_arg: &'schema [(Name, Node<Value>)],
        coordinate: ConnectHTTPCoordinate<'schema>,
        http_arg_node: &Node<Value>,
        source_name: Option<&SourceName>,
        schema: &'schema SchemaInfo<'schema>,
    ) -> Result<Self, Vec<Message>> {
        let source_map = &schema.sources;
        let mut methods = http_arg
            .iter()
            .filter_map(|(method, value)| {
                HTTPMethod::from_str(method)
                    .ok()
                    .map(|method| (method, value))
            })
            .peekable();

        let Some((method, method_value)) = methods.next() else {
            return Err(vec![Message {
                code: Code::MissingHttpMethod,
                message: format!("{coordinate} must specify an HTTP method."),
                locations: http_arg_node
                    .line_column_range(source_map)
                    .into_iter()
                    .collect(),
            }]);
        };

        if methods.peek().is_some() {
            let locations = method_value
                .line_column_range(source_map)
                .into_iter()
                .chain(methods.filter_map(|(_, node)| node.line_column_range(source_map)))
                .collect();
            return Err(vec![Message {
                code: Code::MultipleHttpMethods,
                message: format!("{coordinate} cannot specify more than one HTTP method."),
                locations,
            }]);
        }

        let url_properties = UrlProperties::parse_for_connector(
            coordinate.connect_directive_coordinate,
            schema,
            http_arg,
        )?;

        let coordinate = HttpMethodCoordinate {
            connect: coordinate.connect_directive_coordinate,
            method,
            node: method_value,
        };

        let url_string = coordinate.node.as_str().ok_or_else(|| {
            vec![Message {
                code: Code::GraphQLError,
                message: format!("The value for {coordinate} must be a string."),
                locations: coordinate
                    .node
                    .line_column_range(&schema.sources)
                    .into_iter()
                    .collect(),
            }]
        })?;
        let url = StringTemplate::parse_with_spec(url_string, schema.connect_link.spec)
            .map_err(|string_template::Error { message, location }| Message {
                code: Code::InvalidUrl,
                message: format!("In {coordinate}: {message}"),
                locations: subslice_location(coordinate.node, location, schema)
                    .into_iter()
                    .collect(),
            })
            .map_err(|e| vec![e])?;

        if source_name.is_some() {
            return if url_string.starts_with("http://") || url_string.starts_with("https://") {
                Err(vec![Message {
                    code: Code::AbsoluteConnectUrlWithSource,
                    message: format!(
                        "{coordinate} contains the absolute URL {raw_value} while also specifying a `{CONNECT_SOURCE_ARGUMENT_NAME}`. Either remove the `{CONNECT_SOURCE_ARGUMENT_NAME}` argument or change the URL to be relative.",
                        raw_value = coordinate.node
                    ),
                    locations: coordinate
                        .node
                        .line_column_range(source_map)
                        .into_iter()
                        .collect(),
                }])
            } else {
                Ok(Self {
                    method,
                    url,
                    coordinate,
                    url_properties,
                })
            };
        } else {
            validate_absolute_connect_url(&url, coordinate, coordinate.node, schema)
                .map_err(|e| vec![e])?;
        }
        Ok(Self {
            method,
            url,
            coordinate,
            url_properties,
        })
    }

    /// Type-check the `@connect(http:<METHOD>:)` directive.
    ///
    /// TODO: Return input shapes for keys instead reparsing for `Connector::resolvable_key` later
    fn type_check(self, schema: &SchemaInfo) -> Vec<Message> {
        let expression_context = Context::for_connect_request(
            schema,
            self.coordinate.connect,
            self.coordinate.node,
            Code::InvalidUrl,
        );

        let mut messages = Vec::new();
        for expression in self.url.expressions() {
            messages.extend(
                expression::validate(expression, &expression_context, &scalars())
                    .err()
                    .into_iter()
                    .map(|mut err| {
                        err.message = format!(
                            "In {coordinate}: {}",
                            err.message,
                            coordinate = self.coordinate
                        );
                        err
                    }),
            );
        }

        messages.extend(self.url_properties.type_check(schema));

        messages
    }
}

/// Additional validation rules when using `@connect` without `source:`
fn validate_absolute_connect_url(
    url: &StringTemplate,
    coordinate: HttpMethodCoordinate,
    value: &Node<Value>,
    schema: &SchemaInfo,
) -> Result<(), Message> {
    let mut is_relative = true;
    let mut dynamic_in_domain = None;

    // Check each part of the string template that *should* result in a static, valid base URI (scheme+host+port).
    // - if we don't encounter a scheme, it's not a valid absolute URL
    // - once we encounter a character that ends the authority (starts the path, query, or fragment) we break
    // - if we encounter a dynamic part before we break, we have an illegal dynamic component
    for part in &url.parts {
        match part {
            Part::Constant(constant) => {
                let value = match constant.value.split_once("://") {
                    Some((_scheme, rest)) => {
                        is_relative = false;
                        rest
                    }
                    None => &constant.value,
                };
                if value.contains('/') || value.contains('?') || value.contains('#') {
                    break;
                }
            }
            Part::Expression(dynamic) => {
                dynamic_in_domain = Some(dynamic);
            }
        }
    }

    if is_relative {
        return Err(Message {
            code: Code::RelativeConnectUrlWithoutSource,
            message: format!(
                "{coordinate} specifies the relative URL {raw_value}, but no `{CONNECT_SOURCE_ARGUMENT_NAME}` is defined. Either use an absolute URL including scheme (e.g. https://), or add a `@{source_directive_name}`.",
                raw_value = coordinate.node,
                source_directive_name = schema.source_directive_name(),
            ),
            locations: coordinate
                .node
                .line_column_range(&schema.sources)
                .into_iter()
                .collect(),
        });
    }
    if let Some(dynamic) = dynamic_in_domain {
        return Err(Message {
            code: Code::InvalidUrl,
            message: format!(
                "{coordinate} must not contain dynamic pieces in the domain section (before the first `/` or `?`).",
            ),
            locations: subslice_location(value, dynamic.location.clone(), schema)
                .into_iter()
                .collect(),
        });
    }

    validate_url_scheme(url, coordinate, value, schema)?;

    Ok(())
}