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

entgql: move the order-fields logic to template function #273

Merged
merged 2 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 7 additions & 9 deletions entgql/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (a Annotation) Merge(other schema.Annotation) schema.Annotation {
return a
}

// Decode unmarshal annotation
// Decode unmarshalls the annotation.
func (a *Annotation) Decode(annotation interface{}) error {
buf, err := json.Marshal(annotation)
if err != nil {
Expand All @@ -173,15 +173,13 @@ func (a *Annotation) Decode(annotation interface{}) error {
return json.Unmarshal(buf, a)
}

// decodeAnnotation decodes the annotation from the schema.
func decodeAnnotation(annotations gen.Annotations) (*Annotation, error) {
// annotation extracts the entgql.Annotation or returns its empty value.
func annotation(ants gen.Annotations) (*Annotation, error) {
ant := &Annotation{}
if annotations == nil || annotations[ant.Name()] == nil {
return ant, nil
}

if err := ant.Decode(annotations[ant.Name()]); err != nil {
return nil, err
if ants != nil || ants[ant.Name()] != nil {
a8m marked this conversation as resolved.
Show resolved Hide resolved
if err := ant.Decode(ants[ant.Name()]); err != nil {
return nil, err
}
}
return ant, nil
}
Expand Down
3 changes: 1 addition & 2 deletions entgql/internal/todo/ent/gql_edge.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions entgql/internal/todopulid/ent/gql_edge.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions entgql/internal/todouuid/ent/gql_edge.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions entgql/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (e *schemaGenerator) buildTypes() (map[string]*ast.Definition, error) {
defaultInterfaces = append(defaultInterfaces, "Node")
}
for _, node := range e.nodes {
ant, err := decodeAnnotation(node.Annotations)
ant, err := annotation(node.Annotations)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -160,7 +160,7 @@ func (e *schemaGenerator) buildTypes() (map[string]*ast.Definition, error) {

var enumOrderByValues ast.EnumValueList
for _, f := range node.Fields {
ant, err := decodeAnnotation(f.Annotations)
ant, err := annotation(f.Annotations)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -307,7 +307,7 @@ func (e *schemaGenerator) buildTypeFields(t *gen.Type) (ast.FieldList, error) {
}

func (e *schemaGenerator) typeField(f *gen.Field, isID bool) ([]*ast.FieldDefinition, error) {
ant, err := decodeAnnotation(f.Annotations)
ant, err := annotation(f.Annotations)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -387,7 +387,7 @@ func (e *schemaGenerator) genModels() (map[string]string, error) {
models[RelayCursor] = e.entGoType(RelayCursor)
}
for _, node := range e.nodes {
ant, err := decodeAnnotation(node.Annotations)
ant, err := annotation(node.Annotations)
if err != nil {
return nil, err
}
Expand All @@ -403,7 +403,7 @@ func (e *schemaGenerator) genModels() (map[string]string, error) {

var hasOrderBy bool
for _, field := range node.Fields {
ant, err := decodeAnnotation(field.Annotations)
ant, err := annotation(field.Annotations)
if err != nil {
return nil, err
}
Expand Down
91 changes: 47 additions & 44 deletions entgql/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ var (
"fieldCollections": fieldCollections,
"filterEdges": filterEdges,
"filterFields": filterFields,
"orderFields": orderFields,
"filterNodes": filterNodes,
"findIDType": findIDType,
"nodePaginationNames": nodePaginationNames,
Expand Down Expand Up @@ -110,11 +111,8 @@ func fieldCollections(edges []*gen.Edge) (map[string]fieldCollection, error) {
Name: e.Type.Name,
Mapping: []string{e.Name},
}
ant := &Annotation{}
if e.Annotations == nil || e.Annotations[ant.Name()] == nil {
continue
}
if err := ant.Decode(e.Annotations[ant.Name()]); err != nil {
ant, err := annotation(e.Annotations)
if err != nil {
return nil, err
}
if ant.Unbind {
Expand All @@ -135,67 +133,72 @@ func fieldCollections(edges []*gen.Edge) (map[string]fieldCollection, error) {

// filterNodes filters out nodes that should not be included in the GraphQL schema.
func filterNodes(nodes []*gen.Type) ([]*gen.Type, error) {
var filteredNodes []*gen.Type
filteredNodes := make([]*gen.Type, 0, len(nodes))
for _, n := range nodes {
ant := &Annotation{}
if n.Annotations != nil && n.Annotations[ant.Name()] != nil {
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
return nil, err
}
if ant.Skip {
continue
}
ant, err := annotation(n.Annotations)
if err != nil {
return nil, err
}
if !ant.Skip {
filteredNodes = append(filteredNodes, n)
}
filteredNodes = append(filteredNodes, n)
}
return filteredNodes, nil
}

// filterEdges filters out edges that should not be included in the GraphQL schema.
func filterEdges(edges []*gen.Edge) ([]*gen.Edge, error) {
var filteredEdges []*gen.Edge
filteredEdges := make([]*gen.Edge, 0, len(edges))
for _, e := range edges {
ant := &Annotation{}
if e.Annotations != nil && e.Annotations[ant.Name()] != nil {
if err := ant.Decode(e.Annotations[ant.Name()]); err != nil {
return nil, err
}
if ant.Skip {
continue
}
antE, err := annotation(e.Annotations)
if err != nil {
return nil, err
}
// Check if type is skipped
if e.Type.Annotations != nil && e.Type.Annotations[ant.Name()] != nil {
if err := ant.Decode(e.Type.Annotations[ant.Name()]); err != nil {
return nil, err
}
if ant.Skip {
continue
}
antT, err := annotation(e.Type.Annotations)
if err != nil {
return nil, err
}
if !antE.Skip && !antT.Skip {
filteredEdges = append(filteredEdges, e)
}
filteredEdges = append(filteredEdges, e)
}
return filteredEdges, nil
}

// filterFields filters out fields that should not be included in the GraphQL schema.
func filterFields(fields []*gen.Field) ([]*gen.Field, error) {
var filteredFields []*gen.Field
filteredFields := make([]*gen.Field, 0, len(fields))
for _, f := range fields {
ant := &Annotation{}
if f.Annotations != nil && f.Annotations[ant.Name()] != nil {
if err := ant.Decode(f.Annotations[ant.Name()]); err != nil {
return nil, err
}
if ant.Skip {
continue
}
ant, err := annotation(f.Annotations)
if err != nil {
return nil, err
}
if !ant.Skip {
filteredFields = append(filteredFields, f)
}
filteredFields = append(filteredFields, f)
}
return filteredFields, nil
}

// orderFields returns the fields of the given node with the `OrderField` annotation.
func orderFields(n *gen.Type) ([]*gen.Field, error) {
var ordered []*gen.Field
for _, f := range n.Fields {
ant, err := annotation(f.Annotations)
if err != nil {
return nil, err
}
if ant.OrderField == "" {
continue
}
if !f.Type.Comparable() {
return nil, fmt.Errorf("entgql: ordered field %s.%s must be comparable", n.Name, f.Name)
}
ordered = append(ordered, f)
}
return ordered, nil
}

// PaginationNames holds the names of the pagination fields.
type PaginationNames struct {
Connection string
Expand All @@ -208,7 +211,7 @@ type PaginationNames struct {
// nodePaginationNames returns the names of the pagination types for the node.
func nodePaginationNames(t *gen.Type) (*PaginationNames, error) {
node := t.Name
ant, err := decodeAnnotation(t.Annotations)
ant, err := annotation(t.Annotations)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions entgql/template/edge.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ import "context"

func ({{ $r }} *{{ $n.Name }}) {{ $e.StructField }}(
ctx context.Context, after *Cursor, first *int,
before *Cursor, last *int, orderBy *{{ $order }},
opts ...{{ $opt }},
before *Cursor, last *int, {{ if orderFields $e.Type }}orderBy *{{ $order }},{{ end }} opts ...{{ $opt }},
) (*{{ $conn }}, error) {
query := {{ $r }}.Query{{ $e.StructField }}()
{{ with extend $n "Node" $e.Type "Query" "query" -}}
Expand Down
12 changes: 1 addition & 11 deletions entgql/template/pagination.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,7 @@ const (
)

{{ range $node := $gqlNodes -}}
{{ $orderFields := list -}}
{{- range $f := append (filterFields $node.Fields) $node.ID }}
{{- if $annotation := $f.Annotations.EntGQL }}
{{- if $annotation.OrderField }}
{{- if not $f.Type.Comparable }}
{{ fail (printf "annotated field %s.%s must be comparable" $node.Name $f.Name) }}
{{- end }}
{{ $orderFields = append $orderFields $f }}
{{- end }}
{{- end }}
{{- end }}
{{ $orderFields := orderFields $node }}

{{ $names := nodePaginationNames $node -}}
{{ $name := $names.Node -}}
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ require (

require (
ariga.io/atlas v0.3.8-0.20220314111236-b2171e04c5b2 // indirect
github.com/DATA-DOG/go-sqlmock v1.5.0 // indirect
github.com/agext/levenshtein v1.2.1 // indirect
github.com/agnivade/levenshtein v1.1.0 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/go-faster/errors v0.5.0 // indirect
github.com/go-faster/jx v0.25.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/goccy/go-yaml v1.9.4 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/addlicense v1.0.0 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
Expand Down
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ github.com/AlekSi/pointer v1.1.0 h1:SSDMPcXD9jSl8FPy9cRzoRaMJtm9g9ggGTxecRUbQoI=
github.com/AlekSi/pointer v1.1.0/go.mod h1:y7BvfRI3wXPWKXEBhU71nbnIEEZX0QTSB2Bj48UJIZE=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
Expand All @@ -29,6 +28,8 @@ github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
github.com/bmatcuk/doublestar/v4 v4.0.2 h1:X0krlUVAVmtr2cRoTqR8aDMrDqnB36ht8wpWTiQ3jsA=
github.com/bmatcuk/doublestar/v4 v4.0.2/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
Expand Down Expand Up @@ -73,8 +74,6 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/goccy/go-yaml v1.9.4 h1:S0GCYjwHKVI6IHqio7QWNKNThUl6NLzFd/g8Z65Axw8=
Expand All @@ -98,6 +97,8 @@ github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/addlicense v1.0.0 h1:cqvo5suPWlsk6r6o42Fs2K66xYCl2tnhVPUYoP3EnO4=
github.com/google/addlicense v1.0.0/go.mod h1:Sm/DHu7Jk+T5miFHHehdIjbi4M5+dJDRS3Cq0rncIxA=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
Expand Down