Skip to content

Commit

Permalink
Check strings for matching enum variants which can be passed in from …
Browse files Browse the repository at this point in the history
…variables
  • Loading branch information
dhendrie91 authored and jgautheron committed Nov 15, 2023
1 parent 6b8a9b3 commit b932b1e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
19 changes: 19 additions & 0 deletions crates/planner/tests/enum_variable.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
query($sortOrder: UserSortOrder!){
users(sortOrder: $sortOrder) {
id
username
}
}
---
{
"sortOrder": "CREATED_AT"
}
---
{
"type": "fetch",
"service": "accounts",
"variables": {
"sortOrder": "CREATED_AT"
},
"query": "query($sortOrder: UserSortOrder!)\n{ users(sortOrder: $sortOrder) { id username } }"
}
6 changes: 6 additions & 0 deletions crates/planner/tests/test.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ type Query {
topProducts: [Product!]! @resolve(service: "products")
node(id: ID!): Node @resolve(service: "accounts")
reviews(productId: ID!, limit: Int): [Review!]! @resolve(service: "reviews")
users(sortOrder: UserSortOrder!): [User!]! @resolve(service: "accounts")
}

enum UserSortOrder {
NAME
CREATED_AT
}

type Mutation {
Expand Down
15 changes: 13 additions & 2 deletions crates/validation/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use graphgate_schema::{ComposedSchema, TypeKind};
use parser::types::{BaseType, Type};
use value::ConstValue;
use value::{ConstValue, Name};

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Scope<'a> {
Expand Down Expand Up @@ -119,6 +119,18 @@ pub fn is_valid_input_value(
} else {
None
}
} else if let ConstValue::String(v) = value {
if ty.enum_values.contains_key(&Name::new(v)) {
None
} else {
Some(valid_error(
&path_node,
format!(
"enumeration type \"{}\" does not contain the value \"{}\"",
ty.name, value
)
))
}
} else {
Some(valid_error(
&path_node,
Expand Down Expand Up @@ -178,7 +190,6 @@ pub fn is_valid_input_value(
}
}
}

if !ty.nullable {
if matches!(value, ConstValue::Null) {
Some(valid_error(&path_node, format!("expected type \"{}\"", ty)))
Expand Down

0 comments on commit b932b1e

Please sign in to comment.