2020-03-05 00:39:56 +00:00
|
|
|
use crate::context::Data;
|
2020-03-04 06:24:44 +00:00
|
|
|
use crate::model::__DirectiveLocation;
|
|
|
|
use crate::registry::{Directive, InputValue, Registry};
|
2020-03-03 11:15:18 +00:00
|
|
|
use crate::types::QueryRoot;
|
2020-03-01 10:54:34 +00:00
|
|
|
use crate::{
|
2020-03-05 00:39:56 +00:00
|
|
|
ContextBase, ErrorWithPosition, GQLObject, GQLOutputValue, GQLType, QueryError,
|
2020-03-04 06:24:44 +00:00
|
|
|
QueryParseError, Result, Variables,
|
2020-03-01 10:54:34 +00:00
|
|
|
};
|
|
|
|
use graphql_parser::parse_query;
|
|
|
|
use graphql_parser::query::{Definition, OperationDefinition};
|
2020-03-05 00:39:56 +00:00
|
|
|
use std::any::Any;
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-03 11:15:18 +00:00
|
|
|
pub struct Schema<Query, Mutation> {
|
2020-03-05 00:39:56 +00:00
|
|
|
query: QueryRoot<Query>,
|
|
|
|
mutation: Mutation,
|
2020-03-03 11:15:18 +00:00
|
|
|
registry: Registry,
|
2020-03-05 00:39:56 +00:00
|
|
|
data: Data,
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 11:15:18 +00:00
|
|
|
impl<Query: GQLObject, Mutation: GQLObject> Schema<Query, Mutation> {
|
2020-03-05 00:39:56 +00:00
|
|
|
pub fn new(query: Query, mutation: Mutation) -> Self {
|
2020-03-04 06:24:44 +00:00
|
|
|
let mut registry = Registry::default();
|
|
|
|
|
|
|
|
registry.add_directive(Directive {
|
|
|
|
name: "include",
|
|
|
|
description: Some("Directs the executor to include this field or fragment only when the `if` argument is true."),
|
|
|
|
locations: vec![__DirectiveLocation::FIELD],
|
|
|
|
args: vec![InputValue{
|
|
|
|
name: "if",
|
|
|
|
description: Some("Included when true."),
|
|
|
|
ty: "Boolean!".to_string(),
|
|
|
|
default_value: None
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
|
|
|
registry.add_directive(Directive {
|
|
|
|
name: "skip",
|
|
|
|
description: Some("Directs the executor to skip this field or fragment when the `if` argument is true."),
|
|
|
|
locations: vec![__DirectiveLocation::FIELD],
|
|
|
|
args: vec![InputValue{
|
|
|
|
name: "if",
|
|
|
|
description: Some("Skipped when true."),
|
|
|
|
ty: "Boolean!".to_string(),
|
|
|
|
default_value: None
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
|
|
|
// register scalars
|
|
|
|
bool::create_type_info(&mut registry);
|
|
|
|
i32::create_type_info(&mut registry);
|
|
|
|
f32::create_type_info(&mut registry);
|
|
|
|
String::create_type_info(&mut registry);
|
|
|
|
|
2020-03-03 11:15:18 +00:00
|
|
|
Query::create_type_info(&mut registry);
|
|
|
|
Mutation::create_type_info(&mut registry);
|
2020-03-04 06:24:44 +00:00
|
|
|
|
2020-03-01 10:54:34 +00:00
|
|
|
Self {
|
2020-03-03 11:15:18 +00:00
|
|
|
query: QueryRoot {
|
|
|
|
inner: query,
|
|
|
|
query_type: Query::type_name().to_string(),
|
|
|
|
mutation_type: Mutation::type_name().to_string(),
|
|
|
|
},
|
2020-03-01 10:54:34 +00:00
|
|
|
mutation,
|
2020-03-05 00:39:56 +00:00
|
|
|
registry,
|
|
|
|
data: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn data<D: Any + Send + Sync>(mut self, data: D) -> Self {
|
|
|
|
self.data.insert(Box::new(data));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn query<'a>(&'a self, query_source: &'a str) -> QueryBuilder<'a, Query, Mutation> {
|
|
|
|
QueryBuilder {
|
|
|
|
query: &self.query,
|
|
|
|
mutation: &self.mutation,
|
2020-03-03 11:15:18 +00:00
|
|
|
registry: &self.registry,
|
2020-03-01 10:54:34 +00:00
|
|
|
query_source,
|
|
|
|
operation_name: None,
|
|
|
|
variables: None,
|
2020-03-05 00:39:56 +00:00
|
|
|
data: &self.data,
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-03 11:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct QueryBuilder<'a, Query, Mutation> {
|
2020-03-05 00:39:56 +00:00
|
|
|
query: &'a QueryRoot<Query>,
|
|
|
|
mutation: &'a Mutation,
|
2020-03-03 11:15:18 +00:00
|
|
|
registry: &'a Registry,
|
|
|
|
query_source: &'a str,
|
|
|
|
operation_name: Option<&'a str>,
|
|
|
|
variables: Option<&'a Variables>,
|
2020-03-05 00:39:56 +00:00
|
|
|
data: &'a Data,
|
2020-03-03 11:15:18 +00:00
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-03 11:15:18 +00:00
|
|
|
impl<'a, Query, Mutation> QueryBuilder<'a, Query, Mutation> {
|
2020-03-01 10:54:34 +00:00
|
|
|
pub fn operator_name(self, name: &'a str) -> Self {
|
2020-03-01 13:35:39 +00:00
|
|
|
QueryBuilder {
|
2020-03-01 10:54:34 +00:00
|
|
|
operation_name: Some(name),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn variables(self, vars: &'a Variables) -> Self {
|
2020-03-01 13:35:39 +00:00
|
|
|
QueryBuilder {
|
2020-03-01 10:54:34 +00:00
|
|
|
variables: Some(vars),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn execute(self) -> Result<serde_json::Value>
|
|
|
|
where
|
2020-03-03 11:15:18 +00:00
|
|
|
Query: GQLObject + Send + Sync,
|
|
|
|
Mutation: GQLObject + Send + Sync,
|
2020-03-01 10:54:34 +00:00
|
|
|
{
|
|
|
|
let document =
|
2020-03-01 13:35:39 +00:00
|
|
|
parse_query(self.query_source).map_err(|err| QueryParseError(err.to_string()))?;
|
2020-03-01 10:54:34 +00:00
|
|
|
|
|
|
|
for definition in &document.definitions {
|
|
|
|
match definition {
|
|
|
|
Definition::Operation(OperationDefinition::SelectionSet(selection_set)) => {
|
|
|
|
if self.operation_name.is_none() {
|
2020-03-03 03:48:00 +00:00
|
|
|
let ctx = ContextBase {
|
2020-03-01 10:54:34 +00:00
|
|
|
item: selection_set,
|
|
|
|
variables: self.variables.as_deref(),
|
2020-03-04 03:51:42 +00:00
|
|
|
variable_definitions: None,
|
2020-03-03 11:15:18 +00:00
|
|
|
registry: &self.registry,
|
2020-03-05 00:39:56 +00:00
|
|
|
data: self.data,
|
2020-03-01 10:54:34 +00:00
|
|
|
};
|
|
|
|
return self.query.resolve(&ctx).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Definition::Operation(OperationDefinition::Query(query)) => {
|
|
|
|
if self.operation_name.is_none()
|
|
|
|
|| self.operation_name == query.name.as_ref().map(|s| s.as_str())
|
|
|
|
{
|
2020-03-03 03:48:00 +00:00
|
|
|
let ctx = ContextBase {
|
2020-03-01 10:54:34 +00:00
|
|
|
item: &query.selection_set,
|
|
|
|
variables: self.variables.as_deref(),
|
2020-03-04 03:51:42 +00:00
|
|
|
variable_definitions: Some(&query.variable_definitions),
|
2020-03-03 11:15:18 +00:00
|
|
|
registry: self.registry.clone(),
|
2020-03-05 00:39:56 +00:00
|
|
|
data: self.data,
|
2020-03-01 10:54:34 +00:00
|
|
|
};
|
|
|
|
return self.query.resolve(&ctx).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Definition::Operation(OperationDefinition::Mutation(mutation)) => {
|
|
|
|
if self.operation_name.is_none()
|
|
|
|
|| self.operation_name == mutation.name.as_ref().map(|s| s.as_str())
|
|
|
|
{
|
2020-03-03 03:48:00 +00:00
|
|
|
let ctx = ContextBase {
|
2020-03-01 10:54:34 +00:00
|
|
|
item: &mutation.selection_set,
|
|
|
|
variables: self.variables.as_deref(),
|
2020-03-04 03:51:42 +00:00
|
|
|
variable_definitions: Some(&mutation.variable_definitions),
|
2020-03-03 11:15:18 +00:00
|
|
|
registry: self.registry.clone(),
|
2020-03-05 00:39:56 +00:00
|
|
|
data: self.data,
|
2020-03-01 10:54:34 +00:00
|
|
|
};
|
|
|
|
return self.mutation.resolve(&ctx).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Definition::Operation(OperationDefinition::Subscription(subscription)) => {
|
2020-03-01 13:35:39 +00:00
|
|
|
anyhow::bail!(QueryError::NotSupported.with_position(subscription.position));
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
Definition::Fragment(fragment) => {
|
2020-03-01 13:35:39 +00:00
|
|
|
anyhow::bail!(QueryError::NotSupported.with_position(fragment.position));
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(operation_name) = self.operation_name {
|
2020-03-01 13:35:39 +00:00
|
|
|
anyhow::bail!(QueryError::UnknownOperationNamed {
|
2020-03-01 10:54:34 +00:00
|
|
|
name: operation_name.to_string()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(serde_json::Value::Null)
|
|
|
|
}
|
|
|
|
}
|