Skip to content

Commit 63198cd

Browse files
authored
Redesign #[graphql_scalar] macro (#1014, #1000)
- support generic scalars - make it applicable to type aliases and struct/enums/unions
1 parent 3a70403 commit 63198cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+4297
-2041
lines changed

docs/book/content/types/scalars.md

+309-41
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
error[E0119]: conflicting implementations of trait `<CharacterValueEnum<ObjA, ObjA> as juniper::GraphQLInterface<__S>>::mark::_::{closure#0}::MutuallyExclusive` for type `ObjA`
1+
error[E0119]: conflicting implementations of trait `std::convert::From<ObjA>` for type `CharacterValueEnum<ObjA, ObjA>`
22
--> fail/interface/implementers_duplicate_ugly.rs:11:1
33
|
44
11 | #[graphql_interface(for = [ObjA, ObjAlias])]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
| |
77
| first implementation here
8-
| conflicting implementation for `ObjA`
8+
| conflicting implementation for `CharacterValueEnum<ObjA, ObjA>`
99
|
10-
= note: this error originates in the macro `::juniper::sa::assert_type_ne_all` (in Nightly builds, run with -Z macro-backtrace for more info)
10+
= note: this error originates in the attribute macro `graphql_interface` (in Nightly builds, run with -Z macro-backtrace for more info)
1111

12-
error[E0119]: conflicting implementations of trait `std::convert::From<ObjA>` for type `CharacterValueEnum<ObjA, ObjA>`
12+
error[E0119]: conflicting implementations of trait `<CharacterValueEnum<ObjA, ObjA> as juniper::GraphQLInterface<__S>>::mark::_::{closure#0}::MutuallyExclusive` for type `ObjA`
1313
--> fail/interface/implementers_duplicate_ugly.rs:11:1
1414
|
1515
11 | #[graphql_interface(for = [ObjA, ObjAlias])]
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1717
| |
1818
| first implementation here
19-
| conflicting implementation for `CharacterValueEnum<ObjA, ObjA>`
19+
| conflicting implementation for `ObjA`
2020
|
21-
= note: this error originates in the attribute macro `graphql_interface` (in Nightly builds, run with -Z macro-backtrace for more info)
21+
= note: this error originates in the macro `::juniper::sa::assert_type_ne_all` (in Nightly builds, run with -Z macro-backtrace for more info)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use juniper::{graphql_scalar, InputValue, ScalarValue, Value};
2+
3+
#[graphql_scalar(specified_by_url = "not an url", parse_token(i32))]
4+
struct ScalarSpecifiedByUrl(i32);
5+
6+
impl ScalarSpecifiedByUrl {
7+
fn to_output<S: ScalarValue>(&self) -> Value<S> {
8+
Value::scalar(0)
9+
}
10+
11+
fn from_input<S: ScalarValue>(_: &InputValue<S>) -> Result<Self, String> {
12+
Ok(Self)
13+
}
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: Invalid URL: relative URL without a base
2+
--> fail/scalar/derive_input/impl_invalid_url.rs:3:37
3+
|
4+
3 | #[graphql_scalar(specified_by_url = "not an url", parse_token(i32))]
5+
| ^^^^^^^^^^^^
6+
7+
error[E0412]: cannot find type `ScalarSpecifiedByUrl` in this scope
8+
--> fail/scalar/derive_input/impl_invalid_url.rs:6:6
9+
|
10+
6 | impl ScalarSpecifiedByUrl {
11+
| ^^^^^^^^^^^^^^^^^^^^ not found in this scope
12+
13+
error: the `Self` constructor can only be used with tuple or unit structs
14+
--> fail/scalar/derive_input/impl_invalid_url.rs:12:12
15+
|
16+
12 | Ok(Self)
17+
| ^^^^

integration_tests/codegen_fail/fail/scalar/derive_invalid_url.rs

-7
This file was deleted.

integration_tests/codegen_fail/fail/scalar/derive_invalid_url.stderr

-5
This file was deleted.

integration_tests/codegen_fail/fail/scalar/impl_invalid_url.rs

-22
This file was deleted.

integration_tests/codegen_fail/fail/scalar/impl_invalid_url.stderr

-5
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use juniper::{graphql_scalar, InputValue, ScalarValue, Value};
2+
3+
struct ScalarSpecifiedByUrl;
4+
5+
#[graphql_scalar(
6+
specified_by_url = "not an url",
7+
with = scalar,
8+
parse_token(i32),
9+
)]
10+
type Scalar = ScalarSpecifiedByUrl;
11+
12+
mod scalar {
13+
use super::*;
14+
15+
pub(super) fn to_output<S: ScalarValue>(_: &ScalarSpecifiedByUrl) -> Value<S> {
16+
Value::scalar(0)
17+
}
18+
19+
pub(super) fn from_input<S: ScalarValue>(
20+
_: &InputValue<S>,
21+
) -> Result<ScalarSpecifiedByUrl, String> {
22+
Ok(ScalarSpecifiedByUrl)
23+
}
24+
}
25+
26+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: Invalid URL: relative URL without a base
2+
--> fail/scalar/type_alias/impl_invalid_url.rs:6:24
3+
|
4+
6 | specified_by_url = "not an url",
5+
| ^^^^^^^^^^^^
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use juniper::{graphql_scalar, Value};
2+
3+
struct Scalar;
4+
5+
#[graphql_scalar(to_output_with = Scalar::to_output)]
6+
type CustomScalar = Scalar;
7+
8+
impl Scalar {
9+
fn to_output(&self) -> Value {
10+
Value::scalar(0)
11+
}
12+
}
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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` attributes
2+
--> fail/scalar/type_alias/impl_with_not_all_resolvers.rs:6:1
3+
|
4+
6 | type CustomScalar = Scalar;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use juniper::graphql_scalar;
2+
3+
struct Scalar;
4+
5+
#[graphql_scalar]
6+
type CustomScalar = Scalar;
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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` attributes
2+
--> fail/scalar/type_alias/impl_without_resolvers.rs:6:1
3+
|
4+
6 | type CustomScalar = Scalar;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

integration_tests/juniper_tests/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2018"
55
publish = false
66

77
[dependencies]
8+
chrono = "0.4"
89
derive_more = "0.99"
910
futures = "0.3"
1011
juniper = { path = "../../juniper" }

integration_tests/juniper_tests/src/codegen/derive_scalar.rs

-125
This file was deleted.

0 commit comments

Comments
 (0)