Skip to main content

apollo_federation_types/javascript/
mod.rs

1//! This module contains types matching those in the JavaScript `@apollo/composition` package.
2
3use apollo_federation::subgraph::typestate::{Initial, Subgraph, Validated};
4use apollo_federation::subgraph::SubgraphError;
5use serde::{Deserialize, Serialize};
6
7/// The `SubgraphDefinition` represents everything we need to know about a
8/// subgraph for its GraphQL runtime responsibilities.
9#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
10pub struct SubgraphDefinition {
11    /// The name of the subgraph. We use this name internally to
12    /// in the representation of the composed schema and for designations
13    /// within the human-readable QueryPlan.
14    pub name: String,
15
16    /// The routing/runtime URL where the subgraph can be found that will
17    /// be able to fulfill the requests it is responsible for.
18    pub url: String,
19
20    /// The Schema Definition Language (SDL) containing the type definitions
21    /// for a subgraph.
22    pub sdl: String,
23}
24
25/// The structure returned by `validateSatisfiability` in `@apollo/composition`
26#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
27pub struct SatisfiabilityResult {
28    pub errors: Option<Vec<GraphQLError>>,
29    pub hints: Option<Vec<CompositionHint>>,
30}
31
32#[derive(Debug, Clone, Eq, Hash, PartialEq, Deserialize, Serialize)]
33pub struct CompositionHint {
34    pub message: String,
35    pub nodes: Option<Vec<SubgraphASTNode>>,
36    pub definition: HintCodeDefinition,
37}
38
39#[derive(Debug, Clone, Eq, Hash, PartialEq, Deserialize, Serialize)]
40pub struct HintCodeDefinition {
41    pub code: String,
42}
43
44#[derive(Debug, Clone, Eq, Hash, PartialEq, Deserialize, Serialize)]
45pub struct SubgraphASTNode {
46    pub loc: Option<Location>,
47    pub subgraph: Option<String>,
48}
49
50#[derive(Debug, Clone, Eq, Hash, PartialEq, Deserialize, Serialize)]
51#[serde(rename_all = "camelCase")]
52pub struct Location {
53    pub start_token: Token,
54    pub end_token: Token,
55}
56
57#[derive(Debug, Clone, Eq, Hash, PartialEq, Deserialize, Serialize)]
58pub struct Token {
59    pub column: Option<usize>,
60    pub line: Option<usize>,
61}
62
63#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
64pub struct GraphQLError {
65    pub message: String,
66    pub nodes: Option<Vec<SubgraphASTNode>>,
67    pub extensions: Option<GraphQLErrorExtensions>,
68}
69
70#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
71pub struct GraphQLErrorExtensions {
72    pub code: String,
73}
74
75impl TryFrom<SubgraphDefinition> for Subgraph<Initial> {
76    type Error = SubgraphError;
77
78    fn try_from(value: SubgraphDefinition) -> Result<Self, Self::Error> {
79        Subgraph::parse(value.name.as_str(), value.url.as_str(), value.sdl.as_str())
80    }
81}
82
83impl From<Subgraph<Validated>> for SubgraphDefinition {
84    fn from(value: Subgraph<Validated>) -> Self {
85        SubgraphDefinition {
86            sdl: value.schema_string(),
87            name: value.name,
88            url: value.url,
89        }
90    }
91}