-
Notifications
You must be signed in to change notification settings - Fork 46
Fix: Accept all valid Float value inputs on coerce_variable_value
#1002
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
630f60b
b9098bb
a06f2d6
892891a
2174352
c5f4bac
a62c087
a4c357e
23d67b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
#[test] | ||
fn test_graphql_float_variable_coercion() { | ||
// Small schema with a Float in the input object | ||
let sdl = r#" | ||
type Car { id: ID! kilometers: Float! } | ||
input CarInput { kilometers: Float! } | ||
type Query { getCarById(id: ID!): Car } | ||
type Mutation { insertACar(car: CarInput!): Car! | ||
} | ||
"#; | ||
|
||
let parsed_schema = Schema::parse_and_validate(sdl, "sdl").unwrap(); | ||
|
||
let executable_mutation = ExecutableDocument::parse_and_validate( | ||
&parsed_schema, | ||
"mutation MyCarInsertMutation($car: CarInput!){ insertACar(car:$car) { id kilometers } }", | ||
"MyCarInsertMutation", | ||
) | ||
.unwrap(); | ||
|
||
let operation = executable_mutation | ||
.operations | ||
.get(Some("MyCarInsertMutation")) | ||
.unwrap(); | ||
|
||
let kilometers_value = 3000; | ||
|
||
// Provide an integer for a Float field | ||
let input_variables = serde_json_bytes::json!({ "car": { "kilometers": kilometers_value } }); | ||
let map = match input_variables { | ||
serde_json_bytes::Value::Object(m) => m, | ||
_ => unreachable!(), | ||
}; | ||
|
||
|
||
// Coerce and validate. | ||
let coerced = coerce_variable_values(&parsed_schema, operation, &map).unwrap(); | ||
let vars_for_exec = coerced.into_inner(); | ||
|
||
// ---- Assertions ---- | ||
let car = vars_for_exec | ||
.get("car") | ||
.and_then(|value| value.as_object()) | ||
.expect("coerced `car` object"); | ||
assert_eq!( | ||
car.get("kilometers").unwrap(), | ||
kilometers_value, | ||
"kilometers should be present and a valid amount." | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test won't run as is. It needs to be added to
apollo-compiler/tests/validation/main.rs
. Once added, you will notice rust compiler won't compile this file, and you will need to declareSchema
,ExecutableDocument
andcoerce_variable_values
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve moved
input_coercion.rs
toapollo-compiler/tests/validation
directory and added its declaration toapollo-compiler/tests/validation/mod.rs
with c5f4bac. I believe this is what you were referring to?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sorry, my bad, I typed out the path wrong. Where you had it was good - input coercion is not a validation rule. It needed to be added to
apolo-compiler/tests/main.rs
asmod input_coercion
(and I wroteapolo-compiler/tests/validation/main.rs
, leading to confusion).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for clarification @lrlna 🙌. You are right on this. I moved it to
apollo-compiler/tests/
and correctly adjustedapollo-compiler/tests/main.rs
in a4c357e like you wrote.