Skip to content

Commit

Permalink
Redesign #[graphql_scalar] macro (#1014, #1000)
Browse files Browse the repository at this point in the history
- support generic scalars
- make it applicable to type aliases and struct/enums/unions
  • Loading branch information
ilslv authored Feb 24, 2022
1 parent 3a70403 commit 63198cd
Show file tree
Hide file tree
Showing 47 changed files with 4,297 additions and 2,041 deletions.
350 changes: 309 additions & 41 deletions docs/book/content/types/scalars.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
error[E0119]: conflicting implementations of trait `<CharacterValueEnum<ObjA, ObjA> as juniper::GraphQLInterface<__S>>::mark::_::{closure#0}::MutuallyExclusive` for type `ObjA`
error[E0119]: conflicting implementations of trait `std::convert::From<ObjA>` for type `CharacterValueEnum<ObjA, ObjA>`
--> fail/interface/implementers_duplicate_ugly.rs:11:1
|
11 | #[graphql_interface(for = [ObjA, ObjAlias])]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| first implementation here
| conflicting implementation for `ObjA`
| conflicting implementation for `CharacterValueEnum<ObjA, ObjA>`
|
= note: this error originates in the macro `::juniper::sa::assert_type_ne_all` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the attribute macro `graphql_interface` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0119]: conflicting implementations of trait `std::convert::From<ObjA>` for type `CharacterValueEnum<ObjA, ObjA>`
error[E0119]: conflicting implementations of trait `<CharacterValueEnum<ObjA, ObjA> as juniper::GraphQLInterface<__S>>::mark::_::{closure#0}::MutuallyExclusive` for type `ObjA`
--> fail/interface/implementers_duplicate_ugly.rs:11:1
|
11 | #[graphql_interface(for = [ObjA, ObjAlias])]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| first implementation here
| conflicting implementation for `CharacterValueEnum<ObjA, ObjA>`
| conflicting implementation for `ObjA`
|
= note: this error originates in the attribute macro `graphql_interface` (in Nightly builds, run with -Z macro-backtrace for more info)
= 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 number Diff line number Diff line change
@@ -0,0 +1,16 @@
use juniper::{graphql_scalar, InputValue, ScalarValue, Value};

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

impl ScalarSpecifiedByUrl {
fn to_output<S: ScalarValue>(&self) -> Value<S> {
Value::scalar(0)
}

fn from_input<S: ScalarValue>(_: &InputValue<S>) -> Result<Self, String> {
Ok(Self)
}
}

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

error[E0412]: cannot find type `ScalarSpecifiedByUrl` in this scope
--> fail/scalar/derive_input/impl_invalid_url.rs:6:6
|
6 | impl ScalarSpecifiedByUrl {
| ^^^^^^^^^^^^^^^^^^^^ not found in this scope

error: the `Self` constructor can only be used with tuple or unit structs
--> fail/scalar/derive_input/impl_invalid_url.rs:12:12
|
12 | Ok(Self)
| ^^^^

This file was deleted.

This file was deleted.

22 changes: 0 additions & 22 deletions integration_tests/codegen_fail/fail/scalar/impl_invalid_url.rs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use juniper::{graphql_scalar, InputValue, ScalarValue, Value};

struct ScalarSpecifiedByUrl;

#[graphql_scalar(
specified_by_url = "not an url",
with = scalar,
parse_token(i32),
)]
type Scalar = ScalarSpecifiedByUrl;

mod scalar {
use super::*;

pub(super) fn to_output<S: ScalarValue>(_: &ScalarSpecifiedByUrl) -> Value<S> {
Value::scalar(0)
}

pub(super) fn from_input<S: ScalarValue>(
_: &InputValue<S>,
) -> Result<ScalarSpecifiedByUrl, String> {
Ok(ScalarSpecifiedByUrl)
}
}

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/type_alias/impl_invalid_url.rs:6:24
|
6 | specified_by_url = "not an url",
| ^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use juniper::{graphql_scalar, Value};

struct Scalar;

#[graphql_scalar(to_output_with = Scalar::to_output)]
type CustomScalar = Scalar;

impl Scalar {
fn to_output(&self) -> Value {
Value::scalar(0)
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +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
|
6 | type CustomScalar = Scalar;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use juniper::graphql_scalar;

struct Scalar;

#[graphql_scalar]
type CustomScalar = Scalar;

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +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
|
6 | type CustomScalar = Scalar;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1 change: 1 addition & 0 deletions integration_tests/juniper_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2018"
publish = false

[dependencies]
chrono = "0.4"
derive_more = "0.99"
futures = "0.3"
juniper = { path = "../../juniper" }
Expand Down
125 changes: 0 additions & 125 deletions integration_tests/juniper_tests/src/codegen/derive_scalar.rs

This file was deleted.

Loading

0 comments on commit 63198cd

Please sign in to comment.