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

Implement ScalarValue derive macro #1025

Merged
merged 104 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from 99 commits
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
01d7cb8
WIP
ilslv Dec 20, 2021
205cfcd
WIP
ilslv Dec 20, 2021
928cb9c
WIP (with IsSubtype)
ilslv Dec 20, 2021
3dad162
WIP (It looks like we're onto something)
ilslv Dec 20, 2021
af04486
WIP (return to const assertions)
ilslv Dec 21, 2021
15e527f
It's alive!
ilslv Dec 24, 2021
cb9cad5
Merge branch 'master' into 1000-new-interfaces
ilslv Dec 24, 2021
7a78f9e
Use Field inside object's field resolver
ilslv Dec 24, 2021
5c2087c
Add async object fields
ilslv Dec 24, 2021
e9c6046
Fix
ilslv Dec 24, 2021
0739c3e
Fix Wrapped type for tuple with context
ilslv Dec 27, 2021
263d544
WIP (Make graphql_interface_new attribute work)
ilslv Dec 28, 2021
f4c48a6
WIP (move to the new tests volume 1)
ilslv Dec 28, 2021
a1df387
WIP (move to the new tests volume 2)
ilslv Dec 29, 2021
5040d68
WIP (move to the new tests volume 3)
ilslv Dec 29, 2021
f5533e3
WIP (move to the new tests volume 4)
ilslv Dec 29, 2021
527325c
WIP (move to the new tests volume 5)
ilslv Dec 29, 2021
704f079
WIP (move to the new tests volume 5)
ilslv Dec 29, 2021
b84009e
WIP (refactor)
ilslv Dec 30, 2021
11345f7
WIP (refactor)
ilslv Dec 30, 2021
5e7230f
WIP (moving to readable const asserts)
ilslv Dec 30, 2021
d17ba4f
WIP (finally good const assert message)
ilslv Dec 30, 2021
8cb9b81
WIP (corrections)
ilslv Dec 30, 2021
116cdd9
WIP (pretty field arguments check)
ilslv Jan 3, 2022
7bd8c73
WIP (pretty subtype check)
ilslv Jan 3, 2022
3ebc8fd
WIP (corrections)
ilslv Jan 3, 2022
ce249b0
WIP (FieldMeta trait)
ilslv Jan 3, 2022
d9971a0
WIP (Refactor graphql_interface_new a bit)
ilslv Jan 3, 2022
8a966fb
WIP (Corrections)
ilslv Jan 3, 2022
21f4940
WIP (Prettify non-existent Field assertions)
ilslv Jan 4, 2022
e03901b
WIP (Move to macros::reflection module)
ilslv Jan 4, 2022
3ad8399
WIP (Docs vol. 1)
ilslv Jan 4, 2022
463bbb8
WIP (Docs vol. 2)
ilslv Jan 4, 2022
26ab27e
WIP (Refactoring)
ilslv Jan 4, 2022
cddb252
Merge branch 'master' into 1000-new-interfaces
ilslv Jan 4, 2022
9977746
WIP (Refactoring)
ilslv Jan 4, 2022
f5340a7
WIP (Tests corrections)
ilslv Jan 4, 2022
4c31c13
WIP (More tests vol. 1)
ilslv Jan 4, 2022
7bd09d0
WIP (Move everything to the new graphql_interface macro)
ilslv Jan 5, 2022
b5f552a
WIP (More codegen_fail tests)
ilslv Jan 5, 2022
66819e9
WIP (prettify missing references in `impl` and `for` attributes)
ilslv Jan 5, 2022
3d53dc1
WIP (more tests)
ilslv Jan 5, 2022
732bc13
WIP (Correct docs)
ilslv Jan 6, 2022
81d96ce
WIP (Forbid default trait method impls)
ilslv Jan 6, 2022
94164eb
WIP (Corrections)
ilslv Jan 6, 2022
1345bff
WIP (Corrections)
ilslv Jan 6, 2022
6cb5cef
WIP (Corrections)
ilslv Jan 6, 2022
41a2dcf
WIP (CHANGELOG)
ilslv Jan 6, 2022
eb8c56d
WIP (Correction)
ilslv Jan 6, 2022
7f45aef
WIP
ilslv Jan 10, 2022
81c4df9
Migrate to real GraphQLScalar trait
ilslv Jan 10, 2022
d359524
Corrections
ilslv Jan 10, 2022
888789a
Corrections
ilslv Jan 11, 2022
e28ca92
Add generic test and remove redundant lifetime
ilslv Jan 11, 2022
f531037
Some corrections [skip ci]
tyranron Jan 11, 2022
8f169cf
WIP
ilslv Jan 12, 2022
a2287dc
Minor tests corrections
ilslv Jan 12, 2022
ceb2cf6
Don't inject `#[async_trait]` in case some trait methods are async
ilslv Jan 12, 2022
d77a1ab
Corrections
ilslv Jan 13, 2022
8be555c
Merge branch '1000-new-interfaces' into generic-scalar
ilslv Jan 13, 2022
ebc0149
Merge branch 'generic-scalar' into new-derive-scalar
ilslv Jan 13, 2022
8fb4587
WIP
ilslv Jan 13, 2022
52c14b2
WIP
ilslv Jan 13, 2022
4b6df34
Corrections
ilslv Jan 14, 2022
0cb101d
Corrections
ilslv Jan 14, 2022
8d47ba7
Merge branch '1000-new-interfaces' into generic-scalar
ilslv Jan 14, 2022
22b3fe2
Merge branch 'generic-scalar' into new-derive-scalar
ilslv Jan 14, 2022
7635a1e
Corrections
ilslv Jan 14, 2022
3da33ba
Corrections
ilslv Jan 14, 2022
cc365d3
Corrections
ilslv Jan 14, 2022
30117a8
Docs and the book
ilslv Jan 14, 2022
2362b27
Fix The Book
ilslv Jan 14, 2022
193c77b
Merge branch 'master' into generic-scalar
ilslv Jan 28, 2022
86f1c10
CHANGELOG
ilslv Jan 28, 2022
7db2adc
Sprinkle `#[automatically_derived]` attributes
ilslv Jan 28, 2022
859b3be
Merge branch 'generic-scalar' into new-derive-scalar
ilslv Jan 28, 2022
6fb0dbf
Implement `#[graphql(with)]` attribute
ilslv Feb 2, 2022
f063728
CHANGELOG
ilslv Feb 2, 2022
10bea73
Implement `GraphQLScalarValue` derive macro
ilslv Feb 3, 2022
512f377
Add `codegen_fail` tests
ilslv Feb 3, 2022
60fcbbc
WIP
ilslv Feb 4, 2022
8b3e80a
Merge branch 'master' into new-derive-scalar
ilslv Feb 4, 2022
8706043
Corrections and tests
ilslv Feb 4, 2022
5b96861
Correction
ilslv Feb 4, 2022
81bb650
WIP
ilslv Feb 15, 2022
b4df74a
Corrections
ilslv Feb 15, 2022
2c5cf99
Merge branch 'new-derive-scalar' into derive-scalar-value
ilslv Feb 15, 2022
aed11f9
Corrections
ilslv Feb 15, 2022
a5cf713
CHANGELOG
ilslv Feb 15, 2022
5cc24db
CHANGELOG
ilslv Feb 15, 2022
8cea820
WIP
ilslv Feb 18, 2022
9c58419
Corrections
ilslv Feb 21, 2022
baad84b
Corrections
ilslv Feb 21, 2022
8ebd520
Merge branch 'new-derive-scalar' into derive-scalar-value
ilslv Feb 21, 2022
a2ecb44
Corrections
ilslv Feb 21, 2022
9144d02
Corrections
ilslv Feb 21, 2022
a7d4b28
Corrections
ilslv Feb 25, 2022
28bd546
Merge branch 'master' into derive-scalar-value
ilslv Mar 1, 2022
aa8a345
Corrections
ilslv Mar 1, 2022
fde884b
Minor corrections
tyranron Mar 1, 2022
65c104f
Rename `GraphQLScalarValue` to `ScalarValue`
ilslv Mar 2, 2022
dd06dc6
Rename `#[graphql]` to `#[scalar_valur]` in `ScalarValue` derive macro
ilslv Mar 2, 2022
d445f90
Rename `#[scalar_value]` to `#[value]` in `ScalarValue` derive macro
ilslv Mar 2, 2022
cfa549a
Corrections
tyranron Mar 2, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ error[E0277]: the trait bound `[bool; 2]: From<[bool; 3]>` is not satisfied
| ^^^^^^^^^^^^^^^^^^^^ the trait `From<[bool; 3]>` is not implemented for `[bool; 2]`
|
= help: the following implementations were found:
<&'a [ascii::ascii_char::AsciiChar] as From<&'a ascii::ascii_str::AsciiStr>>
<&'a [u8] as From<&'a ascii::ascii_str::AsciiStr>>
<&'a mut [ascii::ascii_char::AsciiChar] as From<&'a mut ascii::ascii_str::AsciiStr>>
<[T; LANES] as From<Simd<T, LANES>>>
<[bool; LANES] as From<Mask<T, LANES>>>
= note: required because of the requirements on the impl of `Into<[bool; 2]>` for `[bool; 3]`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ error[E0277]: the trait bound `[bool; 2]: From<[bool; 3]>` is not satisfied
| ^^^^^^^^^^^^^^^^^ the trait `From<[bool; 3]>` is not implemented for `[bool; 2]`
|
= help: the following implementations were found:
<&'a [ascii::ascii_char::AsciiChar] as From<&'a ascii::ascii_str::AsciiStr>>
<&'a [u8] as From<&'a ascii::ascii_str::AsciiStr>>
<&'a mut [ascii::ascii_char::AsciiChar] as From<&'a mut ascii::ascii_str::AsciiStr>>
<[T; LANES] as From<Simd<T, LANES>>>
<[bool; LANES] as From<Mask<T, LANES>>>
= note: required because of the requirements on the impl of `Into<[bool; 2]>` for `[bool; 3]`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: GraphQL scalar `with = <path>` attribute can\'t be combined with `transparent`. You can specify custom resolvers with `to_output`, `from_input`, `parse_token` attributes and still use `transparent` for unspecified ones.
error: GraphQL scalar `with = <path>` attribute argument cannot be combined with `transparent`. You can specify custom resolvers with `to_output_with`, `from_input_with`, `parse_token`/`parse_token_with` attribute arguments and still use `transparent` for unspecified ones.
--> fail/scalar/derive_input/attr_transparent_and_with.rs:3:25
|
3 | #[graphql_scalar(with = Self, transparent)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: GraphQL scalar expected exactly 1 field, e.g., Test { test: i32 } because of `transparent` attribute
error: GraphQL scalar `transparent` attribute argument requires exactly 1 field
--> fail/scalar/derive_input/attr_transparent_multiple_named_fields.rs:4:1
|
4 | / struct Scalar {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: GraphQL scalar expected exactly 1 field, e.g., Test(i32) because of `transparent` attribute
error: GraphQL scalar `transparent` attribute argument requires exactly 1 field
--> fail/scalar/derive_input/attr_transparent_multiple_unnamed_fields.rs:4:1
|
4 | struct Scalar(i32, i32);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: GraphQL scalar expected exactly 1 field, e.g.: `Test(i32)`, `Test { test: i32 }` because of `transparent` attribute
error: GraphQL scalar `transparent` attribute argument requires exactly 1 field
--> fail/scalar/derive_input/attr_transparent_unit_struct.rs:4:1
|
4 | struct ScalarSpecifiedByUrl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: GraphQL scalar `with = <path>` attribute can\'t be combined with `transparent`. You can specify custom resolvers with `to_output`, `from_input`, `parse_token` attributes and still use `transparent` for unspecified ones.
error: GraphQL scalar `with = <path>` attribute argument cannot be combined with `transparent`. You can specify custom resolvers with `to_output_with`, `from_input_with`, `parse_token`/`parse_token_with` attribute arguments and still use `transparent` for unspecified ones.
--> fail/scalar/derive_input/derive_transparent_and_with.rs:4:18
|
4 | #[graphql(with = Self, transparent)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: GraphQL scalar expected exactly 1 field, e.g., Test { test: i32 } because of `transparent` attribute
error: GraphQL scalar `transparent` attribute argument requires exactly 1 field
--> fail/scalar/derive_input/derive_transparent_multiple_named_fields.rs:4:1
|
4 | / #[graphql(transparent)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: GraphQL scalar expected exactly 1 field, e.g., Test(i32) because of `transparent` attribute
error: GraphQL scalar `transparent` attribute argument requires exactly 1 field
--> fail/scalar/derive_input/derive_transparent_multiple_unnamed_fields.rs:4:1
|
4 | / #[graphql(transparent)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: GraphQL scalar expected exactly 1 field, e.g.: `Test(i32)`, `Test { test: i32 }` because of `transparent` attribute
error: GraphQL scalar `transparent` attribute argument requires exactly 1 field
--> fail/scalar/derive_input/derive_transparent_unit_struct.rs:4:1
|
4 | / #[graphql(transparent)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: GraphQL scalar all custom resolvers have to be provided via `with` or combination of `to_output_with`, `from_input_with`, `parse_token_with` attribute arguments
error: GraphQL scalar all the resolvers have to be provided via `with` attribute argument or a combination of `to_output_with`, `from_input_with`, `parse_token_with`/`parse_token` attribute arguments
--> fail/scalar/type_alias/attr_with_not_all_resolvers.rs:6:1
|
6 | type CustomScalar = Scalar;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: GraphQL scalar all custom resolvers have to be provided via `with` or combination of `to_output_with`, `from_input_with`, `parse_token_with` attribute arguments
error: GraphQL scalar all the resolvers have to be provided via `with` attribute argument or a combination of `to_output_with`, `from_input_with`, `parse_token_with`/`parse_token` attribute arguments
--> fail/scalar/type_alias/attr_without_resolvers.rs:6:1
|
6 | type CustomScalar = Scalar;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[derive(juniper::GraphQLScalarValue)]
struct ScalarValue;

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: GraphQL built-in scalars can only be derived for enums
--> fail/scalar_value/derive_on_struct.rs:2:1
|
2 | struct ScalarValue;
| ^^^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use juniper::GraphQLScalarValue;

#[derive(Clone, Debug, GraphQLScalarValue, PartialEq)]
pub enum DefaultScalarValue {
Int(i32),
Float(f64),
#[graphql(as_str, as_string, into_string)]
String(String),
#[graphql(as_boolean)]
Boolean(bool),
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: GraphQL built-in scalars missing `#[graphql(as_int, as_float)]` attributes. In case you are sure that it is ok, use `#[graphql(allow_missing_attributes)]` to suppress this error.
--> fail/scalar_value/missing_attributes.rs:4:1
|
4 | / pub enum DefaultScalarValue {
5 | | Int(i32),
6 | | Float(f64),
7 | | #[graphql(as_str, as_string, into_string)]
... |
10 | | Boolean(bool),
11 | | }
| |_^
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[derive(juniper::GraphQLScalarValue)]
enum ScalarValue {
Variant { first: i32, second: u64 },
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: GraphQL built-in scalars expected exactly 1 field
--> fail/scalar_value/multiple_named_fields.rs:3:13
|
3 | Variant { first: i32, second: u64 },
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[derive(juniper::GraphQLScalarValue)]
enum ScalarValue {
Variant(u32, i64),
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: GraphQL built-in scalars expected exactly 1 field
--> fail/scalar_value/multiple_unnamed_fields.rs:3:12
|
3 | Variant(u32, i64),
| ^^^^^^^^^^
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ error[E0277]: the trait bound `[bool; 2]: From<[bool; 3]>` is not satisfied
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<[bool; 3]>` is not implemented for `[bool; 2]`
|
= help: the following implementations were found:
<&'a [ascii::ascii_char::AsciiChar] as From<&'a ascii::ascii_str::AsciiStr>>
<&'a [u8] as From<&'a ascii::ascii_str::AsciiStr>>
<&'a mut [ascii::ascii_char::AsciiChar] as From<&'a mut ascii::ascii_str::AsciiStr>>
<[T; LANES] as From<Simd<T, LANES>>>
<[bool; LANES] as From<Mask<T, LANES>>>
= note: required because of the requirements on the impl of `Into<[bool; 2]>` for `[bool; 3]`
Expand Down
136 changes: 136 additions & 0 deletions integration_tests/juniper_tests/src/codegen/derive_scalar_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
use juniper::{DefaultScalarValue, GraphQLScalarValue, ScalarValue as _};
use serde::{Deserialize, Serialize};

mod trivial {
use super::*;

#[derive(Clone, Debug, Deserialize, GraphQLScalarValue, PartialEq, Serialize)]
#[serde(untagged)]
pub enum CustomScalarValue {
#[graphql(as_int, as_float)]
Int(i32),
#[graphql(as_float)]
Float(f64),
#[graphql(as_str, as_string, into_string)]
String(String),
#[graphql(as_boolean)]
Boolean(bool),
}

#[test]
fn into_another() {
assert!(CustomScalarValue::from(5)
.into_another::<DefaultScalarValue>()
.is_type::<i32>());
assert!(CustomScalarValue::from(0.5_f64)
.into_another::<DefaultScalarValue>()
.is_type::<f64>());
assert!(CustomScalarValue::from("str".to_owned())
.into_another::<DefaultScalarValue>()
.is_type::<String>());
assert!(CustomScalarValue::from(true)
.into_another::<DefaultScalarValue>()
.is_type::<bool>());
}
}

mod named_fields {
use super::*;

#[derive(Clone, Debug, Deserialize, GraphQLScalarValue, PartialEq, Serialize)]
#[serde(untagged)]
pub enum CustomScalarValue {
#[graphql(as_int, as_float)]
Int { int: i32 },
#[graphql(as_float)]
Float(f64),
#[graphql(as_str, as_string, into_string)]
String(String),
#[graphql(as_boolean)]
Boolean { v: bool },
}

#[test]
fn into_another() {
assert!(CustomScalarValue::from(5)
.into_another::<DefaultScalarValue>()
.is_type::<i32>());
assert!(CustomScalarValue::from(0.5_f64)
.into_another::<DefaultScalarValue>()
.is_type::<f64>());
assert!(CustomScalarValue::from("str".to_owned())
.into_another::<DefaultScalarValue>()
.is_type::<String>());
assert!(CustomScalarValue::from(true)
.into_another::<DefaultScalarValue>()
.is_type::<bool>());
}
}

mod custom_fn {
use super::*;

#[derive(Clone, Debug, Deserialize, GraphQLScalarValue, PartialEq, Serialize)]
#[serde(untagged)]
pub enum CustomScalarValue {
#[graphql(as_int, as_float)]
Int(i32),
#[graphql(as_float)]
Float(f64),
#[graphql(
as_str,
as_string = str::to_owned,
into_string = std::convert::identity,
)]
String(String),
#[graphql(as_boolean)]
Boolean(bool),
}

#[test]
fn into_another() {
assert!(CustomScalarValue::from(5)
.into_another::<DefaultScalarValue>()
.is_type::<i32>());
assert!(CustomScalarValue::from(0.5_f64)
.into_another::<DefaultScalarValue>()
.is_type::<f64>());
assert!(CustomScalarValue::from("str".to_owned())
.into_another::<DefaultScalarValue>()
.is_type::<String>());
assert!(CustomScalarValue::from(true)
.into_another::<DefaultScalarValue>()
.is_type::<bool>());
}
}

mod allow_missing_attributes {
use super::*;

#[derive(Clone, Debug, Deserialize, GraphQLScalarValue, PartialEq, Serialize)]
#[graphql(allow_missing_attributes)]
#[serde(untagged)]
pub enum CustomScalarValue {
Int(i32),
#[graphql(as_float)]
Float(f64),
#[graphql(as_str, as_string, into_string)]
String(String),
#[graphql(as_boolean)]
Boolean(bool),
}

#[test]
fn into_another() {
assert!(CustomScalarValue::Int(5).as_int().is_none());
assert!(CustomScalarValue::from(0.5_f64)
.into_another::<DefaultScalarValue>()
.is_type::<f64>());
assert!(CustomScalarValue::from("str".to_owned())
.into_another::<DefaultScalarValue>()
.is_type::<String>());
assert!(CustomScalarValue::from(true)
.into_another::<DefaultScalarValue>()
.is_type::<bool>());
}
}
1 change: 1 addition & 0 deletions integration_tests/juniper_tests/src/codegen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod derive_enum;
mod derive_input_object;
mod derive_object_with_raw_idents;
mod derive_scalar_value;
mod interface_attr;
mod object_attr;
mod object_derive;
Expand Down
Loading