Trait juniper::GraphQLType

source ·
pub trait GraphQLType<S = DefaultScalarValue>: GraphQLValue<S>
where S: ScalarValue,
{ // Required methods fn name(info: &Self::TypeInfo) -> Option<&str>; fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, S> ) -> MetaType<'r, S> where S: 'r; }
Expand description

Primary trait used to expose Rust types in a GraphQL schema.

All of the convenience macros ultimately expand into an implementation of this trait for the given type. This can all be done manually.

§Example

Manually deriving an object is straightforward, but tedious. This is the equivalent of the User object as shown in the example in the documentation root:

use juniper::{
    meta::MetaType, Arguments, Context, DefaultScalarValue, Executor, ExecutionResult,
    FieldResult, GraphQLType, GraphQLValue, Registry,
};

#[derive(Debug)]
struct Database { users: HashMap<String, User> }
impl Context for Database {}

#[derive(Debug)]
struct User { id: String, name: String, friend_ids: Vec<String> }

impl GraphQLType<DefaultScalarValue> for User {
   fn name(_: &()) -> Option<&'static str> {
       Some("User")
   }

   fn meta<'r>(_: &(), registry: &mut Registry<'r>) -> MetaType<'r>
   where DefaultScalarValue: 'r,
   {
       // First, we need to define all fields and their types on this type.
       //
       // If we need arguments, want to implement interfaces, or want to add documentation
       // strings, we can do it here.
       let fields = &[
           registry.field::<&String>("id", &()),
           registry.field::<&String>("name", &()),
           registry.field::<Vec<&User>>("friends", &()),
       ];
       registry.build_object_type::<User>(&(), fields).into_meta()
   }
}

impl GraphQLValue<DefaultScalarValue> for User {
    type Context = Database;
    type TypeInfo = ();

    fn type_name(&self, _: &()) -> Option<&'static str> {
        <User as GraphQLType>::name(&())
    }

    fn resolve_field(
        &self,
        info: &(),
        field_name: &str,
        args: &Arguments,
        executor: &Executor<Database>
    ) -> ExecutionResult
    {
        // Next, we need to match the queried field name. All arms of this match statement
        // return `ExecutionResult`, which makes it hard to statically verify that the type you
        // pass on to `executor.resolve*` actually matches the one that you defined in `meta()`
        // above.
        let database = executor.context();
        match field_name {
            // Because scalars are defined with another `Context` associated type, you must use
            // `resolve_with_ctx` here to make the `executor` perform automatic type conversion
            // of its argument.
            "id" => executor.resolve_with_ctx(info, &self.id),
            "name" => executor.resolve_with_ctx(info, &self.name),

            // You pass a vector of `User` objects to `executor.resolve`, and it will determine
            // which fields of the sub-objects to actually resolve based on the query.
            // The `executor` instance keeps track of its current position in the query.
            "friends" => executor.resolve(info,
                &self.friend_ids.iter()
                    .filter_map(|id| database.users.get(id))
                    .collect::<Vec<_>>()
            ),

            // We can only reach this panic in two cases: either a mismatch between the defined
            // schema in `meta()` above, or a validation failed because of a this library bug.
            //
            // In either of those two cases, the only reasonable way out is to panic the thread.
            _ => panic!("Field {field_name} not found on type User"),
        }
    }
}

Required Methods§

source

fn name(info: &Self::TypeInfo) -> Option<&str>

Returns name of this GraphQLType to expose.

This function will be called multiple times during schema construction. It must not perform any calculation and always return the same value.

source

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, S> ) -> MetaType<'r, S>
where S: 'r,

Returns MetaType representing this GraphQLType.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<'e, S, T> GraphQLType<S> for &'e T
where T: GraphQLType<S> + ?Sized, S: ScalarValue,

source§

fn name(info: &Self::TypeInfo) -> Option<&str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, S> ) -> MetaType<'r, S>
where S: 'r,

source§

impl<S> GraphQLType<S> for str
where S: ScalarValue,

source§

fn name(_: &()) -> Option<&'static str>

source§

fn meta<'r>(_: &(), registry: &mut Registry<'r, S>) -> MetaType<'r, S>
where S: 'r,

source§

impl<S, T> GraphQLType<S> for Option<T>
where T: GraphQLType<S>, S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, S> ) -> MetaType<'r, S>
where S: 'r,

source§

impl<S, T> GraphQLType<S> for [T]
where S: ScalarValue, T: GraphQLType<S>,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, S> ) -> MetaType<'r, S>
where S: 'r,

source§

impl<S, T> GraphQLType<S> for Box<T>
where T: GraphQLType<S> + ?Sized, S: ScalarValue,

source§

fn name(info: &Self::TypeInfo) -> Option<&str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, S> ) -> MetaType<'r, S>
where S: 'r,

source§

impl<S, T> GraphQLType<S> for Arc<T>
where S: ScalarValue, T: GraphQLType<S> + ?Sized,

source§

fn name(info: &Self::TypeInfo) -> Option<&str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, S> ) -> MetaType<'r, S>
where S: 'r,

source§

impl<S, T> GraphQLType<S> for Vec<T>
where T: GraphQLType<S>, S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, S> ) -> MetaType<'r, S>
where S: 'r,

source§

impl<S, T, const N: usize> GraphQLType<S> for [T; N]
where S: ScalarValue, T: GraphQLType<S>,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, S> ) -> MetaType<'r, S>
where S: 'r,

source§

impl<Tz, __S> GraphQLType<__S> for DateTime<Tz>

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for Tz
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for bool
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for f64
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for i32
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for String
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for BigDecimal
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for DateTime
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for ObjectId
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for NaiveDate
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for NaiveDateTime
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for NaiveTime
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for Decimal
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for Date
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for OffsetDateTime
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for PrimitiveDateTime
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for Time
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for UtcOffset
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for Url
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

source§

impl<__S> GraphQLType<__S> for Uuid
where __S: ScalarValue,

source§

fn name(_: &Self::TypeInfo) -> Option<&'static str>

source§

fn meta<'r>( info: &Self::TypeInfo, registry: &mut Registry<'r, __S> ) -> MetaType<'r, __S>
where __S: 'r,

Implementors§

source§

impl<'a, S> GraphQLType<S> for Argument<'a, S>
where S: ScalarValue + ScalarValue + 'a,

source§

impl<'a, S> GraphQLType<S> for Field<'a, S>
where S: ScalarValue + ScalarValue + 'a,

source§

impl<'a, S> GraphQLType<S> for SchemaType<'a, S>
where S: ScalarValue + ScalarValue + 'a,

source§

impl<'a, S, QueryT, MutationT, SubscriptionT> GraphQLType<S> for RootNode<'a, QueryT, MutationT, SubscriptionT, S>
where S: ScalarValue, QueryT: GraphQLType<S>, MutationT: GraphQLType<S, Context = QueryT::Context>, SubscriptionT: GraphQLType<S, Context = QueryT::Context>,

source§

impl<S, T> GraphQLType<S> for Nullable<T>
where T: GraphQLType<S>, S: ScalarValue,

source§

impl<S, T> GraphQLType<S> for EmptyMutation<T>
where S: ScalarValue,

source§

impl<S, T> GraphQLType<S> for EmptySubscription<T>
where S: ScalarValue,

source§

impl<__S> GraphQLType<__S> for TypeKind
where __S: ScalarValue,

source§

impl<__S> GraphQLType<__S> for Episode
where __S: ScalarValue,

source§

impl<__S> GraphQLType<__S> for EnumValue
where __S: ScalarValue,

source§

impl<__S> GraphQLType<__S> for ID
where __S: ScalarValue,

source§

impl<__S> GraphQLType<__S> for Droid
where __S: ScalarValue,

source§

impl<__S> GraphQLType<__S> for Human
where __S: ScalarValue,

source§

impl<__S> GraphQLType<__S> for Query
where __S: ScalarValue,

source§

impl<__S> GraphQLType<__S> for Subscription
where __S: ScalarValue,

source§

impl<__S> GraphQLType<__S> for CharacterValue
where __S: ScalarValue,