Skip to content

Commit

Permalink
fix bug in graph edge filtering, add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
benweint committed Jun 3, 2024
1 parent 5a05757 commit 01e37c3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
47 changes: 47 additions & 0 deletions pkg/commands/testdata/cases/viz_from/expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
digraph {
rankdir=LR
ranksep=2
node [shape=box fontname=Courier]
n_Apple [shape=plain, label=<<TABLE>
<TR><TD COLSPAN="3" PORT="main" BGCOLOR="#fbb4ae">object Apple</TD></TR>
<TR><TD ROWSPAN="1">variety</TD><TD COLSPAN="2" PORT="p_variety">AppleVariety</TD></TR>
<TR><TD ROWSPAN="1">measurements</TD><TD COLSPAN="2" PORT="p_measurements">Measurements</TD></TR>
<TR><TD ROWSPAN="1">calories</TD><TD COLSPAN="2" PORT="p_calories">Int</TD></TR>

</TABLE>>]
n_AppleVariety [shape=plain, label=<<TABLE>
<TR><TD PORT="main" BGCOLOR="#decbe4">enum AppleVariety</TD></TR> <TR><TD>FUJI</TD></TR>\n <TR><TD>COSMIC_CRISP</TD></TR>\n <TR><TD>GRANNY_SMITH</TD></TR>\n</TABLE>>]
n_Fruit [shape=plain, label=<<TABLE>
<TR><TD PORT="main" BGCOLOR="#fed9a6">union Fruit</TD></TR> <TR><TD PORT="p_Apple">Apple</TD></TR>\n <TR><TD PORT="p_Orange">Orange</TD></TR>\n</TABLE>>]
n_Measurements [shape=plain, label=<<TABLE>
<TR><TD COLSPAN="3" PORT="main" BGCOLOR="#fbb4ae">object Measurements</TD></TR>
<TR><TD ROWSPAN="1">height</TD><TD COLSPAN="2" PORT="p_height">Int</TD></TR>
<TR><TD ROWSPAN="1">width</TD><TD COLSPAN="2" PORT="p_width">Int</TD></TR>
<TR><TD ROWSPAN="1">depth</TD><TD COLSPAN="2" PORT="p_depth">Int</TD></TR>

</TABLE>>]
n_Orange [shape=plain, label=<<TABLE>
<TR><TD COLSPAN="3" PORT="main" BGCOLOR="#fbb4ae">object Orange</TD></TR>
<TR><TD ROWSPAN="1">variety</TD><TD COLSPAN="2" PORT="p_variety">OrangeVariety</TD></TR>
<TR><TD ROWSPAN="1">calories</TD><TD COLSPAN="2" PORT="p_calories">Int</TD></TR>

</TABLE>>]
n_OrangeVariety [shape=plain, label=<<TABLE>
<TR><TD PORT="main" BGCOLOR="#decbe4">enum OrangeVariety</TD></TR> <TR><TD>VALENCIA</TD></TR>\n <TR><TD>NAVEL</TD></TR>\n <TR><TD>CARA_CARA</TD></TR>\n</TABLE>>]
n_Query [shape=plain, label=<<TABLE>
<TR><TD COLSPAN="3" PORT="main" BGCOLOR="#fbb4ae">object Query</TD></TR>
<TR><TD ROWSPAN="2">fruit</TD><TD COLSPAN="2" PORT="p_fruit">Fruit</TD></TR>
<TR><TD>name</TD><TD PORT="p_fruit_name">String</TD></TR>
<TR><TD ROWSPAN="2">edible</TD><TD COLSPAN="2" PORT="p_edible">Edible</TD></TR>
<TR><TD>name</TD><TD PORT="p_edible_name">String</TD></TR>
<TR><TD ROWSPAN="2">edibles</TD><TD COLSPAN="2" PORT="p_edibles">[Edible!]!</TD></TR>
<TR><TD>filter</TD><TD PORT="p_edibles_filter">Filter</TD></TR>

</TABLE>>]
n_Apple:p_variety -> n_AppleVariety:main
n_Apple:p_measurements -> n_Measurements:main
n_Fruit:p_Apple -> n_Apple:main
n_Fruit:p_Orange -> n_Orange:main
n_Orange:p_variety -> n_OrangeVariety:main
n_Query:p_fruit -> n_Fruit:main
}
1 change: 1 addition & 0 deletions pkg/commands/testdata/cases/viz_from/meta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
args: ["viz", "--from", "Query.fruit", "testdata/in.graphql"]
10 changes: 6 additions & 4 deletions pkg/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ func (g *Graph) GetDefinitions() model.DefinitionMap {

func (g *Graph) ReachableFrom(roots []*model.NameReference, maxDepth int) *Graph {
var defs model.DefinitionList

for _, node := range g.nodes {
defs = append(defs, node)
}
Expand Down Expand Up @@ -191,11 +190,15 @@ func (g *Graph) ReachableFrom(roots []*model.NameReference, maxDepth int) *Graph
for from, edges := range g.edges {
var filtered []*edge
for _, edge := range edges {
if _, ok := g.nodes[edge.src.Name]; ok {
_, srcPresent := filteredNodes[edge.src.Name]
_, dstPresent := filteredNodes[edge.dst.Name]
if srcPresent && dstPresent {
filtered = append(filtered, edge)
}
}
filteredEdges[from] = filtered
if len(filtered) > 0 {
filteredEdges[from] = filtered
}
}

return &Graph{
Expand Down Expand Up @@ -229,7 +232,6 @@ func (g *Graph) buildNodeDefs() []string {
}

func (g *Graph) buildEdgeDefs() []string {

var result []string
for _, sourceNodeName := range sortedKeys(g.edges) {
edges := g.edges[sourceNodeName]
Expand Down

0 comments on commit 01e37c3

Please sign in to comment.