Skip to content

Commit

Permalink
fix: null handling in the resolvers (#1760)
Browse files Browse the repository at this point in the history
  • Loading branch information
amitksingh1490 authored Apr 19, 2024
1 parent ed7bdc6 commit d5cb75d
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/blueprint/into_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
9 changes: 8 additions & 1 deletion src/http/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ impl Response<Bytes> {
}
}

pub fn to_json<T: DeserializeOwned>(self) -> Result<Response<T>> {
pub fn to_json<T: DeserializeOwned + Default>(self) -> Result<Response<T>> {
if self.body.is_empty() {
return Ok(Response {
status: self.status,
headers: self.headers,
body: Default::default(),
});
}
let body = serde_json::from_slice::<T>(&self.body)?;
Ok(Response { status: self.status, headers: self.headers, body })
}
Expand Down
30 changes: 30 additions & 0 deletions tests/execution/test-null-in-array.md
Original file line number Diff line number Diff line change
@@ -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 } }"
```
30 changes: 30 additions & 0 deletions tests/execution/test-null-in-object.md
Original file line number Diff line number Diff line change
@@ -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 } }"
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ expression: response
},
"body": {
"data": {
"user": {
"name": null
}
"user": null
},
"errors": [
{
Expand Down
28 changes: 28 additions & 0 deletions tests/snapshots/execution_spec__test-null-in-array.md_client.snap
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 16 additions & 0 deletions tests/snapshots/execution_spec__test-null-in-array.md_merged.snap
Original file line number Diff line number Diff line change
@@ -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")
}
15 changes: 15 additions & 0 deletions tests/snapshots/execution_spec__test-null-in-array.md_test_0.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: tests/execution_spec.rs
expression: response
---
{
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"data": {
"hi": null
}
}
}
28 changes: 28 additions & 0 deletions tests/snapshots/execution_spec__test-null-in-object.md_client.snap
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 16 additions & 0 deletions tests/snapshots/execution_spec__test-null-in-object.md_merged.snap
Original file line number Diff line number Diff line change
@@ -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")
}
15 changes: 15 additions & 0 deletions tests/snapshots/execution_spec__test-null-in-object.md_test_0.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: tests/execution_spec.rs
expression: response
---
{
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"data": {
"hi": null
}
}
}

1 comment on commit d5cb75d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 7.66ms 3.59ms 107.91ms 73.42%
Req/Sec 3.31k 290.09 4.30k 86.50%

395550 requests in 30.02s, 1.98GB read

Requests/sec: 13177.21

Transfer/sec: 67.63MB

Please sign in to comment.