Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for lifetime annotations when using derives #226

Merged
merged 2 commits into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion changelog/master.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 5 additions & 4 deletions juniper_codegen/src/derive_input_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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); },
Expand Down Expand Up @@ -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 = ();

Expand All @@ -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)*
Expand All @@ -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)*
Expand Down
3 changes: 2 additions & 1 deletion juniper_codegen/src/derive_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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 = ();

Expand Down
43 changes: 41 additions & 2 deletions juniper_tests/src/codegen/derive_input_object.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -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"));
Expand Down
5 changes: 5 additions & 0 deletions juniper_tests/src/codegen/derive_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down