From d5cb75d83067dac6749ef853f4c90cb8e9f6a354 Mon Sep 17 00:00:00 2001 From: Amit Singh Date: Fri, 19 Apr 2024 20:04:04 +0530 Subject: [PATCH] fix: null handling in the resolvers (#1760) --- src/blueprint/into_schema.rs | 8 ++--- src/http/response.rs | 9 +++++- tests/execution/test-null-in-array.md | 30 +++++++++++++++++++ tests/execution/test-null-in-object.md | 30 +++++++++++++++++++ ...__graphql-datasource-errors.md_test_0.snap | 4 +-- ...on_spec__test-null-in-array.md_client.snap | 28 +++++++++++++++++ ...on_spec__test-null-in-array.md_merged.snap | 16 ++++++++++ ...on_spec__test-null-in-array.md_test_0.snap | 15 ++++++++++ ...n_spec__test-null-in-object.md_client.snap | 28 +++++++++++++++++ ...n_spec__test-null-in-object.md_merged.snap | 16 ++++++++++ ...n_spec__test-null-in-object.md_test_0.snap | 15 ++++++++++ 11 files changed, 191 insertions(+), 8 deletions(-) create mode 100644 tests/execution/test-null-in-array.md create mode 100644 tests/execution/test-null-in-object.md create mode 100644 tests/snapshots/execution_spec__test-null-in-array.md_client.snap create mode 100644 tests/snapshots/execution_spec__test-null-in-array.md_merged.snap create mode 100644 tests/snapshots/execution_spec__test-null-in-array.md_test_0.snap create mode 100644 tests/snapshots/execution_spec__test-null-in-object.md_client.snap create mode 100644 tests/snapshots/execution_spec__test-null-in-object.md_merged.snap create mode 100644 tests/snapshots/execution_spec__test-null-in-object.md_test_0.snap diff --git a/src/blueprint/into_schema.rs b/src/blueprint/into_schema.rs index b35d310129..9b886bb848 100644 --- a/src/blueprint/into_schema.rs +++ b/src/blueprint/into_schema.rs @@ -70,12 +70,12 @@ fn to_type(def: &Definition) -> dynamic::Type { let const_value = expr.eval(ctx, &Concurrent::Sequential).await?; - let p = match const_value { - ConstValue::List(a) => FieldValue::list(a), - a => FieldValue::from(a), + ConstValue::List(a) => Some(FieldValue::list(a)), + ConstValue::Null => FieldValue::NONE, + a => Some(FieldValue::from(a)), }; - Ok(Some(p)) + Ok(p) } .instrument(span) .inspect_err(|err| tracing::error!(?err)), diff --git a/src/http/response.rs b/src/http/response.rs index 97e75e20b4..e4dcf41ad8 100644 --- a/src/http/response.rs +++ b/src/http/response.rs @@ -34,7 +34,14 @@ impl Response { } } - pub fn to_json(self) -> Result> { + pub fn to_json(self) -> Result> { + if self.body.is_empty() { + return Ok(Response { + status: self.status, + headers: self.headers, + body: Default::default(), + }); + } let body = serde_json::from_slice::(&self.body)?; Ok(Response { status: self.status, headers: self.headers, body }) } diff --git a/tests/execution/test-null-in-array.md b/tests/execution/test-null-in-array.md new file mode 100644 index 0000000000..1ec038ebe2 --- /dev/null +++ b/tests/execution/test-null-in-array.md @@ -0,0 +1,30 @@ +# Empty Array Response + +```graphql @server +schema @server { + query: Query +} + +type Query { + hi(id: ID!): [Company] @http(baseURL: "http://localhost:3000", path: "/hi") +} +type Company { + name: String + id: ID +} +``` + +```yml @mock +- request: + method: GET + url: http://localhost:3000/hi + response: + status: 200 +``` + +```yml @test +- method: POST + url: http://localhost:8080/graphql + body: + query: "query { hi (id: 1) { name id } }" +``` diff --git a/tests/execution/test-null-in-object.md b/tests/execution/test-null-in-object.md new file mode 100644 index 0000000000..a356b1f3be --- /dev/null +++ b/tests/execution/test-null-in-object.md @@ -0,0 +1,30 @@ +# Empty Object Response + +```graphql @server +schema @server { + query: Query +} + +type Query { + hi(id: ID!): Company @http(baseURL: "http://localhost:3000", path: "/hi") +} +type Company { + name: String + id: ID +} +``` + +```yml @mock +- request: + method: GET + url: http://localhost:3000/hi + response: + status: 200 +``` + +```yml @test +- method: POST + url: http://localhost:8080/graphql + body: + query: "query { hi (id: 1) { name id } }" +``` diff --git a/tests/snapshots/execution_spec__graphql-datasource-errors.md_test_0.snap b/tests/snapshots/execution_spec__graphql-datasource-errors.md_test_0.snap index 5e4910e7ab..e68bb4b23a 100644 --- a/tests/snapshots/execution_spec__graphql-datasource-errors.md_test_0.snap +++ b/tests/snapshots/execution_spec__graphql-datasource-errors.md_test_0.snap @@ -9,9 +9,7 @@ expression: response }, "body": { "data": { - "user": { - "name": null - } + "user": null }, "errors": [ { diff --git a/tests/snapshots/execution_spec__test-null-in-array.md_client.snap b/tests/snapshots/execution_spec__test-null-in-array.md_client.snap new file mode 100644 index 0000000000..285311d5cb --- /dev/null +++ b/tests/snapshots/execution_spec__test-null-in-array.md_client.snap @@ -0,0 +1,28 @@ +--- +source: tests/execution_spec.rs +expression: client +--- +type Company { + id: ID + name: String +} + +scalar Date + +scalar Email + +scalar Empty + +scalar JSON + +scalar PhoneNumber + +type Query { + hi(id: ID!): [Company] +} + +scalar Url + +schema { + query: Query +} diff --git a/tests/snapshots/execution_spec__test-null-in-array.md_merged.snap b/tests/snapshots/execution_spec__test-null-in-array.md_merged.snap new file mode 100644 index 0000000000..36cf068b16 --- /dev/null +++ b/tests/snapshots/execution_spec__test-null-in-array.md_merged.snap @@ -0,0 +1,16 @@ +--- +source: tests/execution_spec.rs +expression: merged +--- +schema @server @upstream { + query: Query +} + +type Company { + id: ID + name: String +} + +type Query { + hi(id: ID!): [Company] @http(baseURL: "http://localhost:3000", path: "/hi") +} diff --git a/tests/snapshots/execution_spec__test-null-in-array.md_test_0.snap b/tests/snapshots/execution_spec__test-null-in-array.md_test_0.snap new file mode 100644 index 0000000000..611acae330 --- /dev/null +++ b/tests/snapshots/execution_spec__test-null-in-array.md_test_0.snap @@ -0,0 +1,15 @@ +--- +source: tests/execution_spec.rs +expression: response +--- +{ + "status": 200, + "headers": { + "content-type": "application/json" + }, + "body": { + "data": { + "hi": null + } + } +} diff --git a/tests/snapshots/execution_spec__test-null-in-object.md_client.snap b/tests/snapshots/execution_spec__test-null-in-object.md_client.snap new file mode 100644 index 0000000000..3cf48f41f5 --- /dev/null +++ b/tests/snapshots/execution_spec__test-null-in-object.md_client.snap @@ -0,0 +1,28 @@ +--- +source: tests/execution_spec.rs +expression: client +--- +type Company { + id: ID + name: String +} + +scalar Date + +scalar Email + +scalar Empty + +scalar JSON + +scalar PhoneNumber + +type Query { + hi(id: ID!): Company +} + +scalar Url + +schema { + query: Query +} diff --git a/tests/snapshots/execution_spec__test-null-in-object.md_merged.snap b/tests/snapshots/execution_spec__test-null-in-object.md_merged.snap new file mode 100644 index 0000000000..877f21dbc2 --- /dev/null +++ b/tests/snapshots/execution_spec__test-null-in-object.md_merged.snap @@ -0,0 +1,16 @@ +--- +source: tests/execution_spec.rs +expression: merged +--- +schema @server @upstream { + query: Query +} + +type Company { + id: ID + name: String +} + +type Query { + hi(id: ID!): Company @http(baseURL: "http://localhost:3000", path: "/hi") +} diff --git a/tests/snapshots/execution_spec__test-null-in-object.md_test_0.snap b/tests/snapshots/execution_spec__test-null-in-object.md_test_0.snap new file mode 100644 index 0000000000..611acae330 --- /dev/null +++ b/tests/snapshots/execution_spec__test-null-in-object.md_test_0.snap @@ -0,0 +1,15 @@ +--- +source: tests/execution_spec.rs +expression: response +--- +{ + "status": 200, + "headers": { + "content-type": "application/json" + }, + "body": { + "data": { + "hi": null + } + } +}