pub fn arg_from_json<T: DeserializeOwned>(
args: &mut Value,
arg_name: &'static str,
) -> Result<T, AppsyncError>
Expand description
Extracts and deserializes a named argument from a JSON Value into the specified type
§Arguments
args
- Mutable reference to a JSON Value containing argumentsarg_name
- Name of the argument to extract
§Returns
Ok(T)
- Successfully deserialized value of type TErr(AppsyncError)
- Error if argument is missing or invalid format
§Examples
let mut args = json!({
"userId": "123",
"count": 5
});
// Extract userId as String
let user_id: String = arg_from_json(&mut args, "userId")?;
assert_eq!(user_id, "123");
// Extract count as i32
let count: i32 = arg_from_json(&mut args, "count")?;
assert_eq!(count, 5);
// Error case: invalid type
let result: Result<String, _> = arg_from_json(&mut args, "count");
assert!(result.is_err());
// Error case: missing argument
let result: Result<String, _> = arg_from_json(&mut args, "missing");
assert!(result.is_err());