Skip to content

Commit

Permalink
fix: query on nodes with the same type
Browse files Browse the repository at this point in the history
  • Loading branch information
MedHeikelBouzayene committed Nov 1, 2024
1 parent 2f64c1d commit a8c89d8
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 16 deletions.
26 changes: 15 additions & 11 deletions src/core/blueprint/operators/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,28 @@ fn create_related_fields(
for (name, field) in &type_.fields {
let mut new_type_names = type_names.clone();
new_type_names.pop_front();
let bool_self = field.type_of.name() == new_type_names.back()?;
new_type_names.push_back(field.type_of.name().to_string());
if !field.has_resolver() {
if let Some(modify) = &field.modify {
if let Some(modified_name) = &modify.name {
map.insert(
modified_name.clone(),
(
name.clone(),
create_related_fields(config, new_type_names, visited)?,
),
);
}
let used_name = match &field.modify {
Some(modify) => match &modify.name {
Some(modified_name) => Some(modified_name),
_ => None,
},
_ => Some(name),
};
if bool_self {
map.insert(
used_name?.to_string(),
(name.clone(), RelatedFields(HashMap::new()), true),
);
} else {
map.insert(
name.clone(),
used_name?.to_string(),
(
name.clone(),
create_related_fields(config, new_type_names, visited)?,
false,
),
);
}
Expand Down
8 changes: 6 additions & 2 deletions src/core/ir/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,12 @@ fn format_selection_set<'a>(
let set = selection_set
.filter_map(|field| {
// add to set only related fields that should be resolved with current resolver
related_fields.get(field.name()).map(|related_fields| {
format_selection_field(field, &related_fields.0, &related_fields.1)
related_fields.get(field.name()).map(|new_related_fields| {
if new_related_fields.2 {
format_selection_field(field, &new_related_fields.0, related_fields)
} else {
format_selection_field(field, &new_related_fields.0, &new_related_fields.1)
}
})
})
.collect::<Vec<_>>();
Expand Down
4 changes: 2 additions & 2 deletions src/core/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ pub use resolver_context_like::{
/// resolver i.e. fields that don't have their own resolver and are resolved by
/// the ancestor
#[derive(Debug, Default, Clone)]
pub struct RelatedFields(pub HashMap<String, (String, RelatedFields)>);
pub struct RelatedFields(pub HashMap<String, (String, RelatedFields, bool)>);

impl Deref for RelatedFields {
type Target = HashMap<String, (String, RelatedFields)>;
type Target = HashMap<String, (String, RelatedFields, bool)>;

fn deref(&self) -> &Self::Target {
&self.0
Expand Down
4 changes: 4 additions & 0 deletions tests/core/snapshots/graphql-conformance-019.md_0.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: tests/core/spec.rs
expression: response
snapshot_kind: text
---
{
"status": 200,
Expand All @@ -16,6 +17,9 @@ expression: response
},
"nodeC": {
"name": "nodeC"
},
"child": {
"name": "nodeA"
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/core/snapshots/graphql-conformance-019.md_client.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
---
source: tests/core/spec.rs
expression: formatted
snapshot_kind: text
---
type NodeA {
child: NodeA
name: String
nodeB: NodeB
nodeC: NodeC
Expand Down
2 changes: 2 additions & 0 deletions tests/core/snapshots/graphql-conformance-019.md_merged.snap
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
---
source: tests/core/spec.rs
expression: formatter
snapshot_kind: text
---
schema @server(hostname: "0.0.0.0", port: 8000) @upstream {
query: Query
}

type NodeA {
name: String
nodeA: NodeA @modify(name: "child")
nodeB: NodeB
nodeC: NodeC
}
Expand Down
8 changes: 7 additions & 1 deletion tests/execution/graphql-conformance-019.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type NodeA {
name: String
nodeB: NodeB
nodeC: NodeC
nodeA: NodeA @modify(name: "child")
}

type NodeB {
Expand All @@ -32,7 +33,7 @@ type NodeC {
- request:
method: POST
url: http://upstream/graphql
textBody: '{ "query": "query { nodeA { name nodeB { name } nodeC { name } } }" }'
textBody: '{ "query": "query { nodeA { name nodeB { name } nodeC { name } nodeA { name } } }" }'
expectedHits: 1
response:
status: 200
Expand All @@ -44,6 +45,8 @@ type NodeC {
name: nodeB
nodeC:
name: nodeC
nodeA:
name: nodeA
```

```yml @test
Expand All @@ -60,6 +63,9 @@ type NodeC {
nodeC {
name
}
child {
name
}
}
}
```

0 comments on commit a8c89d8

Please sign in to comment.