Skip to content

Commit fe99e1c

Browse files
authored
Allow specifying scalar and context for GraphQLEnum (#621)
* Updated implementation of derive enum - allows context specification - allows scalar specification - shares code with derive object * Added this feature to CHANGELOG * Added matrix with supported macro attributes for enums * Added case which checks for custom context * GraphQLUnion now can use a different context per variant * Moved context switch test for union into right folder * Sync resolve expression has the same form as the other impls * Disabled custom scalar on GraphQLEnum * Fixed CHANGELOG * Fixed support matrix of GraphQLEnum in the book - scalar not supported! - skip not supported! * Added test case for "noasync" derive attribute * Disallowed generics and lifetimes on GraphQLEnum * Added error message for duplicate naming * Added error message for empty variant
1 parent 7e87247 commit fe99e1c

File tree

7 files changed

+349
-266
lines changed

7 files changed

+349
-266
lines changed

docs/book/content/types/enums.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,17 @@ enum StarWarsEpisode {
5454

5555
# fn main() {}
5656
```
57+
58+
## Supported Macro Attributes (Derive)
59+
60+
| Name of Attribute | Container Support | Field Support |
61+
|-------------------|:-----------------:|:----------------:|
62+
| context || ? |
63+
| deprecated |||
64+
| description |||
65+
| interfaces | ? ||
66+
| name |||
67+
| noasync || ? |
68+
| scalar || ? |
69+
| skip | ? ||
70+
| ✔: supported | ✘: not supported | ?: not available |

integration_tests/juniper_tests/src/codegen/derive_enum.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ use fnv::FnvHashMap;
44
#[cfg(test)]
55
use juniper::{self, DefaultScalarValue, FromInputValue, GraphQLType, InputValue, ToInputValue};
66

7+
pub struct CustomContext {}
8+
9+
impl juniper::Context for CustomContext {}
10+
711
#[derive(juniper::GraphQLEnum, Debug, PartialEq)]
812
#[graphql(name = "Some", description = "enum descr")]
913
enum SomeEnum {
@@ -39,6 +43,12 @@ enum OverrideDocEnum {
3943
Foo,
4044
}
4145

46+
#[derive(juniper::GraphQLEnum)]
47+
#[graphql(context = CustomContext, noasync)]
48+
enum ContextEnum {
49+
A,
50+
}
51+
4252
#[test]
4353
fn test_derived_enum() {
4454
// Ensure that rename works.
@@ -98,3 +108,16 @@ fn test_doc_comment_override() {
98108
let meta = OverrideDocEnum::meta(&(), &mut registry);
99109
assert_eq!(meta.description(), Some(&"enum override".to_string()));
100110
}
111+
112+
fn test_context<T>(_t: T)
113+
where
114+
T: GraphQLType<DefaultScalarValue, Context = CustomContext>,
115+
{
116+
// empty
117+
}
118+
119+
#[test]
120+
fn test_doc_custom_context() {
121+
test_context(ContextEnum::A);
122+
// test_context(OverrideDocEnum::Foo); does not work
123+
}

integration_tests/juniper_tests/src/codegen/derive_union.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ impl DroidCompat {
9191
}
9292
}
9393

94+
#[derive(juniper::GraphQLUnion)]
95+
#[graphql(Context = CustomContext)]
96+
pub enum DifferentContext {
97+
A(DroidContext),
98+
B(Droid),
99+
}
100+
94101
// NOTICE: this can not compile due to generic implementation of GraphQLType<__S>
95102
// #[derive(juniper::GraphQLUnion)]
96103
// pub enum CharacterCompatFail {

juniper/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ See [#569](https://github.com/graphql-rust/juniper/pull/569).
2727

2828
See [#618](https://github.com/graphql-rust/juniper/pull/618).
2929

30+
- Derive macro `GraphQLEnum` supports custom context (see [#621](https://github.com/graphql-rust/juniper/pull/621))
31+
3032
## Breaking Changes
3133

3234
- `juniper::graphiql` has moved to `juniper::http::graphiql`

0 commit comments

Comments
 (0)