Derive Macro SimpleObject

Source
#[derive(SimpleObject)]
{
    // Attributes available to this derive:
    #[graphql]
}
Expand description

Define a new GraphQL object type.

To implement a graphql object, you need to define a rust struct and mark it with the #[derive(SimpleObject)] attribute. Simpleobjects are used to represent simple object types in GraphQL, which means it doesn’t have any arguments or context, or resolve functions andsimply define object based on struct fields.

§Macro Attributes

AttributeDescriptionType
rootMark this type as a root query.bool
nameThe name of the objectString
rename_fieldsRename all the fields according to the given case convention. The possible values are lowercase, UPPERCASE, PascalCase, camelCase, snake_case, and SCREAMING_SNAKE_CASE.String
markMark the object as implement Interface, all interface fields should be defined manuallyPath
implMark the object as implement Interface, the interface trait should be implementedPath
get_type_nameIf true, it allows the user to implement TypeName traitbool
registerRegister other types that implement Register traitPath

§Field Attributes

AttributeDescriptionType
nameThe name of the fieldString
skipSkip this fieldbool
deprecationMark this field as a deprecatedbool
deprecationMark this field as deprecated with the reasonString

§Accepted Output Types

§Examples

§Basic

use dynamic_graphql::{SimpleObject, App};


#[derive(SimpleObject)]
struct Foo {
    value: String,
}


#[derive(SimpleObject)]
#[graphql(root)]
struct Query {
    foo: Foo,
}

#[derive(App)]
struct App(Query);

let schema = App::create_schema().finish().unwrap();

assert_eq!(
    normalize_schema(&schema.sdl()),
    r#"
type Foo {
  value: String!
}

type Query {
  foo: Foo!
}

directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

schema {
  query: Query
}
"#
);

§Rename

use dynamic_graphql::{SimpleObject, App};


#[derive(SimpleObject)]
#[graphql(name = "RootQuery")]
#[graphql(root, rename_fields = "snake_case")]
struct Query {
    hello_world: String,
    #[graphql(name = "MyField")]
    other_field: String,
}

#[derive(App)]
struct App(Query);

let schema = App::create_schema().finish().unwrap();

assert_eq!(
    normalize_schema(&schema.sdl()),
    r#"
type RootQuery {
  hello_world: String!
  MyField: String!
}

directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

schema {
  query: RootQuery
}
"#
);

§Skip, Deprecation, Description

use dynamic_graphql::{SimpleObject, App};


#[derive(SimpleObject)]
#[graphql(root)]
/// This is my object
struct Query {
    #[graphql(skip)]
    hello_world: String,
    #[graphql(deprecation)]
    deprecated_field: String,
    
    #[graphql(deprecation = "this is the old one")]
    with_reason: String,
    
    /// This is my field
    my_field: String,
}

#[derive(App)]
struct App(Query);

let schema = App::create_schema().finish().unwrap();

assert_eq!(
    normalize_schema(&schema.sdl()),
    r#"
"This is my object"
type Query {
  deprecatedField: String! @deprecated
  withReason: String! @deprecated(reason: "this is the old one")
  "This is my field"
  myField: String!
}

directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | ENUM_VALUE

directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

schema {
  query: Query
}
"#
);