pub trait GraphQLType<S = DefaultScalarValue>: GraphQLValue<S>where
S: ScalarValue,{
// Required methods
fn name(info: &Self::TypeInfo) -> Option<ArcStr>;
fn meta(info: &Self::TypeInfo, registry: &mut Registry<S>) -> MetaType<S>;
}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::{
arcstr, meta::MetaType, ArcStr, 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<ArcStr> {
Some(arcstr::literal!("User"))
}
fn meta(_: &(), registry: &mut Registry) -> MetaType {
// 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>(arcstr::literal!("id"), &()),
registry.field::<String>(arcstr::literal!("name"), &()),
registry.field::<Vec<User>>(arcstr::literal!("friends"), &()),
];
registry.build_object_type::<User>(&(), fields).into_meta()
}
}
impl GraphQLValue<DefaultScalarValue> for User {
type Context = Database;
type TypeInfo = ();
fn type_name(&self, _: &()) -> Option<ArcStr> {
<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§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl<S> GraphQLType<S> for strwhere
S: ScalarValue,
impl<S> GraphQLType<S> for strwhere
S: ScalarValue,
Source§impl<S, T> GraphQLType<S> for Option<T>where
T: GraphQLType<S>,
S: ScalarValue,
impl<S, T> GraphQLType<S> for Option<T>where
T: GraphQLType<S>,
S: ScalarValue,
Source§impl<S, T> GraphQLType<S> for &T
impl<S, T> GraphQLType<S> for &T
Source§impl<S, T> GraphQLType<S> for [T]where
S: ScalarValue,
T: GraphQLType<S>,
impl<S, T> GraphQLType<S> for [T]where
S: ScalarValue,
T: GraphQLType<S>,
Source§impl<S, T> GraphQLType<S> for Box<T>
impl<S, T> GraphQLType<S> for Box<T>
Source§impl<S, T> GraphQLType<S> for Arc<T>
impl<S, T> GraphQLType<S> for Arc<T>
Source§impl<S, T> GraphQLType<S> for Vec<T>where
T: GraphQLType<S>,
S: ScalarValue,
impl<S, T> GraphQLType<S> for Vec<T>where
T: GraphQLType<S>,
S: ScalarValue,
Source§impl<S, T, const N: usize> GraphQLType<S> for [T; N]where
S: ScalarValue,
T: GraphQLType<S>,
impl<S, T, const N: usize> GraphQLType<S> for [T; N]where
S: ScalarValue,
T: GraphQLType<S>,
Source§impl<Tz, __S> GraphQLType<__S> for DateTime<Tz>
Available on crate feature chrono only.
impl<Tz, __S> GraphQLType<__S> for DateTime<Tz>
Available on crate feature
chrono only.Source§impl<__S> GraphQLType<__S> for Tzwhere
__S: ScalarValue,
Available on crate feature chrono-tz only.
impl<__S> GraphQLType<__S> for Tzwhere
__S: ScalarValue,
Available on crate feature
chrono-tz only.Source§impl<__S> GraphQLType<__S> for boolwhere
__S: ScalarValue,
impl<__S> GraphQLType<__S> for boolwhere
__S: ScalarValue,
Source§impl<__S> GraphQLType<__S> for f64where
__S: ScalarValue,
impl<__S> GraphQLType<__S> for f64where
__S: ScalarValue,
Source§impl<__S> GraphQLType<__S> for i32where
__S: ScalarValue,
impl<__S> GraphQLType<__S> for i32where
__S: ScalarValue,
Source§impl<__S> GraphQLType<__S> for Stringwhere
__S: ScalarValue,
impl<__S> GraphQLType<__S> for Stringwhere
__S: ScalarValue,
Source§impl<__S> GraphQLType<__S> for BigDecimalwhere
__S: ScalarValue,
Available on crate feature bigdecimal only.
impl<__S> GraphQLType<__S> for BigDecimalwhere
__S: ScalarValue,
Available on crate feature
bigdecimal only.Source§impl<__S> GraphQLType<__S> for DateTimewhere
__S: ScalarValue,
Available on crate feature bson only.
impl<__S> GraphQLType<__S> for DateTimewhere
__S: ScalarValue,
Available on crate feature
bson only.Source§impl<__S> GraphQLType<__S> for ObjectIdwhere
__S: ScalarValue,
Available on crate feature bson only.
impl<__S> GraphQLType<__S> for ObjectIdwhere
__S: ScalarValue,
Available on crate feature
bson only.Source§impl<__S> GraphQLType<__S> for NaiveDatewhere
__S: ScalarValue,
Available on crate feature chrono only.
impl<__S> GraphQLType<__S> for NaiveDatewhere
__S: ScalarValue,
Available on crate feature
chrono only.Source§impl<__S> GraphQLType<__S> for NaiveDateTimewhere
__S: ScalarValue,
Available on crate feature chrono only.
impl<__S> GraphQLType<__S> for NaiveDateTimewhere
__S: ScalarValue,
Available on crate feature
chrono only.Source§impl<__S> GraphQLType<__S> for NaiveTimewhere
__S: ScalarValue,
Available on crate feature chrono only.
impl<__S> GraphQLType<__S> for NaiveTimewhere
__S: ScalarValue,
Available on crate feature
chrono only.Source§impl<__S> GraphQLType<__S> for CompactStringwhere
__S: ScalarValue,
impl<__S> GraphQLType<__S> for CompactStringwhere
__S: ScalarValue,
Source§impl<__S> GraphQLType<__S> for Datewhere
__S: ScalarValue,
Available on crate feature jiff only.
impl<__S> GraphQLType<__S> for Datewhere
__S: ScalarValue,
Available on crate feature
jiff only.Source§impl<__S> GraphQLType<__S> for DateTimewhere
__S: ScalarValue,
Available on crate feature jiff only.
impl<__S> GraphQLType<__S> for DateTimewhere
__S: ScalarValue,
Available on crate feature
jiff only.Source§impl<__S> GraphQLType<__S> for Timewhere
__S: ScalarValue,
Available on crate feature jiff only.
impl<__S> GraphQLType<__S> for Timewhere
__S: ScalarValue,
Available on crate feature
jiff only.Source§impl<__S> GraphQLType<__S> for Spanwhere
__S: ScalarValue,
Available on crate feature jiff only.
impl<__S> GraphQLType<__S> for Spanwhere
__S: ScalarValue,
Available on crate feature
jiff only.Source§impl<__S> GraphQLType<__S> for Timestampwhere
__S: ScalarValue,
Available on crate feature jiff only.
impl<__S> GraphQLType<__S> for Timestampwhere
__S: ScalarValue,
Available on crate feature
jiff only.Source§impl<__S> GraphQLType<__S> for Offsetwhere
__S: ScalarValue,
Available on crate feature jiff only.
impl<__S> GraphQLType<__S> for Offsetwhere
__S: ScalarValue,
Available on crate feature
jiff only.Source§impl<__S> GraphQLType<__S> for TimeZonewhere
__S: ScalarValue,
Available on crate feature jiff only.
impl<__S> GraphQLType<__S> for TimeZonewhere
__S: ScalarValue,
Available on crate feature
jiff only.Source§impl<__S> GraphQLType<__S> for Zonedwhere
__S: ScalarValue,
Available on crate feature jiff only.
impl<__S> GraphQLType<__S> for Zonedwhere
__S: ScalarValue,
Available on crate feature
jiff only.Source§impl<__S> GraphQLType<__S> for Decimalwhere
__S: ScalarValue,
Available on crate feature rust_decimal only.
impl<__S> GraphQLType<__S> for Decimalwhere
__S: ScalarValue,
Available on crate feature
rust_decimal only.Source§impl<__S> GraphQLType<__S> for Datewhere
__S: ScalarValue,
Available on crate feature time only.
impl<__S> GraphQLType<__S> for Datewhere
__S: ScalarValue,
Available on crate feature
time only.Source§impl<__S> GraphQLType<__S> for OffsetDateTimewhere
__S: ScalarValue,
Available on crate feature time only.
impl<__S> GraphQLType<__S> for OffsetDateTimewhere
__S: ScalarValue,
Available on crate feature
time only.Source§impl<__S> GraphQLType<__S> for PrimitiveDateTimewhere
__S: ScalarValue,
Available on crate feature time only.
impl<__S> GraphQLType<__S> for PrimitiveDateTimewhere
__S: ScalarValue,
Available on crate feature
time only.Source§impl<__S> GraphQLType<__S> for Timewhere
__S: ScalarValue,
Available on crate feature time only.
impl<__S> GraphQLType<__S> for Timewhere
__S: ScalarValue,
Available on crate feature
time only.Source§impl<__S> GraphQLType<__S> for UtcOffsetwhere
__S: ScalarValue,
Available on crate feature time only.
impl<__S> GraphQLType<__S> for UtcOffsetwhere
__S: ScalarValue,
Available on crate feature
time only.Source§impl<__S> GraphQLType<__S> for Urlwhere
__S: ScalarValue,
Available on crate feature url only.
impl<__S> GraphQLType<__S> for Urlwhere
__S: ScalarValue,
Available on crate feature
url only.Source§impl<__S> GraphQLType<__S> for Uuidwhere
__S: ScalarValue,
Available on crate feature uuid only.
impl<__S> GraphQLType<__S> for Uuidwhere
__S: ScalarValue,
Available on crate feature
uuid only.Implementors§
impl<S> GraphQLType<S> for Argument<S>where
S: ScalarValue + ScalarValue,
impl<S> GraphQLType<S> for Field<S>where
S: ScalarValue + ScalarValue,
impl<S> GraphQLType<S> for SchemaType<S>where
S: ScalarValue + ScalarValue,
impl<S, QueryT, MutationT, SubscriptionT> GraphQLType<S> for RootNode<QueryT, MutationT, SubscriptionT, S>where
S: ScalarValue,
QueryT: GraphQLType<S>,
MutationT: GraphQLType<S, Context = QueryT::Context>,
SubscriptionT: GraphQLType<S, Context = QueryT::Context>,
impl<S, T> GraphQLType<S> for Nullable<T>where
T: GraphQLType<S>,
S: ScalarValue,
impl<S, T> GraphQLType<S> for EmptyMutation<T>where
S: ScalarValue,
impl<S, T> GraphQLType<S> for EmptySubscription<T>where
S: ScalarValue,
impl<__S> GraphQLType<__S> for TypeKindwhere
__S: ScalarValue,
impl<__S> GraphQLType<__S> for Episodewhere
__S: ScalarValue,
Available on crate feature
expose-test-schema only.impl<__S> GraphQLType<__S> for juniper::integrations::jiff::TimeZonewhere
__S: ScalarValue,
Available on crate feature
jiff only.impl<__S> GraphQLType<__S> for EnumValuewhere
__S: ScalarValue,
impl<__S> GraphQLType<__S> for ArcStrwhere
__S: ScalarValue,
impl<__S> GraphQLType<__S> for IDwhere
__S: ScalarValue,
impl<__S> GraphQLType<__S> for Droidwhere
__S: ScalarValue,
Available on crate feature
expose-test-schema only.impl<__S> GraphQLType<__S> for Humanwhere
__S: ScalarValue,
Available on crate feature
expose-test-schema only.impl<__S> GraphQLType<__S> for Querywhere
__S: ScalarValue,
Available on crate feature
expose-test-schema only.impl<__S> GraphQLType<__S> for Subscriptionwhere
__S: ScalarValue,
Available on crate feature
expose-test-schema only.impl<__S> GraphQLType<__S> for CharacterValuewhere
__S: ScalarValue,
Available on crate feature
expose-test-schema only.