async-graphql/src/schema.rs

199 lines
6.9 KiB
Rust
Raw Normal View History

2020-03-05 00:39:56 +00:00
use crate::context::Data;
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 07:50:57 +00:00
ContextBase, GQLObject, GQLOutputValue, GQLType, QueryError, 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-05 07:50:57 +00:00
use std::collections::HashMap;
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 {
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."),
2020-03-05 13:34:31 +00:00
locations: vec![
__DirectiveLocation::FIELD,
__DirectiveLocation::FRAGMENT_SPREAD,
__DirectiveLocation::INLINE_FRAGMENT
],
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."),
2020-03-05 13:34:31 +00:00
locations: vec![
__DirectiveLocation::FIELD,
__DirectiveLocation::FRAGMENT_SPREAD,
__DirectiveLocation::INLINE_FRAGMENT
],
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);
2020-03-05 09:06:14 +00:00
if !Mutation::is_empty() {
Mutation::create_type_info(&mut registry);
}
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(),
2020-03-05 09:06:14 +00:00
mutation_type: if !Mutation::is_empty() {
Some(Mutation::type_name().to_string())
} else {
None
},
2020-03-03 11:15:18 +00:00
},
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 {
2020-03-08 01:21:29 +00:00
self.data.insert(data);
2020-03-05 00:39:56 +00:00
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-05 07:50:57 +00:00
let mut fragments = HashMap::new();
for definition in &document.definitions {
if let Definition::Fragment(fragment) = definition {
fragments.insert(fragment.name.clone(), fragment);
}
}
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-05 07:50:57 +00:00
fragments: &fragments,
2020-03-01 10:54:34 +00:00
};
2020-03-06 15:58:43 +00:00
return GQLOutputValue::resolve(self.query, &ctx).await;
2020-03-01 10:54:34 +00:00
}
}
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-05 07:50:57 +00:00
fragments: &fragments,
2020-03-01 10:54:34 +00:00
};
2020-03-06 15:58:43 +00:00
return GQLOutputValue::resolve(self.query, &ctx).await;
2020-03-01 10:54:34 +00:00
}
}
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-05 07:50:57 +00:00
fragments: &fragments,
2020-03-01 10:54:34 +00:00
};
2020-03-06 15:58:43 +00:00
return GQLOutputValue::resolve(self.mutation, &ctx).await;
2020-03-01 10:54:34 +00:00
}
}
2020-03-05 07:50:57 +00:00
_ => {}
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)
}
}