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 all 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
50 changes: 42 additions & 8 deletions entgql/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,38 @@ func (e *schemaGenerator) buildWhereInput(t *gen.Type, nodeGQLType, gqlType stri
return def, nil
}

type FieldFieldNames struct {
Field string
Append string
Clear string
}

func fieldNamesForField(f *InputFieldDescriptor) FieldFieldNames {
return FieldFieldNames{
Field: camel(f.Name),
Append: "append" + f.StructField(),
Clear: "clear" + f.StructField(),
}
}

type EdgeFieldNames struct {
Clear string
Add string
Remove string
Create string
Unique string
}

func fieldNamesForEdge(e *gen.Edge) EdgeFieldNames {
return EdgeFieldNames{
Unique: camel(e.Name) + "ID",
Create: camel(singular(e.Name)) + "IDs",
Add: "add" + pascal(singular(e.Name)) + "IDs",
Remove: "remove" + pascal(singular(e.Name)) + "IDs",
Clear: camel(snake(e.MutationClear())),
}
}

func (e *schemaGenerator) buildMutationInputs(t *gen.Type, ant *Annotation, gqlType string) ([]*ast.Definition, error) {
var defs []*ast.Definition

Expand Down Expand Up @@ -575,49 +607,51 @@ func (e *schemaGenerator) buildMutationInputs(t *gen.Type, ant *Annotation, gqlT
if scalar == "" {
return nil, fmt.Errorf("%s is not supported as input for %s", f.Name, def.Name)
}
fieldNames := fieldNamesForField(f)
def.Fields = append(def.Fields, &ast.FieldDefinition{
Name: camel(f.Name),
Name: fieldNames.Field,
Type: namedType(scalar, f.Nullable),
Description: f.Comment(),
})
if f.AppendOp {
def.Fields = append(def.Fields, &ast.FieldDefinition{
Name: "append" + f.StructField(),
Name: fieldNames.Append,
Type: namedType(scalar, true),
})
}
if f.ClearOp {
def.Fields = append(def.Fields, &ast.FieldDefinition{
Name: "clear" + f.StructField(),
Name: fieldNames.Clear,
Type: namedType("Boolean", true),
})
}
}

for _, e := range edges {
fieldNames := fieldNamesForEdge(e)
switch {
case e.Unique:
def.Fields = append(def.Fields, &ast.FieldDefinition{
Name: camel(e.Name) + "ID",
Name: fieldNames.Unique,
Type: namedType("ID", !i.IsCreate || e.Optional),
})
case i.IsCreate:
def.Fields = append(def.Fields, &ast.FieldDefinition{
Name: camel(singular(e.Name)) + "IDs",
Name: fieldNames.Create,
Type: namedType("[ID!]", e.Optional),
})
default:
def.Fields = append(def.Fields, &ast.FieldDefinition{
Name: "add" + pascal(singular(e.Name)) + "IDs",
Name: fieldNames.Add,
Type: namedType("[ID!]", true),
}, &ast.FieldDefinition{
Name: "remove" + pascal(singular(e.Name)) + "IDs",
Name: fieldNames.Remove,
Type: namedType("[ID!]", true),
})
}
if !i.IsCreate && e.Optional {
def.Fields = append(def.Fields, &ast.FieldDefinition{
Name: camel(snake(e.MutationClear())),
Name: fieldNames.Clear,
Type: namedType("Boolean", true),
})
}
Expand Down
2 changes: 2 additions & 0 deletions entgql/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ var (

// TemplateFuncs contains the extra template functions used by entgql.
TemplateFuncs = template.FuncMap{
"fieldNamesForField": fieldNamesForField,
"fieldNamesForEdge": fieldNamesForEdge,
"fieldCollections": fieldCollections,
"fieldMapping": fieldMapping,
"filterEdges": filterEdges,
Expand Down
19 changes: 11 additions & 8 deletions entgql/template/mutation_input.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

{{- /*gotype: entgo.io/ent/entc/gen.Graph*/ -}}

{{ $hasJson := $.FeatureEnabled "jsontags" }}
{{ $pkg := base $.Config.Package }}
{{- with extend $ "Package" $pkg }}
{{ template "header" . }}
Expand Down Expand Up @@ -29,28 +30,30 @@ import (
{{- end }}
type {{ $input }} struct {
{{- range $f := $fields }}
{{- $fieldNames := fieldNamesForField $f }}
{{- if $f.ClearOp }}
{{ print "Clear" $f.StructField }} bool
{{ print "Clear" $f.StructField }} bool {{- if $hasJson }} `json:{{ $fieldNames.Clear }}`{{ end }}
{{- end }}
{{ $f.StructField }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }}
{{ $f.StructField }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }} {{- if $hasJson }} `json:{{ $fieldNames.Field }}`{{ end }}
{{- if $f.AppendOp }}
{{ $f.MutationAppend }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }}
{{ $f.MutationAppend }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }} {{- if $hasJson }} `json:{{ $fieldNames.Append }}`{{ end }}
{{- end }}
{{- end }}
{{- range $e := $edges }}
{{- $fieldNames := fieldNamesForEdge $e }}
{{- if and (not $n.IsCreate) $e.Optional }}
{{ $e.MutationClear }} bool
{{ $e.MutationClear }} bool {{- if $hasJson }} `json:{{ $fieldNames.Clear }}`{{ end }}
{{- 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 }} {{- if $hasJson }} `json:{{ $fieldNames.Unique }}`{{ end }}
{{- else }}
{{- if $n.IsCreate }}
{{- $structField := print (singular $e.Name | pascal) "IDs" }}
{{ $structField }} []{{ $e.Type.ID.Type }}
{{ $structField }} []{{ $e.Type.ID.Type }} {{- if $hasJson }} `json:{{ $fieldNames.Create }}`{{ end }}
{{- else }}
{{ $e.MutationAdd }} []{{ $e.Type.ID.Type }}
{{ $e.MutationRemove }} []{{ $e.Type.ID.Type }}
{{ $e.MutationAdd }} []{{ $e.Type.ID.Type }} {{- if $hasJson }} `json:{{ $fieldNames.Add }}`{{ end }}
{{ $e.MutationRemove }} []{{ $e.Type.ID.Type }} {{- if $hasJson }} `json:{{ $fieldNames.Remove }}`{{ end }}
{{- end }}
{{- end }}
{{- end }}
Expand Down