From 8c6b723af462745b2decf30ad253328f67f0deb9 Mon Sep 17 00:00:00 2001 From: Richard Lindhout Date: Thu, 7 May 2020 23:36:33 +0200 Subject: [PATCH] Fixes --- convert.go | 67 +++++++++++++++++++++++++++------------------ convert.gotpl | 6 ++-- convert_input.gotpl | 4 +-- filter.gotpl | 2 +- resolver.gotpl | 37 ++++++++++++++++++++++++- 5 files changed, 83 insertions(+), 33 deletions(-) diff --git a/convert.go b/convert.go index a912c38..2fbdd37 100755 --- a/convert.go +++ b/convert.go @@ -64,6 +64,7 @@ type Model struct { HasOrganizationID bool HasUserOrganizationID bool HasUserID bool + HasStringPrimaryID bool // other stuff Description string PureFields []*ast.FieldDefinition @@ -77,14 +78,14 @@ type ColumnSetting struct { } type Field struct { - Name string - PluralName string - Type string - IsID bool - IsPrimaryID bool - IsRequired bool - IsPlural bool - ConvertConfig ConvertConfig + Name string + PluralName string + Type string + IsNumberID bool + IsPrimaryNumberID bool + IsRequired bool + IsPlural bool + ConvertConfig ConvertConfig // relation stuff IsRelation bool // boiler relation stuff is inside this field @@ -362,14 +363,27 @@ func enhanceModelsWithFields(enums []*Enum, schema *ast.Schema, cfg *config.Conf // generate some booleans because these checks will be used a lot isRelation := fieldDef.Kind == ast.Object || fieldDef.Kind == ast.InputObject - isID := strings.Contains(golangName, "ID") + shortType := getShortType(typ.String()) + isString := strings.Contains(strings.ToLower(shortType), "string") + isPrimaryID := golangName == "ID" + isNumberID := strings.Contains(golangName, "ID") && !isString + isPrimaryNumberID := isPrimaryID && !isString + isPrimaryStringID := isPrimaryID && isString + // enable simpler code in resolvers + + if isPrimaryStringID { + fmt.Println("marked", m.Name, " as string id's") + m.HasStringPrimaryID = isPrimaryStringID + } + // get sqlboiler information of the field boilerField := findBoilerFieldOrForeignKey(m.BoilerModel.Fields, golangName, isRelation) - if isPrimaryID { + if isPrimaryNumberID || isPrimaryStringID { m.PrimaryKeyType = boilerField.Type } + // log some warnings when fields could not be converted if boilerField.Type == "" { // TODO: add filter + where here @@ -394,19 +408,19 @@ func enhanceModelsWithFields(enums []*Enum, schema *ast.Schema, cfg *config.Conf } field := &Field{ - Name: name, - Type: getShortType(typ.String()), - BoilerField: boilerField, - IsID: isID, - IsPrimaryID: isPrimaryID, - IsRelation: isRelation, - IsOr: name == "or", - IsAnd: name == "and", - IsPlural: pluralizer.IsPlural(name), - PluralName: pluralizer.Plural(name), - OriginalType: typ, - Description: field.Description, - Tag: `json:"` + field.Name + `"`, + Name: name, + Type: shortType, + BoilerField: boilerField, + IsNumberID: isNumberID, + IsPrimaryNumberID: isPrimaryNumberID, + IsRelation: isRelation, + IsOr: name == "or", + IsAnd: name == "and", + IsPlural: pluralizer.IsPlural(name), + PluralName: pluralizer.Plural(name), + OriginalType: typ, + Description: field.Description, + Tag: `json:"` + field.Name + `"`, } field.ConvertConfig = getConvertConfig(enums, m, field) m.Fields = append(m.Fields, field) @@ -775,7 +789,7 @@ func getConvertConfig(enums []*Enum, model *Model, field *Field) (cc ConvertConf } else if graphType != boilType { cc.IsCustom = true - if field.IsPrimaryID || field.IsID { + if field.IsPrimaryNumberID || field.IsNumberID { cc.ToGraphQL = "VALUE" cc.ToBoiler = "VALUE" @@ -792,9 +806,9 @@ func getConvertConfig(enums []*Enum, model *Model, field *Field) (cc ConvertConf cc.ToGraphQL = "boilergql." + goToUint + "(VALUE)" } - if field.IsPrimaryID { + if field.IsPrimaryNumberID { cc.ToGraphQL = model.Name + "IDToGraphQL(" + cc.ToGraphQL + ")" - } else if field.IsID { + } else if field.IsNumberID { cc.ToGraphQL = field.BoilerField.Relationship.Name + "IDToGraphQL(" + cc.ToGraphQL + ")" } @@ -805,6 +819,7 @@ func getConvertConfig(enums []*Enum, model *Model, field *Field) (cc ConvertConf if isInt { cc.ToBoiler = fmt.Sprintf("boilergql.NullUintToNullInt(%v)", cc.ToBoiler) } + } else { cc.ToBoiler = fmt.Sprintf("boilergql.IDToBoiler(%v)", cc.ToBoiler) if isInt { diff --git a/convert.gotpl b/convert.gotpl index 7b1e07c..b9817d4 100755 --- a/convert.gotpl +++ b/convert.gotpl @@ -120,7 +120,7 @@ } {{ range $field := .Fields }} - {{- if $field.IsPrimaryID -}} + {{- if $field.IsPrimaryNumberID -}} func {{ $model.Name|go }}IDToGraphQL(v uint) string { return boilergql.IDToGraphQL(v, models.TableNames.{{ $model.Name|go }}) } @@ -138,7 +138,7 @@ {{- with .Description }} {{.|prefixLines "// "}} {{- end}} - {{- if $field.IsID -}} + {{- if $field.IsNumberID -}} {{ $field.Name|go }}: {{ $field.ConvertConfig.ToGraphQL }}, {{- else if $field.IsRelation -}} {{- else if $field.ConvertConfig.IsCustom }} @@ -173,7 +173,7 @@ } {{ range $field := .Fields }} - {{- if $field.IsPrimaryID }} + {{- if $field.IsPrimaryNumberID }} func {{ $model.Name|go }}ID(v string) {{ $field.BoilerField.Type }} { return boilergql.IDToBoiler{{ $field.BoilerField.Type|go }}(v) } diff --git a/convert_input.gotpl b/convert_input.gotpl index 0ab4468..268852f 100644 --- a/convert_input.gotpl +++ b/convert_input.gotpl @@ -50,7 +50,7 @@ } r := &models.{{ .BoilerModel.Name|go }}{ {{ range $field := .Fields -}} - {{ if $field.IsID -}} + {{ if $field.IsNumberID -}} {{- $field.BoilerField.Name|go }} : {{ $field.ConvertConfig.ToBoiler }}, {{ else if $field.IsRelation -}} {{ else if $field.ConvertConfig.IsCustom -}} @@ -72,7 +72,7 @@ switch key { {{ range $field := .Fields -}} case "{{ $field.Name }}": - {{ if $field.IsID -}} + {{ if $field.IsNumberID -}} modelM[models.{{ $model.BoilerModel.Name|go }}Columns.{{- $field.BoilerField.Name|go }}] = {{ $field.ConvertConfig.ToBoiler }} {{ else if $field.IsRelation -}} {{ else if $field.ConvertConfig.IsCustom -}} diff --git a/filter.gotpl b/filter.gotpl index 03c61bf..598134e 100644 --- a/filter.gotpl +++ b/filter.gotpl @@ -249,7 +249,7 @@ func IntFilterToMods(m *graphql_models.IntFilter, column string) []qm.QueryMod { queryMods = append(queryMods, qm.Expr({{ $field.Type|go }}ToMods(m.And, true)...)) } {{- else }} - {{- if $field.IsPrimaryID }} + {{- if $field.IsPrimaryNumberID }} if withPrimaryID { queryMods = append(queryMods, {{ $field.Type|go }}ToMods(m.{{ $field.Name|go }}, models.{{ $model.BoilerModel.Name }}Columns.{{ $field.BoilerField.Name }})...) } diff --git a/resolver.gotpl b/resolver.gotpl index 0c80a2c..cf8d651 100644 --- a/resolver.gotpl +++ b/resolver.gotpl @@ -39,7 +39,13 @@ const inputKey = "input" {{- if .IsSingle }} + + {{- if .Model.HasStringPrimaryID }} + dbID := id + {{- else }} dbID := {{ .Model.Name }}ID(id) + {{- end }} + mods := boilergql.GetPreloadMods(ctx, {{ .Model.Name|go }}PreloadMap) mods = append(mods, dm.{{ .Model.Name|go }}Where.ID.EQ(dbID)) {{- if .Model.BoilerModel.HasOrganizationID }} @@ -198,7 +204,13 @@ const inputKey = "input" {{ range $field := .InputModel.Fields -}} {{ if $field.IsRelation -}} if input.{{ $field.Name|go }} != nil && input.{{ $field.Name|go }}ID != nil { + + {{- if $model.HasStringPrimaryID }} + dbID := *input.{{ $field.Name|go }}ID + {{- else }} dbID := {{ $field.BoilerField.Relationship.Name }}ID(*input.{{ $field.Name|go }}ID) + {{- end }} + nestedM := {{ $field.BoilerField.Relationship.Name }}UpdateInputToModelM( boilergql.GetInputFromContext(ctx, "input.{{ $field.Name }}"), *input.{{ $field.Name|go }}, @@ -229,7 +241,13 @@ const inputKey = "input" {{ end -}} {{ end -}} + {{- if .Model.HasStringPrimaryID }} + dbID := id + {{- else }} dbID := {{ .Model.Name }}ID(id) + {{- end }} + + if _, err := dm.{{ .Model.PluralName }}( dm.{{ .Model.Name }}Where.ID.EQ(dbID), {{- if .Model.BoilerModel.HasOrganizationID }} @@ -271,7 +289,13 @@ const inputKey = "input" {{- end -}} {{- if .IsDelete }} - dbID := {{ .Model.Name|go }}ID(id) + + {{- if .Model.HasStringPrimaryID }} + dbID := id + {{- else }} + dbID := {{ .Model.Name }}ID(id) + {{- end }} + mods := []qm.QueryMod{ dm.{{ .Model.Name|go }}Where.ID.EQ(dbID), {{- if .Model.BoilerModel.HasOrganizationID }} @@ -352,7 +376,11 @@ const inputKey = "input" mods = append(mods, qm.Select(dm.{{ .Model.Name }}Columns.ID)) mods = append(mods, qm.From(dm.TableNames.{{ .Model.Name }})) + {{- if .Model.HasStringPrimaryID }} + var IDsToRemove []boilergql.RemovedStringID + {{- else }} var IDsToRemove []boilergql.RemovedID + {{- end }} if err := dm.{{ .Model.PluralName }}(mods...).Bind(ctx, r.db, IDsToRemove); err != nil { log.Error().Err(err).Msg({{ $resolver.PublicErrorKey }}) return nil, errors.New({{ $resolver.PublicErrorKey }}) @@ -364,9 +392,16 @@ const inputKey = "input" return nil, errors.New({{ $resolver.PublicErrorKey }}) } + {{- if .Model.HasStringPrimaryID }} + return &fm.{{ .Model.PluralName }}DeletePayload{ + Ids: boilerIDs, + }, nil + {{- else }} return &fm.{{ .Model.PluralName }}DeletePayload{ Ids: boilergql.{{.Model.PrimaryKeyType|go}}IDsToGraphQL(boilerIDs, dm.TableNames.{{ .Model.Name }}), }, nil + {{- end }} + {{- end }} }