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

Add json tags to gql_mutation_input #551

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
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
21 changes: 21 additions & 0 deletions entgql/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strings"
"text/template"
"text/template/parse"
"unicode"

"entgo.io/ent/entc/gen"
"entgo.io/ent/schema/field"
Expand Down Expand Up @@ -77,6 +78,7 @@ var (

// TemplateFuncs contains the extra template functions used by entgql.
TemplateFuncs = template.FuncMap{
"decap": decap,
"fieldCollections": fieldCollections,
"fieldMapping": fieldMapping,
"filterEdges": filterEdges,
Expand Down Expand Up @@ -172,6 +174,25 @@ type fieldCollection struct {
Mapping []string
}

// decap takes a string and decapitalizes the first letter
//
// FullName => fullName
// ID => iD
func decap(s string) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generated names are available here: https://github.com/ent/contrib/blob/master/entgql/schema.go#L578-L594. We should bind them to the template data when generating these types (or the WhereInput) either statically or by template function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I will update.

var (
b strings.Builder
)
for i := 0; i < len(s); i++ {
r := rune(s[i])
if i == 0 {
b.WriteRune(unicode.ToLower(r))
} else {
b.WriteRune(r)
}
}
return b.String()
}

func fieldCollections(edges []*gen.Edge) ([]*fieldCollection, error) {
collect := make([]*fieldCollection, 0, len(edges))
for _, e := range edges {
Expand Down
16 changes: 8 additions & 8 deletions entgql/template/mutation_input.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@ import (
type {{ $input }} struct {
{{- range $f := $fields }}
{{- if $f.ClearOp }}
{{ print "Clear" $f.StructField }} bool
{{ print "Clear" $f.StructField }} bool `json:clear{{ $f.Name | pascal }}`
{{- end }}
{{ $f.StructField }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }}
{{ $f.StructField }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }} `json:{{ $f.Name | camel }}`
{{- if $f.AppendOp }}
{{ $f.MutationAppend }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }}
{{ $f.MutationAppend }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }} `json:append{{ $f.Name | pascal }}`
{{- end }}
{{- end }}
{{- range $e := $edges }}
{{- if and (not $n.IsCreate) $e.Optional }}
{{ $e.MutationClear }} bool
{{ $e.MutationClear }} bool `json:clear{{ $e.Name | pascal }}`
{{- end }}
{{- if $e.Unique }}
{{- $structField := print (pascal $e.Name) "ID" }}
{{ $structField }} {{ if or (not $n.IsCreate) $e.Optional }}*{{ end }}{{ $e.Type.ID.Type }}
{{ $structField }} {{ if or (not $n.IsCreate) $e.Optional }}*{{ end }}{{ $e.Type.ID.Type }} `json:{{ $e.Name | camel }}ID`
{{- else }}
{{- if $n.IsCreate }}
{{- $structField := print (singular $e.Name | pascal) "IDs" }}
{{ $structField }} []{{ $e.Type.ID.Type }}
{{ $structField }} []{{ $e.Type.ID.Type }} `json:{{ singular $e.Name | camel }}IDs`
{{- else }}
{{ $e.MutationAdd }} []{{ $e.Type.ID.Type }}
{{ $e.MutationRemove }} []{{ $e.Type.ID.Type }}
{{ $e.MutationAdd }} []{{ $e.Type.ID.Type }} `json:{{ $e.MutationAdd | decap }}`
masseelch marked this conversation as resolved.
Show resolved Hide resolved
{{ $e.MutationRemove }} []{{ $e.Type.ID.Type }} `json:{{ $e.MutationRemove | decap }}`
{{- end }}
{{- end }}
{{- end }}
Expand Down