Formatting...
This commit is contained in:
parent
3cc142bfbc
commit
de12e0eba5
6 changed files with 40 additions and 48 deletions
|
@ -109,21 +109,14 @@ extern crate uuid;
|
||||||
// This allows users to just depend on juniper and get the derive
|
// This allows users to just depend on juniper and get the derive
|
||||||
// functionality automatically.
|
// functionality automatically.
|
||||||
pub use juniper_codegen::{
|
pub use juniper_codegen::{
|
||||||
GraphQLEnum,
|
impl_object, GraphQLEnum, GraphQLInputObject, GraphQLObject, GraphQLScalarValue, ScalarValue,
|
||||||
GraphQLInputObject,
|
|
||||||
GraphQLObject,
|
|
||||||
GraphQLScalarValue,
|
|
||||||
ScalarValue,
|
|
||||||
impl_object,
|
|
||||||
};
|
};
|
||||||
// Internal macros are not exported,
|
// Internal macros are not exported,
|
||||||
// but declared at the root to make them easier to use.
|
// but declared at the root to make them easier to use.
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
use juniper_codegen::{
|
use juniper_codegen::{
|
||||||
|
impl_object_internal, GraphQLEnumInternal, GraphQLInputObjectInternal,
|
||||||
GraphQLScalarValueInternal,
|
GraphQLScalarValueInternal,
|
||||||
GraphQLEnumInternal,
|
|
||||||
GraphQLInputObjectInternal,
|
|
||||||
impl_object_internal,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/**
|
||||||
## DEPRECATION WARNING
|
## DEPRECATION WARNING
|
||||||
|
|
||||||
The `graphql_object!` macro is deprecated and will be removed soon.
|
The `graphql_object!` macro is deprecated and will be removed soon.
|
||||||
Use the new[impl_object](https://docs.rs/juniper/latest/juniper/macro.impl_object.html) macro instead.
|
Use the new[impl_object](https://docs.rs/juniper/latest/juniper/macro.impl_object.html) macro instead.
|
||||||
|
|
||||||
Expose GraphQL objects
|
Expose GraphQL objects
|
||||||
|
|
|
@ -146,15 +146,13 @@ where
|
||||||
fn fields(&self, include_deprecated: bool) -> Option<Vec<&Field<S>>> {
|
fn fields(&self, include_deprecated: bool) -> Option<Vec<&Field<S>>> {
|
||||||
match *self {
|
match *self {
|
||||||
TypeType::Concrete(&MetaType::Interface(InterfaceMeta { ref fields, .. }))
|
TypeType::Concrete(&MetaType::Interface(InterfaceMeta { ref fields, .. }))
|
||||||
| TypeType::Concrete(&MetaType::Object(ObjectMeta { ref fields, .. })) => {
|
| TypeType::Concrete(&MetaType::Object(ObjectMeta { ref fields, .. })) => Some(
|
||||||
Some(
|
fields
|
||||||
fields
|
.iter()
|
||||||
.iter()
|
.filter(|f| include_deprecated || !f.deprecation_status.is_deprecated())
|
||||||
.filter(|f| include_deprecated || !f.deprecation_status.is_deprecated())
|
.filter(|f| !f.name.starts_with("__"))
|
||||||
.filter(|f| !f.name.starts_with("__"))
|
.collect(),
|
||||||
.collect(),
|
),
|
||||||
)
|
|
||||||
}
|
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -233,14 +231,12 @@ where
|
||||||
#[graphql(arguments(include_deprecated(default = false)))]
|
#[graphql(arguments(include_deprecated(default = false)))]
|
||||||
fn enum_values(&self, include_deprecated: bool) -> Option<Vec<&EnumValue>> {
|
fn enum_values(&self, include_deprecated: bool) -> Option<Vec<&EnumValue>> {
|
||||||
match *self {
|
match *self {
|
||||||
TypeType::Concrete(&MetaType::Enum(EnumMeta { ref values, .. })) => {
|
TypeType::Concrete(&MetaType::Enum(EnumMeta { ref values, .. })) => Some(
|
||||||
Some(
|
values
|
||||||
values
|
.iter()
|
||||||
.iter()
|
.filter(|f| include_deprecated || !f.deprecation_status.is_deprecated())
|
||||||
.filter(|f| include_deprecated || !f.deprecation_status.is_deprecated())
|
.collect(),
|
||||||
.collect(),
|
),
|
||||||
)
|
|
||||||
}
|
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -148,7 +148,7 @@ pub fn build_impl_object(args: TokenStream, body: TokenStream, is_internal: bool
|
||||||
// Check for executor arguments.
|
// Check for executor arguments.
|
||||||
if util::type_is_identifier_ref(&captured.ty, "Executor") {
|
if util::type_is_identifier_ref(&captured.ty, "Executor") {
|
||||||
resolve_parts.push(quote!(let #arg_ident = executor;));
|
resolve_parts.push(quote!(let #arg_ident = executor;));
|
||||||
}
|
}
|
||||||
// Make sure executor is specified as a reference.
|
// Make sure executor is specified as a reference.
|
||||||
else if util::type_is_identifier(&captured.ty, "Executor") {
|
else if util::type_is_identifier(&captured.ty, "Executor") {
|
||||||
panic!("Invalid executor argument: to access the Executor, you need to specify the type as a reference.\nDid you mean &Executor?");
|
panic!("Invalid executor argument: to access the Executor, you need to specify the type as a reference.\nDid you mean &Executor?");
|
||||||
|
@ -160,16 +160,19 @@ pub fn build_impl_object(args: TokenStream, body: TokenStream, is_internal: bool
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
{
|
{
|
||||||
resolve_parts.push(quote!( let #arg_ident = executor.context(); ));
|
resolve_parts.push(quote!( let #arg_ident = executor.context(); ));
|
||||||
}
|
}
|
||||||
// Make sure the user does not specify the Context
|
// Make sure the user does not specify the Context
|
||||||
// without a reference. (&Context)
|
// without a reference. (&Context)
|
||||||
else if context_type.clone().map(|ctx| ctx == &captured.ty).unwrap_or(false) {
|
else if context_type
|
||||||
|
.clone()
|
||||||
|
.map(|ctx| ctx == &captured.ty)
|
||||||
|
.unwrap_or(false)
|
||||||
|
{
|
||||||
panic!(
|
panic!(
|
||||||
"Invalid context argument: to access the context, you need to specify the type as a reference.\nDid you mean &{}?",
|
"Invalid context argument: to access the context, you need to specify the type as a reference.\nDid you mean &{}?",
|
||||||
quote!(captured.ty),
|
quote!(captured.ty),
|
||||||
);
|
);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
let ty = &captured.ty;
|
let ty = &captured.ty;
|
||||||
// TODO: respect graphql attribute overwrite.
|
// TODO: respect graphql attribute overwrite.
|
||||||
let final_name = util::to_camel_case(&arg_name);
|
let final_name = util::to_camel_case(&arg_name);
|
||||||
|
|
|
@ -79,16 +79,16 @@ pub fn derive_scalar_value_internal(input: TokenStream) -> TokenStream {
|
||||||
The `impl_object` proc macro is the primary way of defining GraphQL resolvers
|
The `impl_object` proc macro is the primary way of defining GraphQL resolvers
|
||||||
that can not be implemented with the GraphQLObject derive.
|
that can not be implemented with the GraphQLObject derive.
|
||||||
|
|
||||||
It enables you to write GraphQL field resolvers for a type by declaring a
|
It enables you to write GraphQL field resolvers for a type by declaring a
|
||||||
regular Rust `impl` block. Under the hood, the procedural macro implements
|
regular Rust `impl` block. Under the hood, the procedural macro implements
|
||||||
the GraphQLType trait.
|
the GraphQLType trait.
|
||||||
|
|
||||||
`impl_object` comes with many features that allow customization of
|
`impl_object` comes with many features that allow customization of
|
||||||
your fields, all of which are detailed below.
|
your fields, all of which are detailed below.
|
||||||
|
|
||||||
### Getting Started
|
### Getting Started
|
||||||
|
|
||||||
This simple example will show you the most basic use of `impl_object`.
|
This simple example will show you the most basic use of `impl_object`.
|
||||||
More advanced use cases are introduced step by step.
|
More advanced use cases are introduced step by step.
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -105,17 +105,17 @@ impl Query {
|
||||||
|
|
||||||
|
|
||||||
// This defines a simple, static field which does not require any context.
|
// This defines a simple, static field which does not require any context.
|
||||||
// You can return any value that implements the `GraphQLType` trait.
|
// You can return any value that implements the `GraphQLType` trait.
|
||||||
// This trait is implemented for:
|
// This trait is implemented for:
|
||||||
// - basic scalar types like bool, &str, String, i32, f64
|
// - basic scalar types like bool, &str, String, i32, f64
|
||||||
// - GraphQL compatible wrappers like Option<_>, Vec<_>.
|
// - GraphQL compatible wrappers like Option<_>, Vec<_>.
|
||||||
// - types which use the `#derive[juniper::GraphQLObject]`
|
// - types which use the `#derive[juniper::GraphQLObject]`
|
||||||
// - `impl_object` structs.
|
// - `impl_object` structs.
|
||||||
//
|
//
|
||||||
// An important note regarding naming:
|
// An important note regarding naming:
|
||||||
// By default, field names will be converted to camel case.
|
// By default, field names will be converted to camel case.
|
||||||
// For your GraphQL queries, the field will be available as `apiVersion`.
|
// For your GraphQL queries, the field will be available as `apiVersion`.
|
||||||
//
|
//
|
||||||
// You can also manually customize the field name if required. (See below)
|
// You can also manually customize the field name if required. (See below)
|
||||||
fn api_version() -> &'static str {
|
fn api_version() -> &'static str {
|
||||||
"0.1"
|
"0.1"
|
||||||
|
@ -169,7 +169,7 @@ You can specify a context that will be available across
|
||||||
all your resolvers during query execution.
|
all your resolvers during query execution.
|
||||||
|
|
||||||
The Context can be injected into your resolvers by just
|
The Context can be injected into your resolvers by just
|
||||||
specifying an argument with the same type as the context
|
specifying an argument with the same type as the context
|
||||||
(but as a reference).
|
(but as a reference).
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -198,11 +198,11 @@ impl Query {
|
||||||
context.db.user(id)
|
context.db.user(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// You can also gain access to the executor, which
|
// You can also gain access to the executor, which
|
||||||
// allows you to do look aheads.
|
// allows you to do look aheads.
|
||||||
fn with_executor(executor: &Executor) -> bool {
|
fn with_executor(executor: &Executor) -> bool {
|
||||||
let info = executor.look_ahead();
|
let info = executor.look_ahead();
|
||||||
// ...
|
// ...
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -611,7 +611,7 @@ pub struct GraphQLTypeDefiniton {
|
||||||
// This flag signifies if the type generics need to be
|
// This flag signifies if the type generics need to be
|
||||||
// included manually.
|
// included manually.
|
||||||
pub include_type_generics: bool,
|
pub include_type_generics: bool,
|
||||||
// This flag indicates if the generated code should always be
|
// This flag indicates if the generated code should always be
|
||||||
// generic over the ScalarValue.
|
// generic over the ScalarValue.
|
||||||
// If false, the scalar is only generic if a generic parameter
|
// If false, the scalar is only generic if a generic parameter
|
||||||
// is specified manually.
|
// is specified manually.
|
||||||
|
@ -744,7 +744,7 @@ impl GraphQLTypeDefiniton {
|
||||||
// A custom scalar type was specified.
|
// A custom scalar type was specified.
|
||||||
// Therefore, we always insert a where clause that marks the scalar as
|
// Therefore, we always insert a where clause that marks the scalar as
|
||||||
// compatible with ScalarValueRef.
|
// compatible with ScalarValueRef.
|
||||||
// This is done to prevent the user from having to specify this
|
// This is done to prevent the user from having to specify this
|
||||||
// manually.
|
// manually.
|
||||||
let where_clause = generics.where_clause.get_or_insert(parse_quote!(where));
|
let where_clause = generics.where_clause.get_or_insert(parse_quote!(where));
|
||||||
where_clause.predicates.push(
|
where_clause.predicates.push(
|
||||||
|
@ -764,9 +764,9 @@ impl GraphQLTypeDefiniton {
|
||||||
// Insert a where clause that marks the scalar as
|
// Insert a where clause that marks the scalar as
|
||||||
// compatible with ScalarValueRef.
|
// compatible with ScalarValueRef.
|
||||||
// Same as in branch above.
|
// Same as in branch above.
|
||||||
where_clause.predicates.push(
|
where_clause
|
||||||
parse_quote!(for<'__b> &'__b __S: #juniper_crate_name::ScalarRefValue<'__b>),
|
.predicates
|
||||||
);
|
.push(parse_quote!(for<'__b> &'__b __S: #juniper_crate_name::ScalarRefValue<'__b>));
|
||||||
}
|
}
|
||||||
|
|
||||||
let type_generics_tokens = if self.include_type_generics {
|
let type_generics_tokens = if self.include_type_generics {
|
||||||
|
|
Loading…
Reference in a new issue