Add support for lifetime annotations when using derives (#226)

Fixes https://github.com/graphql-rust/juniper/issues/225
This commit is contained in:
Christian Legnitto 2018-08-27 15:25:15 -07:00 committed by GitHub
parent 22c955599a
commit 08c31357af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 8 deletions

View file

@ -45,10 +45,16 @@
[#219](https://github.com/graphql-rust/juniper/pull/219)
* The `GraphQLObject` and `GraphQLInputObject` custom derives
now support lifetime annotations.
[#225](https://github.com/graphql-rust/juniper/issues/225)
* When using the `GraphQLObject` custom derive, fields now be omitted by annotating the field with `#[graphql(skip)]`.
[#220](https://github.com/graphql-rust/juniper/issues/220)
* Due to newer dependencies, the oldest Rust version supported is now 1.22.0
[#231](https://github.com/graphql-rust/juniper/pull/231)
[#231](https://github.com/graphql-rust/juniper/pull/231)

View file

@ -132,6 +132,7 @@ pub fn impl_input_object(ast: &syn::DeriveInput) -> TokenStream {
let ident = &ast.ident;
let attrs = ObjAttrs::from_input(ast);
let name = attrs.name.unwrap_or(ast.ident.to_string());
let generics = &ast.generics;
let meta_description = match attrs.description {
Some(descr) => quote!{ let meta = meta.description(#descr); },
@ -240,7 +241,7 @@ pub fn impl_input_object(ast: &syn::DeriveInput) -> TokenStream {
}
let body = quote! {
impl _juniper::GraphQLType for #ident {
impl #generics _juniper::GraphQLType for #ident #generics {
type Context = ();
type TypeInfo = ();
@ -261,8 +262,8 @@ pub fn impl_input_object(ast: &syn::DeriveInput) -> TokenStream {
}
}
impl _juniper::FromInputValue for #ident {
fn from_input_value(value: &_juniper::InputValue) -> Option<#ident> {
impl #generics _juniper::FromInputValue for #ident #generics {
fn from_input_value(value: &_juniper::InputValue) -> Option<#ident #generics> {
if let Some(obj) = value.to_object_value() {
let item = #ident {
#(#from_inputs)*
@ -275,7 +276,7 @@ pub fn impl_input_object(ast: &syn::DeriveInput) -> TokenStream {
}
}
impl _juniper::ToInputValue for #ident {
impl #generics _juniper::ToInputValue for #ident #generics {
fn to_input_value(&self) -> _juniper::InputValue {
_juniper::InputValue::object(vec![
#(#to_inputs)*

View file

@ -111,6 +111,7 @@ pub fn impl_object(ast: &syn::DeriveInput) -> TokenStream {
// Parse attributes.
let ident = &ast.ident;
let generics = &ast.generics;
let ident_name = ident.to_string();
let attrs = ObjAttrs::from_input(ast);
let name = attrs.name.unwrap_or(ast.ident.to_string());
@ -170,7 +171,7 @@ pub fn impl_object(ast: &syn::DeriveInput) -> TokenStream {
}
let toks = quote! {
impl ::juniper::GraphQLType for #ident {
impl #generics ::juniper::GraphQLType for #ident #generics {
type Context = ();
type TypeInfo = ();

View file

@ -1,8 +1,7 @@
#[cfg(test)]
use fnv::FnvHashMap;
#[cfg(test)]
use juniper::{self, FromInputValue, GraphQLType, InputValue};
use juniper::{self, FromInputValue, GraphQLType, InputValue, ToInputValue};
#[derive(GraphQLInputObject, Debug, PartialEq)]
#[graphql(name = "MyInput", description = "input descr")]
@ -42,6 +41,46 @@ struct OverrideDocComment {
regular_field: bool,
}
#[derive(Debug, PartialEq)]
struct Fake;
impl<'a> FromInputValue for &'a Fake {
fn from_input_value(_v: &InputValue) -> Option<&'a Fake> {
None
}
}
impl<'a> ToInputValue for &'a Fake {
fn to_input_value(&self) -> InputValue {
InputValue::string("this is fake".to_string())
}
}
impl<'a> GraphQLType for &'a Fake {
type Context = ();
type TypeInfo = ();
fn name(_: &()) -> Option<&'static str> {
None
}
fn meta<'r>(_: &(), registry: &mut juniper::Registry<'r>) -> juniper::meta::MetaType<'r> {
let meta = registry.build_enum_type::<&'a Fake>(
&(),
&[juniper::meta::EnumValue {
name: "fake".to_string(),
description: None,
deprecation_reason: None,
}],
);
meta.into_meta()
}
}
#[derive(GraphQLInputObject, Debug, PartialEq)]
struct WithLifetime<'a> {
regular_field: &'a Fake,
}
#[test]
fn test_derived_input_object() {
assert_eq!(Input::name(&()), Some("MyInput"));

View file

@ -48,6 +48,11 @@ struct OverrideDocComment {
regular_field: bool,
}
#[derive(GraphQLObject, Debug, PartialEq)]
struct WithLifetime<'a> {
regular_field: &'a i32,
}
#[derive(GraphQLObject, Debug, PartialEq)]
struct SkippedFieldObj {
regular_field: bool,