forked from graphprotocol/graph-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.rs
More file actions
162 lines (144 loc) · 5.66 KB
/
Copy pathschema.rs
File metadata and controls
162 lines (144 loc) · 5.66 KB
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
use crate::data::graphql::validation::{
get_object_type_definitions, validate_interface_implementation, validate_schema,
SchemaValidationError,
};
use crate::data::subgraph::SubgraphDeploymentId;
use failure::Error;
use graphql_parser;
use graphql_parser::{
query::Name,
schema::{self, InterfaceType, ObjectType, TypeDefinition},
Pos,
};
use std::collections::BTreeMap;
use std::iter::FromIterator;
/// A validated and preprocessed GraphQL schema for a subgraph.
#[derive(Clone, Debug, PartialEq)]
pub struct Schema {
pub id: SubgraphDeploymentId,
pub document: schema::Document,
// Maps type name to implemented interfaces.
interfaces_for_type: BTreeMap<Name, Vec<InterfaceType>>,
// Maps an interface name to the list of entities that implement it.
types_for_interface: BTreeMap<Name, Vec<ObjectType>>,
}
impl Schema {
pub fn parse(raw: &str, id: SubgraphDeploymentId) -> Result<Self, Error> {
let document = graphql_parser::parse_schema(&raw)?;
validate_schema(&document)?;
// Initialize with an empty vec for each interface, so we don't
// miss interfaces that have no implementors.
let mut types_for_interface =
BTreeMap::from_iter(document.definitions.iter().filter_map(|d| match d {
schema::Definition::TypeDefinition(TypeDefinition::Interface(t)) => {
Some((t.name.clone(), vec![]))
}
_ => None,
}));
let mut interfaces_for_type = BTreeMap::<_, Vec<_>>::new();
for object_type in get_object_type_definitions(&document) {
for implemented_interface in object_type.implements_interfaces.clone() {
let interface_type = document
.definitions
.iter()
.find_map(|def| match def {
schema::Definition::TypeDefinition(TypeDefinition::Interface(i))
if i.name == implemented_interface =>
{
Some(i.clone())
}
_ => None,
})
.ok_or_else(|| {
SchemaValidationError::UndefinedInterface(implemented_interface.clone())
})?;
validate_interface_implementation(object_type, &interface_type)?;
interfaces_for_type
.entry(object_type.name.clone())
.or_default()
.push(interface_type);
types_for_interface
.get_mut(&implemented_interface)
.unwrap()
.push(object_type.clone());
}
}
let mut schema = Schema {
id: id.clone(),
document,
interfaces_for_type,
types_for_interface,
};
schema.add_subgraph_id_directives(id);
Ok(schema)
}
/// Returned map has one an entry for each interface in the schema.
pub fn types_for_interface(&self) -> &BTreeMap<Name, Vec<ObjectType>> {
&self.types_for_interface
}
/// Returns `None` if the type implements no interfaces.
pub fn interfaces_for_type(&self, type_name: &Name) -> Option<&Vec<InterfaceType>> {
self.interfaces_for_type.get(type_name)
}
// Adds a @subgraphId(id: ...) directive to object/interface/enum types in the schema.
fn add_subgraph_id_directives(&mut self, id: SubgraphDeploymentId) {
for definition in self.document.definitions.iter_mut() {
let subgraph_id_argument = (
schema::Name::from("id"),
schema::Value::String(id.to_string()),
);
let subgraph_id_directive = schema::Directive {
name: "subgraphId".to_string(),
position: Pos::default(),
arguments: vec![subgraph_id_argument],
};
if let schema::Definition::TypeDefinition(ref mut type_definition) = definition {
match type_definition {
TypeDefinition::Object(ref mut object_type) => {
object_type.directives.push(subgraph_id_directive);
}
TypeDefinition::Interface(ref mut interface_type) => {
interface_type.directives.push(subgraph_id_directive);
}
TypeDefinition::Enum(ref mut enum_type) => {
enum_type.directives.push(subgraph_id_directive);
}
TypeDefinition::Scalar(_scalar_type) => (),
TypeDefinition::InputObject(_input_object_type) => (),
TypeDefinition::Union(_union_type) => (),
}
};
}
}
}
#[test]
fn non_existing_interface() {
let schema = "type Foo implements Bar @entity { foo: Int }";
let res = Schema::parse(schema, SubgraphDeploymentId::new("dummy").unwrap());
let error = res
.unwrap_err()
.downcast::<SchemaValidationError>()
.unwrap();
assert_eq!(
error,
SchemaValidationError::UndefinedInterface("Bar".to_owned())
);
}
#[test]
fn invalid_interface_implementation() {
let schema = "
interface Foo {
x: Int,
y: Int
}
type Bar implements Foo @entity {
x: Boolean
}
";
let res = Schema::parse(schema, SubgraphDeploymentId::new("dummy").unwrap());
assert_eq!(
res.unwrap_err().to_string(),
"Entity type `Bar` cannot implement `Foo` because it is missing the \
required fields: x: Int, y: Int"
);
}