-
Notifications
You must be signed in to change notification settings - Fork 433
Macro for InputValue
creation
#996
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
Conversation
@ilslv it would be better also to move macros definitions into their own modules. |
@tyranron graphql_input_value!({
"key": 123,
"var": let variable,
"enum": enum ENUM,
"arr": [123, let another, enum OTHER]
}); |
@ilslv it's seems that for enums there should be just an So, we have the following options here:
Considering that this feature is primarily for internal usage, what do you prefer? |
@tyranron great idea with |
@ilslv ok, let's do this way then.
Maybe |
FCM
@tyranron this PR is ready for review. I've left support for creating #[test]
fn option_support() {
let val = Some(42);
assert_eq!(graphql_value!(None), V::Null);
assert_eq!(graphql_value!(null), V::Null);
assert_eq!(graphql_value!(Some(42)), V::scalar(42));
assert_eq!(graphql_value!(val), V::scalar(42));
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ilslv despite the described nitpickts, you've done a brilliant work with macros implementation. Thanks!
juniper/src/executor/macros.rs
Outdated
({}) => {{ ::std::collections::HashMap::<::std::string::String, _>::new() }}; | ||
|
||
({ $($map:tt)* }) => {{ | ||
let mut object = ::std::collections::HashMap::<::std::string::String, _>::new(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to reuse the type alias here, referring to it as to an opaque type. This will allow us to unlikely mess with the macro once the implemantion of Variables
changes.
juniper/src/executor/look_ahead.rs
Outdated
@@ -743,7 +743,7 @@ query Hero($episode: Episode) { | |||
|
|||
if let crate::ast::Definition::Operation(ref op) = docs[0] { | |||
let mut vars = Variables::default(); | |||
vars.insert("episode".into(), InputValue::Enum("JEDI".into())); | |||
vars.insert("episode".into(), graphql_input_value!(JEDI)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We better use graphql_vars!()
here.
juniper/src/executor/macros.rs
Outdated
fn scalar() { | ||
let val = 42; | ||
assert_eq!( | ||
graphql_vars!({ "key": 123 }), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as we always have a mapping here, we may strip redundant braces and have only graphql_vars! { "key": 123 }
like indexmap!
does.
@@ -235,7 +235,7 @@ async fn querying_long_variable() { | |||
"query q($test: Long!){ longWithArg(longArg: $test) }", | |||
vec![( | |||
"test".to_owned(), | |||
InputValue::Scalar(MyScalarValue::Long(i64::from(i32::MAX) + 42)), | |||
graphql_input_value!(i64::from(i32::MAX) + 42), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's very strange why CI has passed, but running cargo test -p juniper_tests --all-features
locally fails to compile with the following error:
error[E0277]: the trait bound `InputValue<_>: From<i64>` is not satisfied
--> integration_tests/juniper_tests/src/custom_scalar.rs:238:13
|
238 | graphql_input_value!(i64::from(i32::MAX) + 42),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<i64>` is not implemented for `InputValue<_>`
|
= help: the following implementations were found:
<InputValue<S> as From<&'a str>>
<InputValue<S> as From<bool>>
<InputValue<S> as From<f64>>
<InputValue<S> as From<i32>>
and 2 others
note: required by `std::convert::From::from`
--> ~/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/convert/mod.rs:371:5
|
371 | fn from(_: T) -> Self;
| ^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the macro `graphql_input_value` (in Nightly builds, run with -Z macro-backtrace for more info)
It would be nicer to have transparent conversion or some kind forwarding wrapper here, because custom scalars may have additional From
impls to the ones you've provided for InputValue
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to be quite a rabbit hole with a wrapper, cause due to lack of specialization we cannot structurally propagate in Option<_>
, and so it won't work with expressions inside macro.
The easiest way will be just use InputValue::scalar(i64)
when calling the macro, without any additions.
Resolves #503
Synopsis
We have a
graphql_value!()
macro which allowsValue
s creation in a convenient way.We don't have similar for
InputValue
s andVariables
, however. Which makes test cases unergonomic, harder to write and read.Solution
Introduce
graphql_input_value!()
andgraphql_vars!()
macros for that purpose.Additionally
While
graphql_value!()
exists, it has some inconveniences at the moment:None
fornull
values. It would be better to followserde_json::json!()
macro here and acceptnull
s too instead ofNone
.To cover those needs, it is worth to look at how
serde_json::json!()
is implemented and repeat the simplified version for our needs, both forgraphql_input_value!()
andgraphql_value!()
macros.