Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLindhout committed May 7, 2020
1 parent 6b6c264 commit 8c6b723
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 33 deletions.
67 changes: 41 additions & 26 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Model struct {
HasOrganizationID bool
HasUserOrganizationID bool
HasUserID bool
HasStringPrimaryID bool
// other stuff
Description string
PureFields []*ast.FieldDefinition
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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"
Expand All @@ -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 + ")"
}

Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions convert.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }})
}
Expand All @@ -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 }}
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions convert_input.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
Expand All @@ -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 -}}
Expand Down
2 changes: 1 addition & 1 deletion filter.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }})...)
}
Expand Down
37 changes: 36 additions & 1 deletion resolver.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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 }},
Expand Down Expand Up @@ -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 }}
Expand Down Expand Up @@ -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 }}
Expand Down Expand Up @@ -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 }})
Expand All @@ -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 }}
}
Expand Down

0 comments on commit 8c6b723

Please sign in to comment.