기존 블로그 글에 root 뿐만 아니라 edge에도 조건을 달고 싶을 경우, 복잡한 절차를 해야 했다. 그런데 더 찾아보니 더 간단한 해결 방법이 있었다.
참고
https://velog.io/@dasd412/how-to-add-relationshipedge-filter-in-graphqlfeatgolang
entgql.RelayConnection()
을 활용하면 간단하게 해결할 수 있다.
다음처럼 entgo schema
패키지 내에 있는 구조체들의 edge()
, annotations()
메서드에 entgql.RelayConnection()
을 추가한다. 그러면 where
뿐만 아니라 orderBy
, first
,after
등 여러 가지 조건을 엣지에도 달 수 있게 된다.
func (Post) Edges() []ent.Edge {
return []ent.Edge{
edge.To("comments", Comment.Type).
Annotations(
entgql.RelayConnection(),
entproto.Field(10),
),
}
}
func (Post) Annotations() []schema.Annotation {
return []schema.Annotation{
entgql.RelayConnection(),
}
}