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: recursive types on @graphql directive #2528

Merged
merged 2 commits into from
Jul 25, 2024
Merged
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
21 changes: 16 additions & 5 deletions src/core/blueprint/operators/graphql.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};

use crate::core::blueprint::FieldDefinition;
use crate::core::config::{Config, ConfigModule, Field, GraphQL, GraphQLOperationType, Type};
Expand All @@ -9,18 +9,29 @@
use crate::core::try_fold::TryFold;
use crate::core::valid::{Valid, ValidationError, Validator};

fn create_related_fields(config: &Config, type_name: &str) -> RelatedFields {
fn create_related_fields(
config: &Config,
type_name: &str,
visited: &mut HashSet<String>,
) -> RelatedFields {
let mut map = HashMap::new();
if visited.contains(type_name) {
return 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() {
map.insert(name.clone(), create_related_fields(config, &field.type_of));
map.insert(
name.clone(),
create_related_fields(config, &field.type_of, visited),
);
}
}
} else if let Some(union_) = config.find_union(type_name) {
for type_name in &union_.types {
map.extend(create_related_fields(config, type_name).0);
map.extend(create_related_fields(config, type_name, visited).0);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L34 was not covered by tests
}
};

Expand Down Expand Up @@ -50,7 +61,7 @@
&graphql.name,
args,
headers,
create_related_fields(config, type_name),
create_related_fields(config, type_name, &mut HashSet::new()),
)
.map_err(|e| ValidationError::new(e.to_string())),
)
Expand Down
55 changes: 55 additions & 0 deletions tests/core/snapshots/related-fields-recursive.md_client.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
source: tests/core/spec.rs
expression: formatted
---
scalar Bytes

scalar Date

scalar Email

scalar Empty

scalar Int128

scalar Int16

scalar Int32

scalar Int64

scalar Int8

scalar JSON

type NodeA {
name: String
nodeB: NodeB
}

type NodeB {
name: String
nodeA: NodeA
}

scalar PhoneNumber

type Query {
queryNodeA: [NodeA]
}

scalar UInt128

scalar UInt16

scalar UInt32

scalar UInt64

scalar UInt8

scalar Url

schema {
query: Query
}
23 changes: 23 additions & 0 deletions tests/core/snapshots/related-fields-recursive.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(hostname: "0.0.0.0", port: 8000) @upstream(baseURL: "http://localhost:8083/graphql") {
query: Query
}

scalar DateTime

type NodeA {
name: String
nodeB: NodeB
}

type NodeB {
name: String
nodeA: NodeA
}

type Query {
queryNodeA: [NodeA] @graphQL(name: "queryNodeA")
}
21 changes: 21 additions & 0 deletions tests/execution/related-fields-recursive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
```graphql @config
scalar DateTime

schema @server(port: 8000, hostname: "0.0.0.0") @upstream(baseURL: "http://localhost:8083/graphql") {
query: Query
}

type Query {
queryNodeA: [NodeA] @graphQL(name: "queryNodeA", batch: false)
}

type NodeA {
name: String
nodeB: NodeB
}

type NodeB {
name: String
nodeA: NodeA
}
```
Loading