From 0af62933bff8f09d87c977cd40cae653bd4b990c Mon Sep 17 00:00:00 2001 From: laststylebender <43403528+laststylebender14@users.noreply.github.com> Date: Tue, 3 Sep 2024 16:35:13 +0530 Subject: [PATCH] fix: treat null responses as JSON scalars (#2784) --- .../tests/snapshots/json_to_config_spec__null.json.snap | 2 +- src/core/helpers/gql_type.rs | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/core/generator/tests/snapshots/json_to_config_spec__null.json.snap b/src/core/generator/tests/snapshots/json_to_config_spec__null.json.snap index eb01d1a972..1a01bf6c9c 100644 --- a/src/core/generator/tests/snapshots/json_to_config_spec__null.json.snap +++ b/src/core/generator/tests/snapshots/json_to_config_spec__null.json.snap @@ -7,5 +7,5 @@ schema @server @upstream { } type Query { - usersAge(age: Int): Empty @http(baseURL: "https://example.com", path: "/users", query: [{key: "age", value: "{{.args.age}}"}]) + usersAge(age: Int): JSON @http(baseURL: "https://example.com", path: "/users", query: [{key: "age", value: "{{.args.age}}"}]) } diff --git a/src/core/helpers/gql_type.rs b/src/core/helpers/gql_type.rs index 46bc24b32f..8d4da11518 100644 --- a/src/core/helpers/gql_type.rs +++ b/src/core/helpers/gql_type.rs @@ -25,7 +25,11 @@ pub fn is_valid_field_name(property_name: &str) -> bool { pub fn to_gql_type(value: &Value) -> String { match value { - Value::Null => "Empty", + Value::Null => { + // treat null values as JSON scalars as we don't know the exact shape of the + // output. + "JSON" + } Value::Bool(_) => "Boolean", Value::Number(_) => "Int", Value::String(_) => "String", @@ -81,7 +85,7 @@ mod test { assert_eq!(to_gql_type(&json!(false)), "Boolean"); assert_eq!(to_gql_type(&json!([1, 2, 3])), "List"); assert_eq!(to_gql_type(&json!({"name":"test", "age": 12})), "Object"); - assert_eq!(to_gql_type(&Value::Null), "Empty"); + assert_eq!(to_gql_type(&Value::Null), "JSON"); assert_eq!(to_gql_type(&json!([])), "List"); assert_eq!(to_gql_type(&json!({})), "Object");