Skip to content

Commit c5a1f97

Browse files
committed
Allow strings to be parsed into enums in variables
Fixes #17.
1 parent d8b07e8 commit c5a1f97

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

src/executor_tests/enums.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn does_not_accept_string_literals() {
9696
#[test]
9797
fn accepts_strings_in_variables() {
9898
run_variable_query(
99-
"{ toString(color: RED) }",
99+
"query q($color: Color!) { toString(color: $color) }",
100100
vec![
101101
("color".to_owned(), InputValue::string("RED")),
102102
].into_iter().collect(),

src/macros/enums.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ macro_rules! graphql_enum {
9494

9595
impl $crate::FromInputValue for $name {
9696
fn from(v: &$crate::InputValue) -> Option<$name> {
97-
match v.as_enum_value() {
97+
match v.as_enum_value().or_else(|| v.as_string_value()) {
9898
$(
9999
Some(graphql_enum!(@as_pattern, $ename))
100100
=> Some(graphql_enum!(@as_expr, $eval)), )*

src/types/utilities.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashSet;
22
use ast::InputValue;
33
use schema::model::{SchemaType, TypeType};
4-
use schema::meta::{MetaType, InputObjectMeta};
4+
use schema::meta::{MetaType, InputObjectMeta, EnumMeta};
55

66
pub fn is_valid_literal_value(schema: &SchemaType, arg_type: &TypeType, arg_value: &InputValue) -> bool {
77
match *arg_type {
@@ -20,6 +20,13 @@ pub fn is_valid_literal_value(schema: &SchemaType, arg_type: &TypeType, arg_valu
2020
}
2121
}
2222
TypeType::Concrete(t) => {
23+
// Even though InputValue::String can be parsed into an enum, they
24+
// are not valid as enum *literals* in a GraphQL query.
25+
match (arg_value, arg_type.to_concrete()) {
26+
(&InputValue::String(_), Some(&MetaType::Enum(EnumMeta { .. }))) => return false,
27+
_ => ()
28+
}
29+
2330
match *arg_value {
2431
ref v @ InputValue::Null |
2532
ref v @ InputValue::Int(_) |

0 commit comments

Comments
 (0)