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

Codegen reimplementation for GraphQL unions #666

Merged
merged 30 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
0a6a943
Refactor ground for union macros reimplementation
tyranron May 15, 2020
d7ea1c9
Implement #[derive(GraphQLUnion)] for enums
tyranron May 18, 2020
8f59c4d
Polish #[derive(GraphQLUnion)] for enums
tyranron May 21, 2020
fe24eee
Check duplicate custom resolvers between enum variants and whole enum…
tyranron May 25, 2020
a59964e
Remove From impls generation in favor of derive_more usage
tyranron May 25, 2020
8e88d2c
Support structs
tyranron May 25, 2020
92c4e07
Bootstrap #[graphql_union] for traits
tyranron May 26, 2020
4a23538
Implement #[graphql_union] for traits
tyranron May 27, 2020
9d7e99f
Relax more usages of :Sized trait bound
tyranron May 29, 2020
df39c15
Test trait implementation and pack Send + Sync into trait object
tyranron May 29, 2020
4feb141
Support custom resolver for traits
tyranron May 29, 2020
38464a4
Strip out old code
tyranron Jun 1, 2020
cc07fe2
Polish common code
tyranron Jun 1, 2020
810cc87
Impl PascalCasing for default union names
tyranron Jun 1, 2020
81af517
Verify trait method signature to not be async
tyranron Jun 1, 2020
f8d3e33
Fix type-level union variants duplication check with static assertion…
tyranron Jun 1, 2020
2c25f38
Merge branch 'master' into async-unions
tyranron Jun 1, 2020
a088cdc
Update CHANGELOG
tyranron Jun 1, 2020
23015f6
Bootstrap new book docs for unions
tyranron Jun 1, 2020
25ec5d8
Apply typos corrections provided by @LegNeato
tyranron Jun 2, 2020
2287679
Fix book tests
tyranron Jun 2, 2020
838e826
Add ScalarValue considerations to book
tyranron Jun 2, 2020
ab74bd8
Document macro definitions
tyranron Jun 2, 2020
8d51763
Try infer context type from trait method signature
tyranron Jun 2, 2020
ced1fe7
Fix juniper crate tests
tyranron Jun 2, 2020
6815418
Rework existing codegen failure tests
tyranron Jun 2, 2020
8f40bde
Cover up codegen fail cases
tyranron Jun 3, 2020
ecfe7e1
Cover up positive cases of #[graphql_union] macro
tyranron Jun 3, 2020
13d96ae
Cover up positive cases of #[derive(GraphQLUnion)] macro
tyranron Jun 3, 2020
d9cb3f1
Apply corrections provided by @LegNeato
tyranron Jun 4, 2020
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
435 changes: 347 additions & 88 deletions docs/book/content/types/unions.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions docs/book/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ build = "build.rs"
juniper = { path = "../../../juniper" }
juniper_iron = { path = "../../../juniper_iron" }
juniper_subscriptions = { path = "../../../juniper_subscriptions" }

derive_more = "0.99.7"
futures = "0.3"
tokio = { version = "0.2", features = ["rt-core", "blocking", "stream", "rt-util"] }
iron = "0.5.0"
Expand Down
6 changes: 6 additions & 0 deletions integration_tests/codegen_fail/fail/union/attr_wrong_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use juniper::graphql_union;

#[graphql_union]
enum Character {}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: #[graphql_union] attribute is applicable to trait definitions only
--> $DIR/attr_wrong_item.rs:3:1
|
3 | #[graphql_union]
| ^^^^^^^^^^^^^^^^
|
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
12 changes: 0 additions & 12 deletions integration_tests/codegen_fail/fail/union/derive_enum_field.rs

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions integration_tests/codegen_fail/fail/union/derive_no_fields.rs

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions integration_tests/codegen_fail/fail/union/derive_same_type.stderr

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use juniper::GraphQLUnion;

#[derive(GraphQLUnion)]
union Character { id: i32 }

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: GraphQL union can only be derived for enums and structs
--> $DIR/derive_wrong_item.rs:4:1
|
4 | union Character { id: i32 }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use juniper::{GraphQLObject, GraphQLUnion};

#[derive(GraphQLUnion)]
#[graphql(on Human = resolve_fn1)]
enum Character {
#[graphql(with = resolve_fn2)]
A(Human),
}

#[derive(GraphQLObject)]
pub struct Human {
id: String,
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: GraphQL union variant `Human` already has external resolver function `resolve_fn1` declared on the enum
--> $DIR/enum_external_resolver_fn_conflicts_with_variant_external_resolver_fn.rs:6:15
|
6 | #[graphql(with = resolve_fn2)]
| ^^^^
|
= note: https://spec.graphql.org/June2018/#sec-Unions
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use juniper::{GraphQLObject, GraphQLUnion};

#[derive(GraphQLUnion)]
enum __Character {
A(Human),
}

#[derive(GraphQLObject)]
pub struct Human {
id: String,
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: All types and directives defined within a schema must not have a name which begins with `__` (two underscores), as this is used exclusively by GraphQL’s introspection system.
--> $DIR/enum_name_double_underscored.rs:4:6
|
4 | enum __Character {
| ^^^^^^^^^^^
|
= note: https://spec.graphql.org/June2018/#sec-Schema
6 changes: 6 additions & 0 deletions integration_tests/codegen_fail/fail/union/enum_no_fields.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use juniper::GraphQLUnion;

#[derive(GraphQLUnion)]
enum Character {}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: GraphQL union expects at least one union variant
--> $DIR/enum_no_fields.rs:4:1
|
4 | enum Character {}
| ^^^^^^^^^^^^^^^^^
|
= note: https://spec.graphql.org/June2018/#sec-Unions
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use juniper::{GraphQLEnum, GraphQLUnion};

#[derive(GraphQLEnum)]
pub enum Test {
A,
B,
}

#[derive(GraphQLUnion)]
enum Character {
Test(Test),
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0277]: the trait bound `Test: juniper::types::marker::GraphQLObjectType<juniper::value::scalar::DefaultScalarValue>` is not satisfied
--> $DIR/enum_non_object_variant.rs:9:10
|
9 | #[derive(GraphQLUnion)]
| ^^^^^^^^^^^^ the trait `juniper::types::marker::GraphQLObjectType<juniper::value::scalar::DefaultScalarValue>` is not implemented for `Test`
|
= note: required by `juniper::types::marker::GraphQLObjectType::mark`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `Test: juniper::types::marker::GraphQLObjectType<__S>` is not satisfied
--> $DIR/enum_non_object_variant.rs:9:10
|
9 | #[derive(GraphQLUnion)]
| ^^^^^^^^^^^^ the trait `juniper::types::marker::GraphQLObjectType<__S>` is not implemented for `Test`
|
= note: required by `juniper::types::marker::GraphQLObjectType::mark`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use juniper::GraphQLUnion;

#[derive(GraphQLUnion)]
enum Character {
A(u8),
B(u8),
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: GraphQL union must have a different type for each union variant
--> $DIR/enum_same_type_pretty.rs:4:1
|
4 | / enum Character {
5 | | A(u8),
6 | | B(u8),
7 | | }
| |_^
|
= note: https://spec.graphql.org/June2018/#sec-Unions
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[derive(juniper::GraphQLUnion)]
use juniper::GraphQLUnion;

#[derive(GraphQLUnion)]
enum Character {
A(std::string::String),
B(String),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error[E0119]: conflicting implementations of trait `<Character as juniper::types::marker::GraphQLUnion>::mark::_::{{closure}}#0::MutuallyExclusive` for type `std::string::String`:
--> $DIR/enum_same_type_ugly.rs:3:10
|
3 | #[derive(GraphQLUnion)]
| ^^^^^^^^^^^^
| |
| first implementation here
| conflicting implementation for `std::string::String`
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use juniper::{GraphQLObject, GraphQLUnion};

#[derive(GraphQLUnion)]
enum Character1 {
A { human: Human },
}

#[derive(GraphQLUnion)]
enum Character2 {
A(Human, u8),
}

#[derive(GraphQLObject)]
pub struct Human {
id: String,
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error: GraphQL union enum allows only unnamed variants with a single field, e.g. `Some(T)`
--> $DIR/enum_wrong_variant_field.rs:5:5
|
5 | A { human: Human },
| ^
|
= note: https://spec.graphql.org/June2018/#sec-Unions

error: GraphQL union enum allows only unnamed variants with a single field, e.g. `Some(T)`
--> $DIR/enum_wrong_variant_field.rs:10:6
|
10 | A(Human, u8),
| ^^^^^^^^^^^
|
= note: https://spec.graphql.org/June2018/#sec-Unions
23 changes: 0 additions & 23 deletions integration_tests/codegen_fail/fail/union/impl_enum_field.rs

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions integration_tests/codegen_fail/fail/union/impl_no_fields.rs

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use juniper::{GraphQLObject, GraphQLUnion};

#[derive(GraphQLUnion)]
#[graphql(on Human = __Character::a)]
struct __Character;

impl __Character {
fn a(&self, _: &()) -> Option<&Human> {
None
}
}

#[derive(GraphQLObject)]
pub struct Human {
id: String,
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: All types and directives defined within a schema must not have a name which begins with `__` (two underscores), as this is used exclusively by GraphQL’s introspection system.
--> $DIR/struct_name_double_underscored.rs:5:8
|
5 | struct __Character;
| ^^^^^^^^^^^
|
= note: https://spec.graphql.org/June2018/#sec-Schema
6 changes: 6 additions & 0 deletions integration_tests/codegen_fail/fail/union/struct_no_fields.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use juniper::GraphQLUnion;

#[derive(GraphQLUnion)]
struct Character;

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: GraphQL union expects at least one union variant
--> $DIR/struct_no_fields.rs:4:1
|
4 | struct Character;
| ^^^^^^^^^^^^^^^^^
|
= note: https://spec.graphql.org/June2018/#sec-Unions
Loading