Skip to content

Commit

Permalink
entc/gen: set Ref and Inverse for edge contains both From and To
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Mar 8, 2022
1 parent 78a0fd9 commit 0c7679e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 7 additions & 3 deletions entc/gen/graph.go
Expand Up @@ -310,7 +310,7 @@ func (g *Graph) addEdges(schema *load.Schema) {
ref := e.Ref
expect(e.RefName == "", "reference name is derived from the assoc name: %s.%s <-> %s.%s", t.Name, ref.Name, t.Name, e.Name)
expect(ref.Type == t.Name, "assoc-inverse edge allowed only as o2o relation of the same type")
t.Edges = append(t.Edges, &Edge{
from := &Edge{
def: e,
Type: typ,
Name: e.Name,
Expand All @@ -320,16 +320,20 @@ func (g *Graph) addEdges(schema *load.Schema) {
Optional: !e.Required,
StructTag: structTag(e.Name, e.Tag),
Annotations: e.Annotations,
}, &Edge{
}
to := &Edge{
def: ref,
Ref: from,
Type: typ,
Owner: t,
Name: ref.Name,
Unique: ref.Unique,
Optional: !ref.Required,
StructTag: structTag(ref.Name, ref.Tag),
Annotations: ref.Annotations,
})
}
from.Ref = to
t.Edges = append(t.Edges, from, to)
default:
panic(graphError{"edge must be either an assoc or inverse edge"})
}
Expand Down
4 changes: 4 additions & 0 deletions entc/gen/graph_test.go
Expand Up @@ -123,7 +123,11 @@ func TestNewGraph(t *testing.T) {
require.Equal(graph.Nodes[0], e1.Type)

require.Equal("t2_m2m_from", t2.Edges[5].Name)
require.Equal("t2_m2m_to", t2.Edges[5].Inverse)
require.Equal("t2_m2m_to", t2.Edges[6].Name)
require.Empty(t2.Edges[6].Inverse)
require.Equal(t2.Edges[6], t2.Edges[5].Ref)
require.Equal(t2.Edges[5], t2.Edges[6].Ref)
require.Equal(map[string]string{"Name": "From"}, t2.Edges[5].Annotations["GQL"])
require.Equal(map[string]string{"Name": "To"}, t2.Edges[6].Annotations["GQL"])
}
Expand Down

0 comments on commit 0c7679e

Please sign in to comment.