Define a new GraphQL object type.
Resolved objects fields should be used with #[derive(ResolvedObject)] derive macro.
| Attribute | Description | Type |
rename_fields | Rename all the fields according to the given case convention. The possible values are lowercase, UPPERCASE, PascalCase, camelCase, snake_case, and SCREAMING_SNAKE_CASE. | String |
rename_args | Rename all the arguments according to the given case convention. The possible values are lowercase, UPPERCASE, PascalCase, camelCase, snake_case, and SCREAMING_SNAKE_CASE. | String |
| Attribute | Description | Type |
name | The name of the field | String |
skip | Skip this field | bool |
deprecation | Mark this field as a deprecated | bool |
deprecation | Mark this field as deprecated with the reason | String |
rename_args | Rename all the arguments according to the given case convention. The possible values are lowercase, UPPERCASE, PascalCase, camelCase, snake_case, and SCREAMING_SNAKE_CASE. | String |
| Attribute | Description | Type |
name | The name of the argument | String |
ctx | Mark this argument as a context | bool |
String, &str, ID
bool
i8, i16, i32, i64, i128, isize
u8, u16, u32, u64, u128, usize
f32, f64
Option<T> where T is one of the valid output types
Vec<T>, &[T] where T is one of the valid output types
Result<T, E> where T is one of the valid output types
Instance<dyn Trait> where Trait is marked by #[Interface]
- types defined by
#[derive(SimpleObject)]
- types defined by
#[derive(ResolvedObject)]
- types defined by
#[derive(Enum)]
- types defined by
#[derive(Scalar)]
- types defined by
#[derive(Union)]
- any type implements
OutputTypeName and ResolveRef traits if a reference is returned
- any type implements
OutputTypeName and ResolveOwned traits if an owned value is returned
String, &str, ID
bool
i8, i16, i32, i64, i128, isize
u8, u16, u32, u64, u128, usize
f32, f64
Option<T> where T is one of the valid argument types
MaybeUndefined<T> where T is one of the valid argument types
Vec<T> where T is one of the valid argument types
Upload type
Result<T> where T is one of the valid argument types (except Option<T> and MaybeUndefined<T>, use Option<Result<T>> or MaybeUndefined<Result<T>> instead)
- types defined by
#[derive(InputObject)]
- types defined by
#[derive(Enum)]
- types defined by
#[derive(Scalar)]
- any type implements
InputTypeName and FromValue traits
use dynamic_graphql::{ResolvedObject, ResolvedObjectFields, App};
#[derive(ResolvedObject)]
struct Foo {
value: i32,
}
#[ResolvedObjectFields]
impl Foo {
fn bar(&self) -> i32 {
self.value
}
}
#[derive(ResolvedObject)]
#[graphql(root)]
struct Query;
#[ResolvedObjectFields]
impl Query {
async fn foo() -> Foo {
Foo { value: 1 }
}
}
#[derive(App)]
struct App(Query);
let schema = App::create_schema().finish().unwrap();
assert_eq!(
normalize_schema(&schema.sdl()),
r#"
type Foo {
bar: Int!
}
type Query {
foo: Foo!
}
schema {
query: Query
}
"#
);
use dynamic_graphql::{ResolvedObject, ResolvedObjectFields, App};
#[derive(ResolvedObject)]
#[graphql(root)]
#[graphql(name = "RootQuery")]
struct Query;
#[ResolvedObjectFields]
#[graphql(rename_fields = "snake_case")]
#[graphql(rename_args = "PascalCase")]
impl Query {
fn first_field(&self) -> i32 {
1
}
#[graphql(name = "THE_SECOND")]
fn second_field(&self) -> i32 {
2
}
fn third_field(&self, #[graphql(name = "theArg")] arg: i32, _other_arg: i32) -> i32 {
arg
}
#[graphql(rename_args = "snake_case")]
fn fourth_field(&self, #[graphql(name = "theArg")] arg: i32, _other_arg: i32) -> i32 {
arg
}
}
#[derive(App)]
struct App(Query);
let schema = App::create_schema().finish().unwrap();
assert_eq!(
normalize_schema(&schema.sdl()),
r#"
type RootQuery {
first_field: Int!
THE_SECOND: Int!
third_field(theArg: Int!, OtherArg: Int!): Int!
fourth_field(theArg: Int!, other_arg: Int!): Int!
}
schema {
query: RootQuery
}
"#
);
use dynamic_graphql::{ResolvedObject, ResolvedObjectFields, App};
#[derive(ResolvedObject)]
#[graphql(root)]
struct Query;
#[ResolvedObjectFields]
impl Query {
#[graphql(skip)]
fn first_field(&self) -> i32 {
1
}
#[graphql(deprecation)]
fn second_field(&self) -> i32 {
2
}
#[graphql(deprecation = "the old one")]
fn third_field(&self) -> i32 {
3
}
fn fourth_field(&self) -> i32 {
4
}
}
#[derive(App)]
struct App(Query);
let schema = App::create_schema().finish().unwrap();
assert_eq!(
normalize_schema(&schema.sdl()),
r#"
"""
the query
"""
type Query {
secondField: Int! @deprecated
thirdField: Int! @deprecated(reason: "the old one")
"""
the fourth field
"""
fourthField: Int!
}
schema {
query: Query
}
"#
);
use dynamic_graphql::ResolvedObjectFields;
use dynamic_graphql::ResolvedObject;
use dynamic_graphql::App;
use dynamic_graphql::Context;
use dynamic_graphql::dynamic;
use dynamic_graphql::FieldValue;
use dynamic_graphql::value;
use dynamic_graphql::dynamic::DynamicRequestExt;
#[derive(ResolvedObject)]
#[graphql(root)]
struct Query {
value: i32,
}
#[ResolvedObjectFields]
impl Query {
fn simple_arg(arg: i32) -> i32 {
arg
}
fn with_context(
_ctx: &Context,
#[graphql(ctx)] _context: &Context,
#[graphql(name = "ctx")] ctx_arg: i32,
) -> i32 {
ctx_arg
}
fn with_self(&self, arg: i32) -> i32 {
self.value + arg
}
}
#[derive(App)]
struct App(Query);
let schema = App::create_schema().finish().unwrap();
assert_eq!(
normalize_schema(&schema.sdl()),
r#"
type Query {
simpleArg(arg: Int!): Int!
withContext(ctx: Int!): Int!
withSelf(arg: Int!): Int!
}
schema {
query: Query
}
"#
);
let query = r#"
query {
withSelf(arg: 1)
}
"#;
let root = Query {
value: 1,
};
let req = dynamic_graphql::Request::new(query).root_value(FieldValue::owned_any(root));
let res = schema.execute(req).await;
let data = res.data;
assert_eq!(
data,
value!({ "withSelf": 2 }),
);