From 29cdb9bebd809cf36b7dd3d851f45c296626bdc8 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Fri, 27 Oct 2023 20:23:16 -0400 Subject: [PATCH 01/10] Add json tags --- entgql/template.go | 23 +++++++++++++++++++++++ entgql/template/mutation_input.tmpl | 16 ++++++++-------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/entgql/template.go b/entgql/template.go index d906c2cdc..00f58353b 100644 --- a/entgql/template.go +++ b/entgql/template.go @@ -25,6 +25,7 @@ import ( "strings" "text/template" "text/template/parse" + "unicode" "entgo.io/ent/entc/gen" "entgo.io/ent/schema/field" @@ -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, @@ -172,6 +174,27 @@ type fieldCollection struct { Mapping []string } +// decap takes a string and decapitalizes the first letter +// +// FullName => fullName +// ID => iD +// user_id => userID +// full-admin => fullAdmin +func decap(s string) string { + 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 { diff --git a/entgql/template/mutation_input.tmpl b/entgql/template/mutation_input.tmpl index 03ccc3656..68c7653d4 100644 --- a/entgql/template/mutation_input.tmpl +++ b/entgql/template/mutation_input.tmpl @@ -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 }}` + {{ $e.MutationRemove }} []{{ $e.Type.ID.Type }} `json:{{ $e.MutationRemove | decap }}` {{- end }} {{- end }} {{- end }} From 0a753919c19966c6b6627235ffe131f07994f441 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Fri, 27 Oct 2023 20:40:14 -0400 Subject: [PATCH 02/10] Fix comment --- entgql/template.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/entgql/template.go b/entgql/template.go index 00f58353b..5a18ef1d9 100644 --- a/entgql/template.go +++ b/entgql/template.go @@ -178,8 +178,6 @@ type fieldCollection struct { // // FullName => fullName // ID => iD -// user_id => userID -// full-admin => fullAdmin func decap(s string) string { var ( b strings.Builder From af70b55ba1fbca0cb002e2ba8b280d46b7a510d5 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Wed, 1 Nov 2023 23:21:59 -0400 Subject: [PATCH 03/10] Use functions for field/edge names --- entgql/schema.go | 50 ++++++++++++++++++++++++----- entgql/template.go | 13 +++++++- entgql/template/mutation_input.tmpl | 18 ++++++----- 3 files changed, 64 insertions(+), 17 deletions(-) diff --git a/entgql/schema.go b/entgql/schema.go index 39893888c..8dfa937a0 100644 --- a/entgql/schema.go +++ b/entgql/schema.go @@ -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 @@ -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), }) } diff --git a/entgql/template.go b/entgql/template.go index 5a18ef1d9..4f69566d3 100644 --- a/entgql/template.go +++ b/entgql/template.go @@ -78,7 +78,8 @@ var ( // TemplateFuncs contains the extra template functions used by entgql. TemplateFuncs = template.FuncMap{ - "decap": decap, + "fieldNamesForField": fieldNamesForField, + "fieldNamesForEdge": fieldNamesForEdge, "fieldCollections": fieldCollections, "fieldMapping": fieldMapping, "filterEdges": filterEdges, @@ -254,6 +255,14 @@ type InputFieldDescriptor struct { Nullable bool } +type OperationNames struct { + +} + +func (f *InputFieldDescriptor) GetOperationNames() OperationNames { + return f.Nullable +} + // IsPointer returns true if the Go type should be a pointer func (f *InputFieldDescriptor) IsPointer() bool { if f.Type.Nillable || f.Type.RType.IsPtr() { @@ -712,6 +721,8 @@ func nodePaginationNames(t *gen.Type) (*PaginationNames, error) { return paginationNames(node), nil } +func operationNames() + func paginationNames(node string) *PaginationNames { return &PaginationNames{ Connection: fmt.Sprintf("%sConnection", node), diff --git a/entgql/template/mutation_input.tmpl b/entgql/template/mutation_input.tmpl index 68c7653d4..7855eb967 100644 --- a/entgql/template/mutation_input.tmpl +++ b/entgql/template/mutation_input.tmpl @@ -29,28 +29,30 @@ import ( {{- end }} type {{ $input }} struct { {{- range $f := $fields }} + {{- $fieldNames := fieldNamesForField $f }} {{- if $f.ClearOp }} - {{ print "Clear" $f.StructField }} bool `json:clear{{ $f.Name | pascal }}` + {{ print "Clear" $f.StructField }} bool `json:{{ $fieldNames.Clear }}` {{- end }} - {{ $f.StructField }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }} `json:{{ $f.Name | camel }}` + {{ $f.StructField }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }} `json:{{ $fieldNames.Field }}` {{- if $f.AppendOp }} - {{ $f.MutationAppend }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }} `json:append{{ $f.Name | pascal }}` + {{ $f.MutationAppend }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }} `json:{{ $fieldNames.Append }}` {{- end }} {{- end }} {{- range $e := $edges }} + {{- $fieldNames := fieldNamesForEdge $e }} {{- if and (not $n.IsCreate) $e.Optional }} - {{ $e.MutationClear }} bool `json:clear{{ $e.Name | pascal }}` + {{ $e.MutationClear }} bool `json:{{ $fieldNames.Clear }}` {{- end }} {{- if $e.Unique }} {{- $structField := print (pascal $e.Name) "ID" }} - {{ $structField }} {{ if or (not $n.IsCreate) $e.Optional }}*{{ end }}{{ $e.Type.ID.Type }} `json:{{ $e.Name | camel }}ID` + {{ $structField }} {{ if or (not $n.IsCreate) $e.Optional }}*{{ end }}{{ $e.Type.ID.Type }} `json:{{ $fieldNames.Unique }}` {{- else }} {{- if $n.IsCreate }} {{- $structField := print (singular $e.Name | pascal) "IDs" }} - {{ $structField }} []{{ $e.Type.ID.Type }} `json:{{ singular $e.Name | camel }}IDs` + {{ $structField }} []{{ $e.Type.ID.Type }} `json:{{ $fieldNames.Create }}` {{- else }} - {{ $e.MutationAdd }} []{{ $e.Type.ID.Type }} `json:{{ $e.MutationAdd | decap }}` - {{ $e.MutationRemove }} []{{ $e.Type.ID.Type }} `json:{{ $e.MutationRemove | decap }}` + {{ $e.MutationAdd }} []{{ $e.Type.ID.Type }} `json:{{ $fieldNames.Add }}` + {{ $e.MutationRemove }} []{{ $e.Type.ID.Type }} `json:{{ $fieldNames.Remove }}` {{- end }} {{- end }} {{- end }} From 0114f600fcd3206c6795bee843b3f7c92a510be6 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Wed, 1 Nov 2023 23:22:13 -0400 Subject: [PATCH 04/10] Remove decap --- entgql/template.go | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/entgql/template.go b/entgql/template.go index 4f69566d3..9b67ac1df 100644 --- a/entgql/template.go +++ b/entgql/template.go @@ -25,7 +25,6 @@ import ( "strings" "text/template" "text/template/parse" - "unicode" "entgo.io/ent/entc/gen" "entgo.io/ent/schema/field" @@ -175,25 +174,6 @@ type fieldCollection struct { Mapping []string } -// decap takes a string and decapitalizes the first letter -// -// FullName => fullName -// ID => iD -func decap(s string) string { - 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 { From 51d17b676c66d65dcf529a67715187131524bcde Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Wed, 1 Nov 2023 23:44:03 -0400 Subject: [PATCH 05/10] Add configuration option --- entgql/extension.go | 17 +++++++++++++++++ entgql/schema.go | 1 + entgql/template/mutation_input.tmpl | 14 +++++++------- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/entgql/extension.go b/entgql/extension.go index 5d11b5331..66f281929 100644 --- a/entgql/extension.go +++ b/entgql/extension.go @@ -139,6 +139,17 @@ func WithWhereInputs(b bool) ExtensionOption { } } +// WithJsonTags configures the extension to either add or +// remove the `json:` tags from the code generation templates. +// +// The WhereTemplate generates GraphQL filters to all types in the ent/schema. +func WithJsonTags(b bool) ExtensionOption { + return func(ex *Extension) error { + ex.genJsonTags = b + return nil + } +} + // WithNodeDescriptor configures the extension to either add or // remove the NodeDescriptorTemplate from the code generation templates. // @@ -274,6 +285,12 @@ func (e *Extension) hasTemplate(tem *gen.Template) (int, bool) { return -1, false } +// shouldGenJsonTags reports if JSON tags have +// been configured for output. +func (e *Extension) shouldGenJsonTags() (bool) { + return e.genJsonTags +} + var ( _ entc.Extension = (*Extension)(nil) diff --git a/entgql/schema.go b/entgql/schema.go index 8dfa937a0..99a2f11aa 100644 --- a/entgql/schema.go +++ b/entgql/schema.go @@ -99,6 +99,7 @@ type schemaGenerator struct { genSchema bool genWhereInput bool genMutations bool + genJsonTags bool cfg *config.Config scalarFunc func(*gen.Field, gen.Op) string diff --git a/entgql/template/mutation_input.tmpl b/entgql/template/mutation_input.tmpl index 7855eb967..f4fdbc713 100644 --- a/entgql/template/mutation_input.tmpl +++ b/entgql/template/mutation_input.tmpl @@ -31,11 +31,11 @@ import ( {{- range $f := $fields }} {{- $fieldNames := fieldNamesForField $f }} {{- if $f.ClearOp }} - {{ print "Clear" $f.StructField }} bool `json:{{ $fieldNames.Clear }}` + {{ print "Clear" $f.StructField }} bool {{- if shouldGenJsonTags }} `json:{{ $fieldNames.Clear }}`{{ end }} {{- end }} - {{ $f.StructField }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }} `json:{{ $fieldNames.Field }}` + {{ $f.StructField }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }} {{- if shouldGenJsonTags }} `json:{{ $fieldNames.Field }}`{{ end }} {{- if $f.AppendOp }} - {{ $f.MutationAppend }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }} `json:{{ $fieldNames.Append }}` + {{ $f.MutationAppend }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }} {{- if shouldGenJsonTags }} `json:{{ $fieldNames.Append }}`{{ end }} {{- end }} {{- end }} {{- range $e := $edges }} @@ -45,14 +45,14 @@ import ( {{- end }} {{- if $e.Unique }} {{- $structField := print (pascal $e.Name) "ID" }} - {{ $structField }} {{ if or (not $n.IsCreate) $e.Optional }}*{{ end }}{{ $e.Type.ID.Type }} `json:{{ $fieldNames.Unique }}` + {{ $structField }} {{ if or (not $n.IsCreate) $e.Optional }}*{{ end }}{{ $e.Type.ID.Type }} {{- if shouldGenJsonTags }} `json:{{ $fieldNames.Unique }}`{{ end }} {{- else }} {{- if $n.IsCreate }} {{- $structField := print (singular $e.Name | pascal) "IDs" }} - {{ $structField }} []{{ $e.Type.ID.Type }} `json:{{ $fieldNames.Create }}` + {{ $structField }} []{{ $e.Type.ID.Type }} {{- if shouldGenJsonTags }} `json:{{ $fieldNames.Create }}`{{ end }} {{- else }} - {{ $e.MutationAdd }} []{{ $e.Type.ID.Type }} `json:{{ $fieldNames.Add }}` - {{ $e.MutationRemove }} []{{ $e.Type.ID.Type }} `json:{{ $fieldNames.Remove }}` + {{ $e.MutationAdd }} []{{ $e.Type.ID.Type }} {{- if shouldGenJsonTags }} `json:{{ $fieldNames.Add }}`{{ end }} + {{ $e.MutationRemove }} []{{ $e.Type.ID.Type }} {{- if shouldGenJsonTags }} `json:{{ $fieldNames.Remove }}`{{ end }} {{- end }} {{- end }} {{- end }} From c5b340f7b02276dbbc66c15aae6b6ef2cb884eb9 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Wed, 1 Nov 2023 23:44:54 -0400 Subject: [PATCH 06/10] Remove cruft --- entgql/template.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/entgql/template.go b/entgql/template.go index 9b67ac1df..968b4047e 100644 --- a/entgql/template.go +++ b/entgql/template.go @@ -235,14 +235,6 @@ type InputFieldDescriptor struct { Nullable bool } -type OperationNames struct { - -} - -func (f *InputFieldDescriptor) GetOperationNames() OperationNames { - return f.Nullable -} - // IsPointer returns true if the Go type should be a pointer func (f *InputFieldDescriptor) IsPointer() bool { if f.Type.Nillable || f.Type.RType.IsPtr() { @@ -701,8 +693,6 @@ func nodePaginationNames(t *gen.Type) (*PaginationNames, error) { return paginationNames(node), nil } -func operationNames() - func paginationNames(node string) *PaginationNames { return &PaginationNames{ Connection: fmt.Sprintf("%sConnection", node), From ac85432f6bcc22908c54d193cc3316532fc79807 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Thu, 2 Nov 2023 00:13:28 -0400 Subject: [PATCH 07/10] Correct usage of feature flag --- entgql/template/mutation_input.tmpl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/entgql/template/mutation_input.tmpl b/entgql/template/mutation_input.tmpl index f4fdbc713..b5faac897 100644 --- a/entgql/template/mutation_input.tmpl +++ b/entgql/template/mutation_input.tmpl @@ -31,28 +31,28 @@ import ( {{- range $f := $fields }} {{- $fieldNames := fieldNamesForField $f }} {{- if $f.ClearOp }} - {{ print "Clear" $f.StructField }} bool {{- if shouldGenJsonTags }} `json:{{ $fieldNames.Clear }}`{{ end }} + {{ print "Clear" $f.StructField }} bool {{- if $.FeatureEnabled "jsontags" }} `json:{{ $fieldNames.Clear }}`{{ end }} {{- end }} - {{ $f.StructField }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }} {{- if shouldGenJsonTags }} `json:{{ $fieldNames.Field }}`{{ end }} + {{ $f.StructField }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }} {{- if $.FeatureEnabled "jsontags" }} `json:{{ $fieldNames.Field }}`{{ end }} {{- if $f.AppendOp }} - {{ $f.MutationAppend }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }} {{- if shouldGenJsonTags }} `json:{{ $fieldNames.Append }}`{{ end }} + {{ $f.MutationAppend }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }} {{- if $.FeatureEnabled "jsontags" }} `json:{{ $fieldNames.Append }}`{{ end }} {{- end }} {{- end }} {{- range $e := $edges }} {{- $fieldNames := fieldNamesForEdge $e }} {{- if and (not $n.IsCreate) $e.Optional }} - {{ $e.MutationClear }} bool `json:{{ $fieldNames.Clear }}` + {{ $e.MutationClear }} bool {{- if $.FeatureEnabled "jsontags" }} `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 }} {{- if shouldGenJsonTags }} `json:{{ $fieldNames.Unique }}`{{ end }} + {{ $structField }} {{ if or (not $n.IsCreate) $e.Optional }}*{{ end }}{{ $e.Type.ID.Type }} {{- if $.FeatureEnabled "jsontags" }} `json:{{ $fieldNames.Unique }}`{{ end }} {{- else }} {{- if $n.IsCreate }} {{- $structField := print (singular $e.Name | pascal) "IDs" }} - {{ $structField }} []{{ $e.Type.ID.Type }} {{- if shouldGenJsonTags }} `json:{{ $fieldNames.Create }}`{{ end }} + {{ $structField }} []{{ $e.Type.ID.Type }} {{- if $.FeatureEnabled "jsontags" }} `json:{{ $fieldNames.Create }}`{{ end }} {{- else }} - {{ $e.MutationAdd }} []{{ $e.Type.ID.Type }} {{- if shouldGenJsonTags }} `json:{{ $fieldNames.Add }}`{{ end }} - {{ $e.MutationRemove }} []{{ $e.Type.ID.Type }} {{- if shouldGenJsonTags }} `json:{{ $fieldNames.Remove }}`{{ end }} + {{ $e.MutationAdd }} []{{ $e.Type.ID.Type }} {{- if $.FeatureEnabled "jsontags" }} `json:{{ $fieldNames.Add }}`{{ end }} + {{ $e.MutationRemove }} []{{ $e.Type.ID.Type }} {{- if $.FeatureEnabled "jsontags" }} `json:{{ $fieldNames.Remove }}`{{ end }} {{- end }} {{- end }} {{- end }} From ab8f68f72c69ea8335500fea5d1cbe89460a9228 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Thu, 2 Nov 2023 00:16:17 -0400 Subject: [PATCH 08/10] Cleanup --- entgql/template/mutation_input.tmpl | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/entgql/template/mutation_input.tmpl b/entgql/template/mutation_input.tmpl index b5faac897..2ec7d6097 100644 --- a/entgql/template/mutation_input.tmpl +++ b/entgql/template/mutation_input.tmpl @@ -2,6 +2,7 @@ {{- /*gotype: entgo.io/ent/entc/gen.Graph*/ -}} +{{ $hasJson := $.FeatureEnabled "jsontags" }} {{ $pkg := base $.Config.Package }} {{- with extend $ "Package" $pkg }} {{ template "header" . }} @@ -31,28 +32,28 @@ import ( {{- range $f := $fields }} {{- $fieldNames := fieldNamesForField $f }} {{- if $f.ClearOp }} - {{ print "Clear" $f.StructField }} bool {{- if $.FeatureEnabled "jsontags" }} `json:{{ $fieldNames.Clear }}`{{ end }} + {{ print "Clear" $f.StructField }} bool {{- if $hasJson }} `json:{{ $fieldNames.Clear }}`{{ end }} {{- end }} - {{ $f.StructField }} {{ if $f.IsPointer }}*{{ end }}{{ $f.Type }} {{- if $.FeatureEnabled "jsontags" }} `json:{{ $fieldNames.Field }}`{{ end }} + {{ $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 }} {{- if $.FeatureEnabled "jsontags" }} `json:{{ $fieldNames.Append }}`{{ end }} + {{ $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 {{- if $.FeatureEnabled "jsontags" }} `json:{{ $fieldNames.Clear }}`{{ end }} + {{ $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 }} {{- if $.FeatureEnabled "jsontags" }} `json:{{ $fieldNames.Unique }}`{{ end }} + {{ $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 }} {{- if $.FeatureEnabled "jsontags" }} `json:{{ $fieldNames.Create }}`{{ end }} + {{ $structField }} []{{ $e.Type.ID.Type }} {{- if $hasJson }} `json:{{ $fieldNames.Create }}`{{ end }} {{- else }} - {{ $e.MutationAdd }} []{{ $e.Type.ID.Type }} {{- if $.FeatureEnabled "jsontags" }} `json:{{ $fieldNames.Add }}`{{ end }} - {{ $e.MutationRemove }} []{{ $e.Type.ID.Type }} {{- if $.FeatureEnabled "jsontags" }} `json:{{ $fieldNames.Remove }}`{{ end }} + {{ $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 }} From d07ae5e6548eccc188c8d03c8a52a627b82d187c Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Thu, 2 Nov 2023 00:24:03 -0400 Subject: [PATCH 09/10] Remove cruft --- entgql/extension.go | 17 ----------------- entgql/schema.go | 21 ++++++++++----------- 2 files changed, 10 insertions(+), 28 deletions(-) diff --git a/entgql/extension.go b/entgql/extension.go index 66f281929..5d11b5331 100644 --- a/entgql/extension.go +++ b/entgql/extension.go @@ -139,17 +139,6 @@ func WithWhereInputs(b bool) ExtensionOption { } } -// WithJsonTags configures the extension to either add or -// remove the `json:` tags from the code generation templates. -// -// The WhereTemplate generates GraphQL filters to all types in the ent/schema. -func WithJsonTags(b bool) ExtensionOption { - return func(ex *Extension) error { - ex.genJsonTags = b - return nil - } -} - // WithNodeDescriptor configures the extension to either add or // remove the NodeDescriptorTemplate from the code generation templates. // @@ -285,12 +274,6 @@ func (e *Extension) hasTemplate(tem *gen.Template) (int, bool) { return -1, false } -// shouldGenJsonTags reports if JSON tags have -// been configured for output. -func (e *Extension) shouldGenJsonTags() (bool) { - return e.genJsonTags -} - var ( _ entc.Extension = (*Extension)(nil) diff --git a/entgql/schema.go b/entgql/schema.go index 99a2f11aa..da41f2a56 100644 --- a/entgql/schema.go +++ b/entgql/schema.go @@ -99,7 +99,6 @@ type schemaGenerator struct { genSchema bool genWhereInput bool genMutations bool - genJsonTags bool cfg *config.Config scalarFunc func(*gen.Field, gen.Op) string @@ -536,34 +535,34 @@ func (e *schemaGenerator) buildWhereInput(t *gen.Type, nodeGQLType, gqlType stri } type FieldFieldNames struct { - Field string + Field string Append string - Clear string + Clear string } func fieldNamesForField(f *InputFieldDescriptor) FieldFieldNames { - return FieldFieldNames { - Field: camel(f.Name), + return FieldFieldNames{ + Field: camel(f.Name), Append: "append" + f.StructField(), - Clear: "clear" + f.StructField() , + Clear: "clear" + f.StructField(), } } type EdgeFieldNames struct { - Clear string - Add string + Clear string + Add string Remove string Create string Unique string } func fieldNamesForEdge(e *gen.Edge) EdgeFieldNames { - return EdgeFieldNames { + return EdgeFieldNames{ Unique: camel(e.Name) + "ID", Create: camel(singular(e.Name)) + "IDs", - Add: "add" + pascal(singular(e.Name)) + "IDs", + Add: "add" + pascal(singular(e.Name)) + "IDs", Remove: "remove" + pascal(singular(e.Name)) + "IDs", - Clear: camel(snake(e.MutationClear())), + Clear: camel(snake(e.MutationClear())), } } From 5e2ea4377a7b0ec36d942966859e9c27a385d0c6 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Thu, 2 Nov 2023 00:24:15 -0400 Subject: [PATCH 10/10] go fmt --- entgql/template.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/entgql/template.go b/entgql/template.go index 968b4047e..7217a67a5 100644 --- a/entgql/template.go +++ b/entgql/template.go @@ -77,8 +77,8 @@ var ( // TemplateFuncs contains the extra template functions used by entgql. TemplateFuncs = template.FuncMap{ - "fieldNamesForField": fieldNamesForField, - "fieldNamesForEdge": fieldNamesForEdge, + "fieldNamesForField": fieldNamesForField, + "fieldNamesForEdge": fieldNamesForEdge, "fieldCollections": fieldCollections, "fieldMapping": fieldMapping, "filterEdges": filterEdges,