Skip to main content

Input

Derive Macro Input 

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

Derive a GraphQL input type.

§Arguments by default

Without a name, the type is args-only: its fields become a field’s arguments (A: InputObject) and no input object is registered in the schema. This is what an arguments struct should be — friends(limit: 10, after: "...") rather than a leaked FriendsArgs input type.

A type that is also used nested inside another input object must be named:

ⓘ
use gurafi::Input;

#[derive(Input)]
struct FriendsArgs {
    #[graphql(description = "Max results", default = 10)]
    limit: i32,
    after: Option<String>,
}

#[derive(Input)]
#[graphql(name = "FilterInput")]
struct Filter {
    prefix: Option<String>,
}

#[derive(Input)]
struct SearchArgs {
    filter: Option<Filter>, // requires FilterInput to exist
}

Attribute support:

  • #[graphql(name = "...")] makes the type a reusable named input object, or renames one of its fields.
  • #[graphql(description = "...")] (or a doc comment) sets a description.
  • #[graphql(default = expr)] sets a default value, typed as the field’s Rust type.
  • #[graphql(default_with = "path")] computes the default by calling path().
  • #[graphql(deprecated)] / #[graphql(deprecated = "reason")] deprecates a field.
  • #[graphql(validate = "path")] runs path(&value) after coercion. On the type it receives &Self; on a field it receives &FieldType. The function returns Result<(), gql::GraphQLError> and its error becomes a field error.
  • #[graphql(one_of)] marks a named input as a @oneOf input. Every field must be Option<T> and no field may have a default.

Option<T> fields are optional; every other field without a default is required.