Allow strings to be parsed into enums in variables

Fixes #17.
This commit is contained in:
Magnus Hallin 2017-02-04 11:08:00 +01:00
parent d8b07e8c68
commit c5a1f975ed
3 changed files with 10 additions and 3 deletions

View file

@ -96,7 +96,7 @@ fn does_not_accept_string_literals() {
#[test]
fn accepts_strings_in_variables() {
run_variable_query(
"{ toString(color: RED) }",
"query q($color: Color!) { toString(color: $color) }",
vec![
("color".to_owned(), InputValue::string("RED")),
].into_iter().collect(),

View file

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

View file

@ -1,7 +1,7 @@
use std::collections::HashSet;
use ast::InputValue;
use schema::model::{SchemaType, TypeType};
use schema::meta::{MetaType, InputObjectMeta};
use schema::meta::{MetaType, InputObjectMeta, EnumMeta};
pub fn is_valid_literal_value(schema: &SchemaType, arg_type: &TypeType, arg_value: &InputValue) -> bool {
match *arg_type {
@ -20,6 +20,13 @@ pub fn is_valid_literal_value(schema: &SchemaType, arg_type: &TypeType, arg_valu
}
}
TypeType::Concrete(t) => {
// Even though InputValue::String can be parsed into an enum, they
// are not valid as enum *literals* in a GraphQL query.
match (arg_value, arg_type.to_concrete()) {
(&InputValue::String(_), Some(&MetaType::Enum(EnumMeta { .. }))) => return false,
_ => ()
}
match *arg_value {
ref v @ InputValue::Null |
ref v @ InputValue::Int(_) |