Trait juniper::GraphQLType [−][src]
pub trait GraphQLType<S = DefaultScalarValue>: GraphQLValue<S> where
S: ScalarValue, { 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][3] 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 {} not found on type User", field_name), } } }
Required methods
fn name(info: &Self::TypeInfo) -> Option<&str>
[src]
Expand description
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.
fn meta<'r>(
info: &Self::TypeInfo,
registry: &mut Registry<'r, S>
) -> MetaType<'r, S> where
S: 'r,
[src]
info: &Self::TypeInfo,
registry: &mut Registry<'r, S>
) -> MetaType<'r, S> where
S: 'r,
Expand description
Returns MetaType
representing this GraphQLType
.
Implementations on Foreign Types
impl<S, T> GraphQLType<S> for Option<T> where
T: GraphQLType<S>,
S: ScalarValue,
[src]
impl<S, T> GraphQLType<S> for Option<T> where
T: GraphQLType<S>,
S: ScalarValue,
[src]impl<S, T> GraphQLType<S> for Vec<T> where
T: GraphQLType<S>,
S: ScalarValue,
[src]
impl<S, T> GraphQLType<S> for Vec<T> where
T: GraphQLType<S>,
S: ScalarValue,
[src]impl<S, T> GraphQLType<S> for [T] where
S: ScalarValue,
T: GraphQLType<S>,
[src]
impl<S, T> GraphQLType<S> for [T] where
S: ScalarValue,
T: GraphQLType<S>,
[src]impl<S, T: ?Sized> GraphQLType<S> for Box<T> where
T: GraphQLType<S>,
S: ScalarValue,
[src]
impl<S, T: ?Sized> GraphQLType<S> for Box<T> where
T: GraphQLType<S>,
S: ScalarValue,
[src]impl<'e, S, T: ?Sized> GraphQLType<S> for &'e T where
T: GraphQLType<S>,
S: ScalarValue,
[src]
impl<'e, S, T: ?Sized> GraphQLType<S> for &'e T where
T: GraphQLType<S>,
S: ScalarValue,
[src]impl<S, T: ?Sized> GraphQLType<S> for Arc<T> where
S: ScalarValue,
T: GraphQLType<S>,
[src]
impl<S, T: ?Sized> GraphQLType<S> for Arc<T> where
S: ScalarValue,
T: GraphQLType<S>,
[src]impl<S> GraphQLType<S> for String where
S: ScalarValue,
[src]
impl<S> GraphQLType<S> for String where
S: ScalarValue,
[src]impl<S> GraphQLType<S> for str where
S: ScalarValue,
[src]
impl<S> GraphQLType<S> for str where
S: ScalarValue,
[src]impl<S> GraphQLType<S> for bool where
S: ScalarValue,
[src]
impl<S> GraphQLType<S> for bool where
S: ScalarValue,
[src]impl<S> GraphQLType<S> for i32 where
S: ScalarValue,
[src]
impl<S> GraphQLType<S> for i32 where
S: ScalarValue,
[src]impl<S> GraphQLType<S> for f64 where
S: ScalarValue,
[src]
impl<S> GraphQLType<S> for f64 where
S: ScalarValue,
[src]impl<S> GraphQLType<S> for DateTime<FixedOffset> where
S: ScalarValue,
[src]
impl<S> GraphQLType<S> for DateTime<FixedOffset> where
S: ScalarValue,
[src]impl<S> GraphQLType<S> for DateTime<Utc> where
S: ScalarValue,
[src]
impl<S> GraphQLType<S> for DateTime<Utc> where
S: ScalarValue,
[src]impl<S> GraphQLType<S> for NaiveDate where
S: ScalarValue,
[src]
impl<S> GraphQLType<S> for NaiveDate where
S: ScalarValue,
[src]impl<S> GraphQLType<S> for NaiveDateTime where
S: ScalarValue,
[src]
impl<S> GraphQLType<S> for NaiveDateTime where
S: ScalarValue,
[src]impl<S> GraphQLType<S> for Url where
S: ScalarValue,
[src]
impl<S> GraphQLType<S> for Url where
S: ScalarValue,
[src]impl<S> GraphQLType<S> for Uuid where
S: ScalarValue,
[src]
impl<S> GraphQLType<S> for Uuid where
S: ScalarValue,
[src]impl<S> GraphQLType<S> for ObjectId where
S: ScalarValue,
[src]
impl<S> GraphQLType<S> for ObjectId where
S: ScalarValue,
[src]impl<S> GraphQLType<S> for UtcDateTime where
S: ScalarValue,
[src]
impl<S> GraphQLType<S> for UtcDateTime where
S: ScalarValue,
[src]Implementors
impl<'a, S> GraphQLType<S> for Argument<'a, S> where
S: ScalarValue + 'a,
[src]
impl<'a, S> GraphQLType<S> for Argument<'a, S> where
S: ScalarValue + 'a,
[src]impl<'a, S> GraphQLType<S> for EnumValue where
S: ScalarValue + 'a,
[src]
impl<'a, S> GraphQLType<S> for EnumValue where
S: ScalarValue + 'a,
[src]impl<'a, S> GraphQLType<S> for Field<'a, S> where
S: ScalarValue + 'a,
[src]
impl<'a, S> GraphQLType<S> for Field<'a, S> where
S: ScalarValue + 'a,
[src]impl<'a, S> GraphQLType<S> for SchemaType<'a, S> where
S: ScalarValue + 'a,
[src]
impl<'a, S> GraphQLType<S> for SchemaType<'a, S> where
S: ScalarValue + 'a,
[src]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>,
[src]
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>,
[src]impl<S> GraphQLType<S> for ID where
S: ScalarValue,
[src]
impl<S> GraphQLType<S> for ID where
S: ScalarValue,
[src]impl<S, T> GraphQLType<S> for Nullable<T> where
T: GraphQLType<S>,
S: ScalarValue,
[src]
impl<S, T> GraphQLType<S> for Nullable<T> where
T: GraphQLType<S>,
S: ScalarValue,
[src]impl<S, T> GraphQLType<S> for EmptyMutation<T> where
S: ScalarValue,
[src]
impl<S, T> GraphQLType<S> for EmptyMutation<T> where
S: ScalarValue,
[src]impl<S, T> GraphQLType<S> for EmptySubscription<T> where
S: ScalarValue,
[src]
impl<S, T> GraphQLType<S> for EmptySubscription<T> where
S: ScalarValue,
[src]