@@ -16,7 +16,7 @@ use async_graphql_parser::{
1616 VariableDefinition ,
1717 } ,
1818} ;
19- use async_graphql_value:: { ConstValue , Name } ;
19+ use async_graphql_value:: { ConstValue , Name , indexmap :: IndexMap } ;
2020use serde:: de:: Error ;
2121use serde_json:: { Map , Value } ;
2222
@@ -226,33 +226,77 @@ impl<'a> OperationValidator<'a> {
226226 } ) ,
227227 } ,
228228 BaseType :: Named ( type_name) => {
229- if let Some ( type_definition) = self . schema . get_type_definition ( type_name. as_str ( ) )
230- && let TypeKind :: Enum ( enum_type) = & type_definition. kind
231- {
232- return match value {
233- Value :: String ( enum_value) => {
234- let is_valid = enum_type
235- . values
236- . iter ( )
237- . any ( |value_def| value_def. node . value . node . as_str ( ) == enum_value) ;
229+ if let Some ( type_definition) = self . schema . get_type_definition ( type_name. as_str ( ) ) {
230+ match & type_definition. kind {
231+ TypeKind :: Enum ( enum_type) => {
232+ return match value {
233+ Value :: String ( enum_value) => {
234+ let is_valid = enum_type. values . iter ( ) . any ( |value_def| {
235+ value_def. node . value . node . as_str ( ) == enum_value
236+ } ) ;
238237
239- if is_valid {
240- Ok ( ConstValue :: Enum ( Name :: new ( enum_value) ) )
241- } else {
242- Err ( error ( format ! (
243- "Invalid enum value '{}' for type '{}'" ,
244- enum_value,
245- type_name. as_str( )
246- ) ) )
247- }
238+ if is_valid {
239+ Ok ( ConstValue :: Enum ( Name :: new ( enum_value) ) )
240+ } else {
241+ Err ( error ( format ! (
242+ "Invalid enum value '{}' for type '{}'" ,
243+ enum_value,
244+ type_name. as_str( )
245+ ) ) )
246+ }
247+ }
248+ Value :: Null => Ok ( ConstValue :: Null ) ,
249+ _ => Err ( error ( format ! (
250+ "Expected enum value for type '{}', got {}" ,
251+ type_name. as_str( ) ,
252+ value
253+ ) ) ) ,
254+ } ;
248255 }
249- Value :: Null => Ok ( ConstValue :: Null ) ,
250- _ => Err ( error ( format ! (
251- "Expected enum value for type '{}', got {}" ,
252- type_name. as_str( ) ,
253- value
254- ) ) ) ,
255- } ;
256+ TypeKind :: InputObject ( input_object_type) => {
257+ return match value {
258+ Value :: Object ( values) => {
259+ let coerced_values = values
260+ . into_iter ( )
261+ . map ( |( field_name, field_value) | {
262+ let field_definition =
263+ input_object_type. fields . iter ( ) . find ( |field| {
264+ field. node . name . node . as_str ( ) == field_name
265+ } ) ;
266+
267+ let coerced_value = match field_definition {
268+ Some ( field_definition) => self
269+ . coerce_variable_value (
270+ & field_definition. node . ty . node ,
271+ field_value,
272+ variable_name,
273+ ) ?,
274+ None => ConstValue :: from_json ( field_value)
275+ . map_err ( |e| {
276+ ValidationError :: MalformedVariable (
277+ variable_name. node . as_str ( ) . to_string ( ) ,
278+ variable_name. pos ,
279+ e,
280+ )
281+ } ) ?,
282+ } ;
283+
284+ Ok ( ( Name :: new ( field_name) , coerced_value) )
285+ } )
286+ . collect :: < Result < IndexMap < _ , _ > , ValidationError > > ( ) ?;
287+
288+ Ok ( ConstValue :: Object ( coerced_values) )
289+ }
290+ Value :: Null => Ok ( ConstValue :: Null ) ,
291+ _ => Err ( error ( format ! (
292+ "Expected input object for type '{}', got {}" ,
293+ type_name. as_str( ) ,
294+ value
295+ ) ) ) ,
296+ } ;
297+ }
298+ _ => { }
299+ }
256300 }
257301
258302 ConstValue :: from_json ( value) . map_err ( |e| {
0 commit comments