-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extensions for dynamic GraphQL type definition
- Loading branch information
Showing
11 changed files
with
680 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ pub mod arguments; | |
pub mod dynamic; | ||
pub mod graphql; | ||
pub mod queries; | ||
pub mod util; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//! `async_graphql::dynamic` extensions for handling GraphQL connections. | ||
//! See: https://graphql.org/learn/pagination/#end-of-list-counts-and-connections | ||
//! See: https://relay.dev/graphql/connections.htm#sec-Connection-Types | ||
|
||
pub use super::{edge::*, filtering::*, node::*, ordering::*, paging::*}; | ||
use async_graphql::dynamic::{ | ||
Field, InputObject, InputValue, Object, SchemaBuilder, TypeRef, | ||
}; | ||
use extension_trait::extension_trait; | ||
|
||
#[extension_trait] | ||
pub impl TypeRefConnectionExt for TypeRef { | ||
fn connection(node_name: impl Into<String>) -> String { | ||
format!("{}Connection", node_name.into()) | ||
} | ||
} | ||
|
||
#[extension_trait] | ||
pub impl ObjectConnectionExt for Object { | ||
fn new_connection(node_name: impl Into<String>) -> Self { | ||
let node_name = node_name.into(); | ||
Self::new(TypeRef::connection(node_name.clone())) | ||
.field(Field::new( | ||
"totalCount", | ||
TypeRef::named_nn(TypeRef::INT), | ||
|_| unimplemented!(), | ||
)) | ||
.field(Field::new( | ||
"nodes", | ||
TypeRef::named_nn_list_nn(node_name.clone()), | ||
|_| unimplemented!(), | ||
)) | ||
.field(Field::new( | ||
"edges", | ||
TypeRef::named_nn_list_nn(TypeRef::edge(node_name)), | ||
|_| unimplemented!(), | ||
)) | ||
.field(Field::new( | ||
"pageInfo", | ||
TypeRef::named_nn(TypeRef::PAGE_INFO), | ||
|_| unimplemented!(), | ||
)) | ||
} | ||
} | ||
|
||
#[extension_trait] | ||
pub impl FieldConnectionExt for Field { | ||
fn connection_arguments(self, node_name: impl Into<String>) -> Self { | ||
let node_name = node_name.into(); | ||
self.filtering_arguments(node_name.clone()) | ||
.ordering_arguments(node_name) | ||
.paging_arguments() | ||
} | ||
} | ||
|
||
#[extension_trait] | ||
pub impl SchemaBuilderConnectionExt for SchemaBuilder { | ||
fn register_connection_types(self) -> Self { | ||
let id_filter_input = InputObject::new_eq_filter(TypeRef::ID); | ||
let node_filter_input = InputObject::new_filter(TypeRef::NODE).field( | ||
InputValue::new("id", TypeRef::named(TypeRef::filter_input(TypeRef::ID))), | ||
); | ||
let node_order_input = InputObject::new_order(TypeRef::NODE).field( | ||
InputValue::new("id", TypeRef::named(TypeRef::ORDER_DIRECTION)), | ||
); | ||
self.register_edge_types() | ||
.register_filtering_types() | ||
.register_ordering_types() | ||
.register_paging_types() | ||
.register(id_filter_input) | ||
.register(node_filter_input) | ||
.register(node_order_input) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//! `async_graphql::dynamic` extensions for handling GraphQL edges. | ||
//! See: https://graphql.org/learn/pagination/#pagination-and-edges | ||
//! See: https://relay.dev/graphql/connections.htm#sec-Edge-Types.Fields | ||
|
||
pub use super::node::*; | ||
use async_graphql::dynamic::{Field, Object, SchemaBuilder, TypeRef}; | ||
use extension_trait::extension_trait; | ||
|
||
#[extension_trait] | ||
pub impl TypeRefEdgeExt for TypeRef { | ||
fn edge(node_name: impl Into<String>) -> String { | ||
format!("{}Edge", node_name.into()) | ||
} | ||
} | ||
|
||
#[extension_trait] | ||
pub impl ObjectEdgeExt for Object { | ||
fn new_edge(node_name: impl Into<String>) -> Self { | ||
let node_name = node_name.into(); | ||
Self::new(TypeRef::edge(node_name.clone())) | ||
.field(Field::new( | ||
"node", | ||
TypeRef::named_nn(node_name), | ||
|_| unimplemented!(), | ||
)) | ||
.field(Field::new( | ||
"cursor", | ||
TypeRef::named_nn(TypeRef::STRING), | ||
|_| unimplemented!(), | ||
)) | ||
} | ||
} | ||
|
||
#[extension_trait] | ||
pub impl SchemaBuilderEdgeExt for SchemaBuilder { | ||
fn register_edge_types(self) -> Self { | ||
self.register_node_types() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//! `async_graphql::dynamic` extensions for handling connection filtering. | ||
|
||
use async_graphql::dynamic::{Field, InputObject, InputValue, SchemaBuilder, TypeRef}; | ||
use extension_trait::extension_trait; | ||
|
||
#[extension_trait] | ||
pub impl TypeRefFilteringExt for TypeRef { | ||
fn filter_input(value_name: impl Into<String>) -> String { | ||
format!("{}FilterInput", value_name.into()) | ||
} | ||
} | ||
|
||
#[extension_trait] | ||
pub impl InputObjectFilteringExt for InputObject { | ||
fn new_filter(value_name: impl Into<String>) -> Self { | ||
let name = TypeRef::filter_input(value_name); | ||
Self::new(name).filter_fields() | ||
} | ||
|
||
fn new_eq_filter(value_name: impl Into<String>) -> Self { | ||
let name = TypeRef::filter_input(value_name); | ||
Self::new(name.clone()) | ||
.filter_fields() | ||
.filter_eq_fields(name) | ||
} | ||
|
||
fn new_ord_filter(value_name: impl Into<String>) -> Self { | ||
let name = TypeRef::filter_input(value_name); | ||
Self::new(name.clone()) | ||
.filter_fields() | ||
.filter_eq_fields(name.clone()) | ||
.filter_ord_fields(name) | ||
} | ||
|
||
fn filter_fields(self) -> Self { | ||
let name = self.type_name().to_string(); | ||
self.field(InputValue::new("and", TypeRef::named_nn_list(name.clone()))) | ||
.field(InputValue::new("or", TypeRef::named_nn_list(name.clone()))) | ||
.field(InputValue::new("not", TypeRef::named_nn_list(name))) | ||
} | ||
|
||
fn filter_eq_fields(self, value_name: impl Into<String>) -> Self { | ||
let value_name = value_name.into(); | ||
self.field(InputValue::new("eq", TypeRef::named(value_name.clone()))) | ||
.field(InputValue::new("in", TypeRef::named_nn_list(value_name))) | ||
} | ||
|
||
fn filter_ord_fields(self, value_name: impl Into<String>) -> Self { | ||
let value_name = value_name.into(); | ||
self.field(InputValue::new("gt", TypeRef::named(value_name.clone()))) | ||
.field(InputValue::new("gte", TypeRef::named(value_name.clone()))) | ||
.field(InputValue::new("lt", TypeRef::named(value_name.clone()))) | ||
.field(InputValue::new("lte", TypeRef::named(value_name))) | ||
} | ||
} | ||
|
||
#[extension_trait] | ||
pub impl SchemaBuilderFilteringExt for SchemaBuilder { | ||
fn register_filtering_types(self) -> Self { | ||
self | ||
} | ||
} | ||
|
||
#[extension_trait] | ||
pub impl FieldFilteringExt for Field { | ||
fn filtering_arguments(self, value_name: impl Into<String>) -> Self { | ||
let filter_name = TypeRef::filter_input(value_name); | ||
self.argument(InputValue::new("filter", TypeRef::named(filter_name))) | ||
} | ||
} |
Oops, something went wrong.