-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Provide impls for arrays * Remove redundant Default bound * Recheck other places of mem::transmute usage * Fix missing marker impls * Extend GraphQL list validation with optional expected size * Improve input object codegen * Cover arrays with tests * Add CHANGELOG entry * Consider panic safety in FromInputValue implementation for array * Tune up codegen failure tests
- Loading branch information
Showing
25 changed files
with
629 additions
and
70 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
integration_tests/codegen_fail/fail/interface/argument_wrong_default_array.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use juniper::{graphql_interface, GraphQLObject}; | ||
|
||
#[derive(GraphQLObject)] | ||
#[graphql(impl = CharacterValue)] | ||
pub struct ObjA { | ||
test: String, | ||
} | ||
|
||
#[graphql_interface] | ||
impl Character for ObjA {} | ||
|
||
#[graphql_interface(for = ObjA)] | ||
trait Character { | ||
fn wrong( | ||
&self, | ||
#[graphql(default = [true, false, false])] | ||
input: [bool; 2], | ||
) -> bool { | ||
input[0] | ||
} | ||
} | ||
|
||
fn main() {} |
12 changes: 12 additions & 0 deletions
12
integration_tests/codegen_fail/fail/interface/argument_wrong_default_array.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0277]: the trait bound `[bool; 2]: From<[bool; 3]>` is not satisfied | ||
--> $DIR/argument_wrong_default_array.rs:12:1 | ||
| | ||
12 | #[graphql_interface(for = ObjA)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<[bool; 3]>` is not implemented for `[bool; 2]` | ||
| | ||
= help: the following implementations were found: | ||
<&'a [ascii::ascii_char::AsciiChar] as From<&'a ascii::ascii_str::AsciiStr>> | ||
<&'a [u8] as From<&'a ascii::ascii_str::AsciiStr>> | ||
<&'a mut [ascii::ascii_char::AsciiChar] as From<&'a mut ascii::ascii_str::AsciiStr>> | ||
= note: required because of the requirements on the impl of `Into<[bool; 2]>` for `[bool; 3]` | ||
= note: this error originates in the attribute macro `graphql_interface` (in Nightly builds, run with -Z macro-backtrace for more info) |
11 changes: 11 additions & 0 deletions
11
integration_tests/codegen_fail/fail/object/impl_argument_wrong_default_array.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
struct Object; | ||
|
||
#[juniper::graphql_object] | ||
impl Object { | ||
#[graphql(arguments(input(default = [true, false, false])))] | ||
fn wrong(input: [bool; 2]) -> bool { | ||
input[0] | ||
} | ||
} | ||
|
||
fn main() {} |
7 changes: 7 additions & 0 deletions
7
integration_tests/codegen_fail/fail/object/impl_argument_wrong_default_array.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/impl_argument_wrong_default_array.rs:3:1 | ||
| | ||
3 | #[juniper::graphql_object] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an array with a fixed size of 2 elements, found one with 3 elements | ||
| | ||
= note: this error originates in the attribute macro `juniper::graphql_object` (in Nightly builds, run with -Z macro-backtrace for more info) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,257 @@ | ||
use juniper::{ | ||
graphql_object, graphql_value, EmptyMutation, EmptySubscription, GraphQLInputObject, RootNode, | ||
Variables, | ||
}; | ||
|
||
mod as_output_field { | ||
use super::*; | ||
|
||
struct Query; | ||
|
||
#[graphql_object] | ||
impl Query { | ||
fn roll() -> [bool; 3] { | ||
[true, false, true] | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn works() { | ||
let query = r#" | ||
query Query { | ||
roll | ||
} | ||
"#; | ||
|
||
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); | ||
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &()) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(errors.len(), 0); | ||
assert_eq!(res, graphql_value!({"roll": [true, false, true]})); | ||
} | ||
} | ||
|
||
mod as_input_field { | ||
use super::*; | ||
|
||
#[derive(GraphQLInputObject)] | ||
struct Input { | ||
two: [bool; 2], | ||
} | ||
|
||
#[derive(GraphQLInputObject)] | ||
struct InputSingle { | ||
one: [bool; 1], | ||
} | ||
|
||
struct Query; | ||
|
||
#[graphql_object] | ||
impl Query { | ||
fn first(input: InputSingle) -> bool { | ||
input.one[0] | ||
} | ||
|
||
fn second(input: Input) -> bool { | ||
input.two[1] | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn works() { | ||
let query = r#" | ||
query Query { | ||
second(input: { two: [true, false] }) | ||
} | ||
"#; | ||
|
||
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); | ||
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &()) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(errors.len(), 0); | ||
assert_eq!(res, graphql_value!({"second": false})); | ||
} | ||
|
||
#[tokio::test] | ||
async fn fails_on_incorrect_count() { | ||
let query = r#" | ||
query Query { | ||
second(input: { two: [true, true, false] }) | ||
} | ||
"#; | ||
|
||
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); | ||
let res = juniper::execute(query, None, &schema, &Variables::new(), &()).await; | ||
|
||
assert!(res.is_err()); | ||
assert!(res | ||
.unwrap_err() | ||
.to_string() | ||
.contains(r#"Invalid value for argument "input", expected type "Input!""#)); | ||
} | ||
|
||
#[tokio::test] | ||
async fn cannot_coerce_from_raw_value_if_multiple() { | ||
let query = r#" | ||
query Query { | ||
second(input: { two: true }) | ||
} | ||
"#; | ||
|
||
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); | ||
let res = juniper::execute(query, None, &schema, &Variables::new(), &()).await; | ||
|
||
assert!(res.is_err()); | ||
assert!(res | ||
.unwrap_err() | ||
.to_string() | ||
.contains(r#"Invalid value for argument "input", expected type "Input!""#)); | ||
} | ||
|
||
#[tokio::test] | ||
async fn can_coerce_from_raw_value_if_single() { | ||
let query = r#" | ||
query Query { | ||
first(input: { one: true }) | ||
} | ||
"#; | ||
|
||
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); | ||
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &()) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(errors.len(), 0); | ||
assert_eq!(res, graphql_value!({"first": true})); | ||
} | ||
} | ||
|
||
mod as_input_argument { | ||
use super::*; | ||
|
||
struct Query; | ||
|
||
#[graphql_object] | ||
impl Query { | ||
fn second(input: [bool; 2]) -> bool { | ||
input[1] | ||
} | ||
|
||
fn first(input: [bool; 1]) -> bool { | ||
input[0] | ||
} | ||
|
||
#[graphql(arguments(input(default = [true, false, false])))] | ||
fn third(input: [bool; 3]) -> bool { | ||
input[2] | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn works() { | ||
let query = r#" | ||
query Query { | ||
second(input: [false, true]) | ||
} | ||
"#; | ||
|
||
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); | ||
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &()) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(errors.len(), 0); | ||
assert_eq!(res, graphql_value!({"second": true})); | ||
} | ||
|
||
#[tokio::test] | ||
async fn fails_on_incorrect_count() { | ||
let query = r#" | ||
query Query { | ||
second(input: [true, true, false]) | ||
} | ||
"#; | ||
|
||
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); | ||
let res = juniper::execute(query, None, &schema, &Variables::new(), &()).await; | ||
|
||
assert!(res.is_err()); | ||
assert!(res | ||
.unwrap_err() | ||
.to_string() | ||
.contains(r#"Invalid value for argument "input", expected type "[Boolean!]!""#)); | ||
} | ||
|
||
#[tokio::test] | ||
async fn cannot_coerce_from_raw_value_if_multiple() { | ||
let query = r#" | ||
query Query { | ||
second(input: true) | ||
} | ||
"#; | ||
|
||
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); | ||
let res = juniper::execute(query, None, &schema, &Variables::new(), &()).await; | ||
|
||
assert!(res.is_err()); | ||
assert!(res | ||
.unwrap_err() | ||
.to_string() | ||
.contains(r#"Invalid value for argument "input", expected type "[Boolean!]!""#)); | ||
} | ||
|
||
#[tokio::test] | ||
async fn can_coerce_from_raw_value_if_single() { | ||
let query = r#" | ||
query Query { | ||
first(input: true) | ||
} | ||
"#; | ||
|
||
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); | ||
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &()) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(errors.len(), 0); | ||
assert_eq!(res, graphql_value!({"first": true})); | ||
} | ||
|
||
#[tokio::test] | ||
async fn picks_default() { | ||
let query = r#" | ||
query Query { | ||
third | ||
} | ||
"#; | ||
|
||
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); | ||
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &()) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(errors.len(), 0); | ||
assert_eq!(res, graphql_value!({"third": false})); | ||
} | ||
|
||
#[tokio::test] | ||
async fn picks_specified_over_default() { | ||
let query = r#" | ||
query Query { | ||
third(input: [false, false, true]) | ||
} | ||
"#; | ||
|
||
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); | ||
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &()) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(errors.len(), 0); | ||
assert_eq!(res, graphql_value!({"third": true})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
#[cfg(test)] | ||
mod arc_fields; | ||
#[cfg(test)] | ||
mod array; | ||
#[cfg(test)] | ||
mod codegen; | ||
#[cfg(test)] | ||
mod custom_scalar; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.