Skip to content

Redesign GraphQLScalar macros #1017

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

Merged
merged 92 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 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
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
8cea820
WIP
ilslv Feb 18, 2022
9c58419
Corrections
ilslv Feb 21, 2022
baad84b
Corrections
ilslv Feb 21, 2022
a0686a4
Sync up with main
ilslv Feb 25, 2022
398655c
Merge branch 'master' into new-derive-scalar
ilslv Feb 25, 2022
b9cd040
Remove redundant tests
ilslv Feb 25, 2022
1a07e3e
Corrections, vol.1
tyranron Feb 25, 2022
9fb0a18
Corrections, vol.2
tyranron Feb 28, 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
24 changes: 12 additions & 12 deletions docs/book/content/types/scalars.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Often, you might need a custom scalar that just wraps an existing type.
This can be done with the newtype pattern and a custom derive, similar to how
serde supports this pattern with `#[serde(transparent)]`.

```rust,ignore
```rust
# extern crate juniper;
#
#[derive(juniper::GraphQLScalar)]
Expand All @@ -70,7 +70,7 @@ struct User {
`#[derive(GraphQLScalar)]` is mostly interchangeable with `#[graphql_scalar]`
attribute:

```rust,ignore
```rust
# extern crate juniper;
# use juniper::graphql_scalar;
#
Expand All @@ -91,7 +91,7 @@ That's it, you can now use `UserId` in your schema.

The macro also allows for more customization:

```rust,ignore
```rust
# extern crate juniper;
/// You can use a doc comment to specify a description.
#[derive(juniper::GraphQLScalar)]
Expand All @@ -112,7 +112,7 @@ All the methods used from newtype's field can be replaced with attributes:

### `#[graphql(to_output_with = <fn>)]` attribute

```rust,ignore
```rust
# use juniper::{GraphQLScalar, ScalarValue, Value};
#
#[derive(GraphQLScalar)]
Expand All @@ -129,7 +129,7 @@ fn to_output<S: ScalarValue>(v: &Incremented) -> Value<S> {

### `#[graphql(from_input_with = <fn>)]` attribute

```rust,ignore
```rust
# use juniper::{GraphQLScalar, InputValue, ScalarValue};
#
#[derive(GraphQLScalar)]
Expand Down Expand Up @@ -164,7 +164,7 @@ impl UserId {

### `#[graphql(parse_token_with = <fn>]` or `#[graphql(parse_token(<types>)]` attributes

```rust,ignore
```rust
# use juniper::{
# GraphQLScalar, InputValue, ParseScalarResult, ParseScalarValue,
# ScalarValue, ScalarToken, Value
Expand Down Expand Up @@ -226,7 +226,7 @@ Instead of providing all custom resolvers, you can provide path to the `to_outpu
Path can be simply `with = Self` (default path where macro expects resolvers to be),
in case there is an impl block with custom resolvers:

```rust,ignore
```rust
# use juniper::{
# GraphQLScalar, InputValue, ParseScalarResult, ParseScalarValue,
# ScalarValue, ScalarToken, Value
Expand All @@ -253,7 +253,7 @@ impl StringOrInt {
{
v.as_string_value()
.map(|s| Self::String(s.to_owned()))
.or_else(|| v.as_int_value().map(|i| Self::Int(i)))
.or_else(|| v.as_int_value().map(Self::Int))
.ok_or_else(|| format!("Expected `String` or `Int`, found: {}", v))
}

Expand All @@ -271,7 +271,7 @@ impl StringOrInt {

Or it can be path to a module, where custom resolvers are located.

```rust,ignore
```rust
# use juniper::{
# GraphQLScalar, InputValue, ParseScalarResult, ParseScalarValue,
# ScalarValue, ScalarToken, Value
Expand Down Expand Up @@ -303,7 +303,7 @@ mod string_or_int {
{
v.as_string_value()
.map(|s| StringOrInt::String(s.to_owned()))
.or_else(|| v.as_int_value().map(|i| StringOrInt::Int(i)))
.or_else(|| v.as_int_value().map(StringOrInt::Int))
.ok_or_else(|| format!("Expected `String` or `Int`, found: {}", v))
}

Expand All @@ -321,7 +321,7 @@ mod string_or_int {

Also, you can partially override `#[graphql(with)]` attribute with other custom scalars.

```rust,ignore
```rust
# use juniper::{GraphQLScalar, InputValue, ParseScalarResult, ScalarValue, ScalarToken, Value};
#
#[derive(GraphQLScalar)]
Expand All @@ -348,7 +348,7 @@ impl StringOrInt {
{
v.as_string_value()
.map(|s| Self::String(s.to_owned()))
.or_else(|| v.as_int_value().map(|i| Self::Int(i)))
.or_else(|| v.as_int_value().map(Self::Int))
.ok_or_else(|| format!("Expected `String` or `Int`, found: {}", v))
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use juniper::graphql_scalar;

#[graphql_scalar(specified_by_url = "not an url", transparent)]
struct ScalarSpecifiedByUrl(i32);

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Invalid URL: relative URL without a base
--> fail/scalar/derive_input/attr_invalid_url.rs:3:37
|
3 | #[graphql_scalar(specified_by_url = "not an url", transparent)]
| ^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use juniper::graphql_scalar;

#[graphql_scalar(with = Self, transparent)]
struct Scalar;

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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.
--> fail/scalar/derive_input/attr_transparent_and_with.rs:3:25
|
3 | #[graphql_scalar(with = Self, transparent)]
| ^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use juniper::graphql_scalar;

#[graphql_scalar(transparent)]
struct Scalar {
id: i32,
another: i32,
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: GraphQL scalar expected exactly 1 field, e.g., Test { test: i32 } because of `transparent` attribute
--> fail/scalar/derive_input/attr_transparent_multiple_named_fields.rs:4:1
|
4 | / struct Scalar {
5 | | id: i32,
6 | | another: i32,
7 | | }
| |_^
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use juniper::graphql_scalar;

#[graphql_scalar(transparent)]
struct Scalar(i32, i32);

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: GraphQL scalar expected exactly 1 field, e.g., Test(i32) because of `transparent` attribute
--> fail/scalar/derive_input/attr_transparent_multiple_unnamed_fields.rs:4:1
|
4 | struct Scalar(i32, i32);
| ^^^^^^^^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use juniper::graphql_scalar;

#[graphql_scalar(transparent)]
struct ScalarSpecifiedByUrl;

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: GraphQL scalar expected exactly 1 field, e.g.: `Test(i32)`, `Test { test: i32 }` because of `transparent` attribute
--> fail/scalar/derive_input/attr_transparent_unit_struct.rs:4:1
|
4 | struct ScalarSpecifiedByUrl;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use juniper::GraphQLScalar;

#[derive(GraphQLScalar)]
#[graphql(specified_by_url = "not an url", transparent)]
struct ScalarSpecifiedByUrl(i64);

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Invalid URL: relative URL without a base
--> fail/scalar/derive_input/derive_invalid_url.rs:4:30
|
4 | #[graphql(specified_by_url = "not an url", transparent)]
| ^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use juniper::GraphQLScalar;

#[derive(GraphQLScalar)]
#[graphql(with = Self, transparent)]
struct Scalar;

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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.
--> fail/scalar/derive_input/derive_transparent_and_with.rs:4:18
|
4 | #[graphql(with = Self, transparent)]
| ^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use juniper::GraphQLScalar;

#[derive(GraphQLScalar)]
#[graphql(transparent)]
struct Scalar {
id: i32,
another: i32,
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error: GraphQL scalar expected exactly 1 field, e.g., Test { test: i32 } because of `transparent` attribute
--> fail/scalar/derive_input/derive_transparent_multiple_named_fields.rs:4:1
|
4 | / #[graphql(transparent)]
5 | | struct Scalar {
6 | | id: i32,
7 | | another: i32,
8 | | }
| |_^
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use juniper::GraphQLScalar;

#[derive(GraphQLScalar)]
#[graphql(transparent)]
struct Scalar(i32, i32);

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error: GraphQL scalar expected exactly 1 field, e.g., Test(i32) because of `transparent` attribute
--> fail/scalar/derive_input/derive_transparent_multiple_unnamed_fields.rs:4:1
|
4 | / #[graphql(transparent)]
5 | | struct Scalar(i32, i32);
| |________________________^
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use juniper::GraphQLScalar;

#[derive(GraphQLScalar)]
#[graphql(transparent)]
struct ScalarSpecifiedByUrl;

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error: GraphQL scalar expected exactly 1 field, e.g.: `Test(i32)`, `Test { test: i32 }` because of `transparent` attribute
--> fail/scalar/derive_input/derive_transparent_unit_struct.rs:4:1
|
4 | / #[graphql(transparent)]
5 | | struct ScalarSpecifiedByUrl;
| |____________________________^

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: Invalid URL: relative URL without a base
--> fail/scalar/type_alias/impl_invalid_url.rs:6:24
--> fail/scalar/type_alias/attr_invalid_url.rs:6:24
|
6 | specified_by_url = "not an url",
| ^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: GraphQL scalar all custom resolvers have to be provided via `with` or combination of `to_output_with`, `from_input_with`, `parse_token_with` attributes
--> fail/scalar/type_alias/impl_with_not_all_resolvers.rs:6:1
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
--> fail/scalar/type_alias/attr_with_not_all_resolvers.rs:6:1
|
6 | type CustomScalar = Scalar;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: GraphQL scalar all custom resolvers have to be provided via `with` or combination of `to_output_with`, `from_input_with`, `parse_token_with` attributes
--> fail/scalar/type_alias/impl_without_resolvers.rs:6:1
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
--> fail/scalar/type_alias/attr_without_resolvers.rs:6:1
|
6 | type CustomScalar = Scalar;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1 change: 1 addition & 0 deletions integration_tests/juniper_tests/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod object_attr;
mod object_derive;
mod scalar_attr_derive_input;
mod scalar_attr_type_alias;
mod scalar_derive;
mod subscription_attr;
mod union_attr;
mod union_derive;
Loading