Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(discriminator): union nullable #2982

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/core/ir/discriminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ where
if let Some(obj) = self.as_object_mut() {
obj.insert_key(TYPENAME_FIELD, T::string(type_name.into()));

Ok(())
} else {
bail!("Expected object")
return Ok(());
}

if self.is_null() {
return Ok(());
}

bail!("Value expected to be object")
}
}

Expand Down Expand Up @@ -70,6 +74,8 @@ pub struct Discriminator {
/// Set of all fields that are part of types with
/// the [FieldInfo] about their relations to types.
fields_info: IndexMap<String, FieldInfo>,
/// The name of parent type
name: String,
}

impl std::fmt::Debug for Discriminator {
Expand Down Expand Up @@ -227,7 +233,7 @@ impl Discriminator {
.collect()
};

let discriminator = Self { fields_info, types };
let discriminator = Self { fields_info, types, name: union_name.to_string() };

tracing::debug!(
"Generated discriminator for union type '{union_name}':\n{discriminator:?}",
Expand All @@ -238,7 +244,7 @@ impl Discriminator {

pub fn resolve_type(&self, value: &Value) -> Result<&str> {
let Value::Object(obj) = value else {
bail!("Value expected to be object");
return Ok(&self.name);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it could hide some errors when we are trying to resolve object type but getting something else. Null is a special case though, what about return Option<&str> here and for null use None?

};

let mut possible_types = Repr::all_covered(self.types.len());
Expand Down Expand Up @@ -1228,17 +1234,15 @@ mod tests {
assert_eq!(
discriminator
.resolve_type(&Value::from_json(json!("string")).unwrap())
.unwrap_err()
.to_string(),
"Value expected to be object"
.unwrap(),
"Test"
);

assert_eq!(
discriminator
.resolve_type(&Value::from_json(json!(25)).unwrap())
.unwrap_err()
.to_string(),
"Value expected to be object"
.unwrap(),
"Test"
);
}
}
15 changes: 15 additions & 0 deletions tests/core/snapshots/test-union-optional.md_0.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: tests/core/spec.rs
expression: response
---
{
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"data": {
"nodes": null
}
}
}
59 changes: 59 additions & 0 deletions tests/core/snapshots/test-union-optional.md_client.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
source: tests/core/spec.rs
expression: formatted
---
scalar Bytes

scalar Date

scalar DateTime

scalar Email

scalar Empty

scalar Int128

scalar Int16

scalar Int32

scalar Int64

scalar Int8

scalar JSON

union Node = Page | User

type Page {
id: ID!
slug: String!
}

scalar PhoneNumber

type Query {
nodes: Node
}

scalar UInt128

scalar UInt16

scalar UInt32

scalar UInt64

scalar UInt8

scalar Url

type User {
id: ID!
username: String!
}

schema {
query: Query
}
23 changes: 23 additions & 0 deletions tests/core/snapshots/test-union-optional.md_merged.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: tests/core/spec.rs
expression: formatter
---
schema @server @upstream {
query: Query
}

union Node = Page | User

type Page {
id: ID!
slug: String!
}

type Query {
nodes: Node @expr
}

type User {
id: ID!
username: String!
}
43 changes: 43 additions & 0 deletions tests/execution/test-union-optional.md
Copy link
Contributor

Choose a reason for hiding this comment

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

let's update the test with list case as well

type Query {
  node: Node @expr(body: null)
  nodes: [Node] @expr(body: [{id: 1, username: "user"}, null, {id: 2, slug: "page"}])
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Test union optional

```graphql @config
schema @server {
query: Query
}

type Query {
nodes: Node @expr(body: null)
}

union Node = User | Page

type User {
id: ID!
username: String!
}

type Page {
id: ID!
slug: String!
}
```

```yml @test
- method: POST
url: http://localhost:8080/graphql
body:
query: |
{
nodes {
__typename
... on Page {
id
slug
}
... on User {
id
username
}
}
}
```
Loading