forked from ent/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedge.tmpl
More file actions
104 lines (94 loc) · 3.74 KB
/
edge.tmpl
File metadata and controls
104 lines (94 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
{{/*
Copyright 2019-present Facebook Inc. All rights reserved.
This source code is licensed under the Apache 2.0 license found
in the LICENSE file in the root directory of this source tree.
*/}}
{{/* gotype: entgo.io/ent/entc/gen.Graph */}}
{{ define "gql_edge" }}
{{ template "header" $ }}
{{ template "import" $ }}
import (
"context"
"entgo.io/contrib/entgql"
)
{{ range $n := filterNodes $.Nodes (skipMode "type") }}
{{ $r := $n.Receiver }}
{{ range $i, $e := filterEdges $n.Edges (skipMode "type") }}
{{ if isRelayConn $e }}
{{ with extend $n "Node" $n "Edge" $e "Index" $i }}
{{ template "gql_edge/helper/paginate" . }}
{{ end }}
{{ else if not $e.Unique }}
func ({{ $r }} *{{ $n.Name }}) {{ $e.StructField }}(ctx context.Context) (result []*{{ $e.Type.Name }}, err error) {
if fc := graphql.GetFieldContext(ctx); fc != nil && fc.Field.Alias != "" {
result, err = {{ $r }}.Named{{ $e.StructField }}(graphql.GetFieldContext(ctx).Field.Alias)
} else {
{{- /* For regular edges (not Relay connections), we fallback to .Edges field in case the context is not GraphQL */}}
result, err = {{ $r }}.Edges.{{ $e.StructField }}OrErr()
}
if IsNotLoaded(err) {
result, err = {{ $r }}.Query{{ $e.StructField }}().All(ctx)
}
return result, err
}
{{ else }}
func ({{ $r }} *{{ $n.Name }}) {{ $e.StructField }}(ctx context.Context) (*{{ $e.Type.Name }}, error) {
result, err := {{ $r }}.Edges.{{ $e.StructField }}OrErr()
if IsNotLoaded(err) {
result, err = {{ $r }}.Query{{ $e.StructField }}().Only(ctx)
}
return result, {{ if $e.Optional }}MaskNotFound(err){{ else }}err{{ end }}
}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{ define "gql_edge/helper/paginate" }}
{{ $n := $.Scope.Node }}
{{ $e := $.Scope.Edge }}
{{ $i := $.Scope.Index }}
{{ $names := nodePaginationNames $e.Type }}
{{ $order := $names.Order }}
{{ $multiOrder := $e.Type.Annotations.EntGQL.MultiOrder }}
{{ $whereInput := $names.WhereInput }}
{{- $edge := $names.Edge }}
{{ $conn := $names.Connection }}
{{ $opt := print $names.Node "PaginateOption" }}
{{ $r := $n.Receiver }}
{{ $newPager := print "new" $names.Node "Pager" }}
func ({{ $r }} *{{ $n.Name }}) {{ $e.StructField }}(
ctx context.Context, after *Cursor, first *int, before *Cursor, last *int,
{{- if orderFields $e.Type }}orderBy {{ if $multiOrder }}[]{{ end }}*{{ $order }},{{ end }}
{{- if and (hasTemplate "gql_where_input") (hasWhereInput $e) }}where *{{ $whereInput }},{{ end }}
) (*{{ $conn }}, error) {
opts := []{{ $opt }}{
{{- if orderFields $e.Type }}
{{ print "With" $order }}(orderBy),
{{- end }}
{{- if and (hasTemplate "gql_where_input") (hasWhereInput $e) }}
{{ print "With" $names.Node "Filter" }}(where.Filter),
{{- end }}
}
{{- /* May be nil if the totalCount was not loaded. */}}
alias := graphql.GetFieldContext(ctx).Field.Alias
totalCount, hasTotalCount := {{ $r }}.Edges.totalCount[{{ $i }}][alias]
{{- /* Nodes were loaded, totalCount was loaded, or both. */}}
if nodes, err := {{ $r }}.Named{{ $e.StructField }}(alias); err == nil || hasTotalCount {
pager, err := {{ $newPager }}(opts, last != nil)
if err != nil {
return nil, err
}
{{- /* Ensure the "edges" field is marshaled as "[]" in case it is empty. */}}
conn := &{{ $conn }}{Edges: []*{{ $edge }}{}, TotalCount: totalCount}
conn.build(nodes, pager, after, first, before, last)
return conn, nil
}
return {{ $r }}.Query{{ $e.StructField }}().Paginate(ctx, after, first, before, last, opts...)
}
{{ end }}
{{ define "dialect/sql/model/edges/fields/additional/load_total" }}
{{- with filterEdges $.Edges (skipMode "type") }}
// totalCount holds the count of the edges above.
totalCount [{{ len . }}]map[string]int
{{- end }}
{{ end }}