Skip to content

Commit

Permalink
fix: null handling in the resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
amitksingh1490 committed Apr 19, 2024
1 parent 03f9829 commit 7f7edbe
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/blueprint/blueprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ impl Type {
Type::ListType { non_null, .. } => *non_null,
}
}

/// checks if the type is a list
pub fn is_list(&self) -> bool {
match self {
Type::NamedType { .. } => false,
Type::ListType { .. } => true,
}
}
}

#[derive(Clone, Debug)]
Expand Down
14 changes: 10 additions & 4 deletions src/blueprint/into_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn to_type(def: &Definition) -> dynamic::Type {
let field = field.clone();
let type_ref = to_type_ref(&field.of_type);
let field_name = &field.name.clone();
let is_list = field.of_type.is_list();
let mut dyn_schema_field = dynamic::Field::new(
field_name,
type_ref.clone(),
Expand Down Expand Up @@ -70,12 +71,17 @@ 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)),

Check warning on line 75 in src/blueprint/into_schema.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

Diff in /home/runner/work/tailcall/tailcall/src/blueprint/into_schema.rs
a => {
if a == ConstValue::Null && is_list {
FieldValue::NONE
} else {
FieldValue::try_from(a).ok()

Check failure on line 80 in src/blueprint/into_schema.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

use of a fallible conversion when an infallible one could be used
}
},

Check warning on line 82 in src/blueprint/into_schema.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

Diff in /home/runner/work/tailcall/tailcall/src/blueprint/into_schema.rs
};
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(),
});

Check warning on line 43 in src/http/response.rs

View check run for this annotation

Codecov / codecov/patch

src/http/response.rs#L39-L43

Added lines #L39 - L43 were not covered by tests
}
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 } }"
```
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")
}
18 changes: 18 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,18 @@
---
source: tests/execution_spec.rs
expression: response
---
{
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"data": {
"hi": {
"name": null,
"id": null
}
}
}
}

0 comments on commit 7f7edbe

Please sign in to comment.