additions so type can also be shareable as well as field

This commit is contained in:
Damien Pontifex 2022-08-17 10:36:00 +08:00
parent 969ce53e41
commit be19b76d5d
13 changed files with 48 additions and 4 deletions

View File

@ -200,6 +200,8 @@ pub struct SimpleObject {
#[darling(default)]
pub extends: bool,
#[darling(default)]
pub shareable: bool,
#[darling(default)]
pub visible: Option<Visible>,
#[darling(default, multiple, rename = "concrete")]
pub concretes: Vec<ConcreteType>,
@ -237,6 +239,7 @@ pub struct Object {
pub rename_args: Option<RenameRule>,
pub cache_control: CacheControl,
pub extends: bool,
pub shareable: bool,
pub use_type_description: bool,
pub visible: Option<Visible>,
pub serial: bool,
@ -599,6 +602,8 @@ pub struct MergedObject {
#[darling(default)]
pub extends: bool,
#[darling(default)]
pub shareable: bool,
#[darling(default)]
pub visible: Option<Visible>,
#[darling(default)]
pub serial: bool,

View File

@ -14,6 +14,7 @@ pub fn generate(object_args: &args::MergedObject) -> GeneratorResult<TokenStream
let ident = &object_args.ident;
let (impl_generics, ty_generics, where_clause) = object_args.generics.split_for_impl();
let extends = object_args.extends;
let shareable = object_args.shareable;
let gql_typename = object_args
.name
.clone()
@ -103,6 +104,7 @@ pub fn generate(object_args: &args::MergedObject) -> GeneratorResult<TokenStream
fields,
cache_control,
extends: #extends,
shareable: #shareable,
keys: ::std::option::Option::None,
visible: #visible,
is_subscription: false,

View File

@ -74,6 +74,7 @@ pub fn generate(object_args: &args::MergedSubscription) -> GeneratorResult<Token
extends: #extends,
keys: ::std::option::Option::None,
visible: #visible,
shareable: false,
is_subscription: true,
rust_typename: ::std::any::type_name::<Self>(),
}

View File

@ -26,6 +26,7 @@ pub fn generate(
let (self_ty, self_name) = get_type_path_and_name(item_impl.self_ty.as_ref())?;
let (impl_generics, _, where_clause) = item_impl.generics.split_for_impl();
let extends = object_args.extends;
let shareable = object_args.shareable;
let gql_typename = if !object_args.name_type {
object_args
.name
@ -642,6 +643,7 @@ pub fn generate(
},
cache_control: #cache_control,
extends: #extends,
shareable: #shareable,
keys: ::std::option::Option::None,
visible: #visible,
is_subscription: false,
@ -681,6 +683,7 @@ pub fn generate(
},
cache_control: #cache_control,
extends: #extends,
shareable: #shareable,
keys: ::std::option::Option::None,
visible: #visible,
is_subscription: false,

View File

@ -30,6 +30,7 @@ pub fn generate(object_args: &args::SimpleObject) -> GeneratorResult<TokenStream
let ident = &object_args.ident;
let (impl_generics, ty_generics, where_clause) = object_args.generics.split_for_impl();
let extends = object_args.extends;
let shareable = object_args.shareable;
let gql_typename = if !object_args.name_type {
object_args
.name
@ -331,6 +332,7 @@ pub fn generate(object_args: &args::SimpleObject) -> GeneratorResult<TokenStream
},
cache_control: #cache_control,
extends: #extends,
shareable: #shareable,
keys: ::std::option::Option::None,
visible: #visible,
is_subscription: false,
@ -395,6 +397,7 @@ pub fn generate(object_args: &args::SimpleObject) -> GeneratorResult<TokenStream
},
cache_control: #cache_control,
extends: #extends,
shareable: #shareable,
keys: ::std::option::Option::None,
visible: #visible,
is_subscription: false,

View File

@ -409,6 +409,7 @@ pub fn generate(
extends: #extends,
keys: ::std::option::Option::None,
visible: #visible,
shareable: false,
is_subscription: true,
rust_typename: ::std::any::type_name::<Self>(),
})

View File

@ -12,6 +12,8 @@
- The `requires` directive is used to annotate the required input fieldset from a base type for a resolver. It is used to develop a query plan where the required fields may not be needed by the client, but the service may need additional information from other services.
- The `shareable` directive is used to indicate that an object type's field is allowed to be resolved by multiple subgraphs (by default, each field can be resolved by only one subgraph).
## Entity lookup function
```rust

View File

@ -201,6 +201,7 @@ impl Registry {
extends,
keys,
description,
shareable,
..
} => {
if Some(name.as_str()) == self.subscription_type.as_deref()
@ -244,6 +245,9 @@ impl Registry {
write!(sdl, "@key(fields: \"{}\") ", key).ok();
}
}
if *shareable {
write!(sdl, "@shareable ").ok();
}
}
writeln!(sdl, "{{").ok();

View File

@ -218,6 +218,7 @@ pub enum MetaType {
fields: IndexMap<String, MetaField>,
cache_control: CacheControl,
extends: bool,
shareable: bool,
keys: Option<Vec<String>>,
visible: Option<MetaVisibleFn>,
is_subscription: bool,
@ -493,6 +494,7 @@ impl Registry {
fields: Default::default(),
cache_control: Default::default(),
extends: false,
shareable: false,
keys: None,
visible: None,
is_subscription: false,
@ -706,6 +708,7 @@ impl Registry {
},
cache_control: Default::default(),
extends: false,
shareable: false,
keys: None,
visible: None,
is_subscription: false,

View File

@ -54,6 +54,7 @@ impl OutputType for EmptyMutation {
fields: Default::default(),
cache_control: Default::default(),
extends: false,
shareable: false,
keys: None,
visible: None,
is_subscription: false,

View File

@ -23,6 +23,7 @@ impl SubscriptionType for EmptySubscription {
fields: Default::default(),
cache_control: Default::default(),
extends: false,
shareable: false,
keys: None,
visible: None,
is_subscription: true,

View File

@ -77,6 +77,7 @@ where
fields,
cache_control: cc,
extends: false,
shareable: false,
keys: None,
visible: None,
is_subscription: false,
@ -135,6 +136,7 @@ where
fields,
cache_control: cc,
extends: false,
shareable: false,
keys: None,
visible: None,
is_subscription: false,
@ -168,6 +170,7 @@ impl SubscriptionType for MergedObjectTail {
fields: Default::default(),
cache_control: Default::default(),
extends: false,
shareable: false,
keys: None,
visible: None,
is_subscription: false,

View File

@ -290,26 +290,41 @@ pub async fn test_entity_union() {
#[tokio::test]
pub async fn test_entity_shareable() {
#[derive(SimpleObject)]
struct MyObj {
struct MyObjFieldShareable {
#[graphql(shareable)]
field_shareable_a: i32,
}
#[derive(SimpleObject)]
#[graphql(shareable)]
struct MyObjShareable {
a: i32,
}
struct Query;
#[Object(extends)]
impl Query {
#[graphql(entity)]
async fn find_obj(&self, _id: i32) -> MyObj {
async fn find_obj_field_shareable(&self, _id: i32) -> MyObjFieldShareable {
todo!()
}
#[graphql(entity)]
async fn find_obj_shareable(&self, _id: i32) -> MyObjShareable {
todo!()
}
}
let schema_sdl = Schema::new(Query, EmptyMutation, EmptySubscription)
.sdl_with_options(SDLExportOptions::new().federation());
println!("{}", schema_sdl);
assert_eq!(
schema_sdl.contains("a: Int! @shareable"),
schema_sdl.contains("fieldShareableA: Int! @shareable"),
true
);
assert_eq!(
schema_sdl.contains(r#"MyObjShareable @key(fields: "id") @shareable"#),
true
);
}