Skip to content

Commit

Permalink
fix: recursive types on @graphql directive (#2528)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddOnTop authored Jul 25, 2024
1 parent 81bbb1a commit 89e021b
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 5 deletions.
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::ir::RelatedFields;
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);
}
};

Expand Down Expand Up @@ -50,7 +61,7 @@ pub fn compile_graphql(
&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
}
```

0 comments on commit 89e021b

Please sign in to comment.