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: bug with graphql nested queries #3089

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
98 changes: 65 additions & 33 deletions src/core/blueprint/operators/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,67 @@
use crate::core::ir::RelatedFields;
use crate::core::try_fold::TryFold;

#[allow(clippy::too_many_arguments)]
fn create_related_fields(
config: &Config,
type_name: &str,
visited: &mut HashSet<String>,
) -> RelatedFields {
paths: &mut HashMap<String, Vec<String>>,
path: Vec<String>,
root: &str,
) -> Option<RelatedFields> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
) -> Option<RelatedFields> {
) -> Valid<RelatedFields, String> {

let mut map = HashMap::new();
if visited.contains(type_name) {
return RelatedFields(map);
return Some(RelatedFields(map));
}
visited.insert(type_name.to_string());

if let Some(type_) = config.find_type(type_name) {
for (name, field) in &type_.fields {
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, field.type_of.name(), visited),
),
);
}
} else {
map.insert(
let used_name = match &field.modify {
Some(modify) => match &modify.name {
Some(modified_name) => Some(modified_name),
_ => None,

Check warning on line 35 in src/core/blueprint/operators/graphql.rs

View check run for this annotation

Codecov / codecov/patch

src/core/blueprint/operators/graphql.rs#L35

Added line #L35 was not covered by tests
},
_ => Some(name),
};
let mut next_path = path.clone();
next_path.push(used_name?.to_string());
if !(paths.contains_key(field.type_of.name())) {
paths.insert(field.type_of.name().to_string(), next_path.clone());
};
map.insert(
used_name?.to_string(),
(
name.clone(),
(
name.clone(),
create_related_fields(config, field.type_of.name(), visited),
),
);
}
create_related_fields(
config,
field.type_of.name(),
visited,
paths,
next_path.clone(),
root,
)?,
paths.get(field.type_of.name())?.to_vec(),
root == field.type_of.name(),
),
);
}
}
} else if let Some(union_) = config.find_union(type_name) {
for type_name in &union_.types {
map.extend(create_related_fields(config, type_name, visited).0);
let mut next_path = path.clone();
next_path.push(type_name.to_string());
if !(paths.contains_key(type_name)) {
paths.insert(type_name.to_string(), next_path.clone());
};
map.extend(
create_related_fields(config, type_name, visited, paths, next_path, root)?.0,

Check warning on line 70 in src/core/blueprint/operators/graphql.rs

View check run for this annotation

Codecov / codecov/patch

src/core/blueprint/operators/graphql.rs#L64-L70

Added lines #L64 - L70 were not covered by tests
);
}
};

RelatedFields(map)
Some(RelatedFields(map))
}

pub fn compile_graphql(
Expand All @@ -66,17 +85,30 @@
Valid::succeed(graphql.url.as_str())
.zip(helpers::headers::to_mustache_headers(&graphql.headers))
.and_then(|(base_url, headers)| {
Valid::from(
RequestTemplate::new(
base_url.to_owned(),
operation_type,
&graphql.name,
args,
headers,
create_related_fields(config, type_name, &mut HashSet::new()),
)
.map_err(|e| ValidationError::new(e.to_string())),
Valid::from_option(
create_related_fields(
config,
type_name,
&mut HashSet::new(),
&mut HashMap::new(),
vec![],
type_name,
),
"Logical error occurred while creating Related Fields".to_string(),
)
.and_then(|related_fields| {
Valid::from(
RequestTemplate::new(
base_url.to_owned(),
operation_type,
&graphql.name,
args,
headers,
related_fields,
)
.map_err(|e| ValidationError::new(e.to_string())),
)
})
Copy link
Contributor

Choose a reason for hiding this comment

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

This implementation needs to be changed. The related field information can't be inferred while creating a blueprint. It should be implemented as a part of the JIT transformation step. The JIT Transformations should identify exactly what the RequestTemplate should look like for each query and then while evaluating we should simply use that operation and execute the GraphQL request.

})
.map(|req_template| {
let field_name = graphql.name.clone();
Expand Down
38 changes: 32 additions & 6 deletions src/core/ir/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,43 @@ impl<Ctx: ResolverContextLike> GraphQLOperationContext for EvalContext<'_, Ctx>

fn selection_set(&self, related_fields: &RelatedFields) -> Option<String> {
let selection_field = self.graphql_ctx.field()?;
format_selection_set(selection_field.selection_set(), related_fields)
format_selection_set(
selection_field.selection_set(),
Some(related_fields),
related_fields,
)
}
}

fn format_selection_set<'a>(
selection_set: impl Iterator<Item = &'a SelectionField>,
related_fields: &RelatedFields,
related_fields: Option<&RelatedFields>,
global_related_fields: &RelatedFields,
) -> Option<String> {
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| {
let next_related_fields = if new_related_fields.3 {
Some(global_related_fields)
} else {
new_related_fields.2.iter().try_fold(
global_related_fields,
|parent_related_fields, node| {
parent_related_fields.get(node).map(
|(_nickname, next_related_fields, _path, _root)| {
next_related_fields
},
)
},
)
};
format_selection_field(
field,
&new_related_fields.0,
next_related_fields,
global_related_fields,
)
})
})
.collect::<Vec<_>>();
Expand All @@ -146,10 +170,12 @@ fn format_selection_set<'a>(
fn format_selection_field(
field: &SelectionField,
name: &str,
related_fields: &RelatedFields,
related_fields: Option<&RelatedFields>,
global_related_fields: &RelatedFields,
) -> String {
let arguments = format_selection_field_arguments(field);
let selection_set = format_selection_set(field.selection_set(), related_fields);
let selection_set =
format_selection_set(field.selection_set(), related_fields, global_related_fields);

let mut output = format!("{}{}", name, arguments);

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, Vec<String>, bool)>);

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

fn deref(&self) -> &Self::Target {
&self.0
Expand Down
27 changes: 27 additions & 0 deletions tests/core/snapshots/graphql-conformance-019.md_0.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: tests/core/spec.rs
expression: response
snapshot_kind: text
---
{
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"data": {
"queryNodeA": {
"name": "nodeA",
"nodeB": {
"name": "nodeB"
},
"nodeC": {
"name": "nodeC"
},
"child": {
"name": "nodeA"
}
}
}
}
}
31 changes: 31 additions & 0 deletions tests/core/snapshots/graphql-conformance-019.md_client.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
source: tests/core/spec.rs
expression: formatted
snapshot_kind: text
---
type NodeA {
child: NodeA
name: String
nodeB: NodeB
nodeC: NodeC
}

type NodeB {
name: String
nodeA: NodeA
nodeC: NodeC
}

type NodeC {
name: String
nodeA: NodeA
nodeB: NodeB
}

type Query {
queryNodeA: NodeA
}

schema {
query: Query
}
31 changes: 31 additions & 0 deletions tests/core/snapshots/graphql-conformance-019.md_merged.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
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
}

type NodeB {
name: String
nodeA: NodeA
nodeC: NodeC
}

type NodeC {
name: String
nodeA: NodeA
nodeB: NodeB
}

type Query {
queryNodeA: NodeA @graphQL(url: "http://upstream/graphql", name: "nodeA")
}
71 changes: 71 additions & 0 deletions tests/execution/graphql-conformance-019.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Complicated queries

```graphql @config
schema @server(port: 8000, hostname: "0.0.0.0") {
query: Query
}

type Query {
queryNodeA: NodeA @graphQL(url: "http://upstream/graphql", name: "nodeA")
}

type NodeA {
name: String
nodeB: NodeB
nodeC: NodeC
nodeA: NodeA @modify(name: "child")
}

type NodeB {
name: String
nodeA: NodeA
nodeC: NodeC
}

type NodeC {
name: String
nodeA: NodeA
nodeB: NodeB
}
```

```yml @mock
- request:
method: POST
url: http://upstream/graphql
textBody: '{ "query": "query { nodeA { name nodeB { name } nodeC { name } nodeA { name } } }" }'
expectedHits: 1
response:
status: 200
body:
data:
nodeA:
name: nodeA
nodeB:
name: nodeB
nodeC:
name: nodeC
nodeA:
name: nodeA
```

```yml @test
- method: POST
url: http://localhost:8080/graphql
body:
query: |
query queryNodeA {
queryNodeA {
name
nodeB {
name
}
nodeC {
name
}
child {
name
}
}
}
```
Loading