diff --git a/cache/cache.go b/cache/cache.go index 0de29e8..0dbb552 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -525,7 +525,7 @@ func getModelsFromSchema(schema *ast.Schema, boilerModels []*structs.BoilerModel // silent continue continue } - // log.Warn().Str("model", modelName).Msg("skipped because no database model found") + log.Debug().Str("model", modelName).Msg("skipped because no database model found") continue } } @@ -541,6 +541,7 @@ func getModelsFromSchema(schema *ast.Schema, boilerModels []*structs.BoilerModel } m := &structs.Model{ Name: modelName, + JSONName: strcase.ToCamel(modelName), Description: schemaType.Description, PluralName: Plural(modelName), BoilerModel: boilerModel, @@ -654,7 +655,7 @@ func safeTrim(v string, trimSuffix string) string { } func foreignKeyToRel(v string) string { - return strings.TrimSuffix(strings.TrimSuffix(strcase.ToCamel(v), "Id"), "ID") + return strings.TrimSuffix(v, "ID") } func isStruct(t types.Type) bool { @@ -814,7 +815,7 @@ func getGraphTypeAsText(graphType string) string { func FindBoilerModel(models []*structs.BoilerModel, modelName string) *structs.BoilerModel { for _, m := range models { - if m.Name == modelName { + if strings.ToLower(m.Name) == strings.ToLower(modelName) { return m } } diff --git a/cache/sqlboiler_parse.go b/cache/sqlboiler_parse.go index 23e0540..8f5493e 100644 --- a/cache/sqlboiler_parse.go +++ b/cache/sqlboiler_parse.go @@ -53,19 +53,26 @@ func GetBoilerModels(dir string) ([]*structs.BoilerModel, []*structs.BoilerEnum) splitted := strings.Split(boiler.Name, ".") // result in e.g. User modelName := splitted[0] + // result in e.g. ID boilerFieldName := splitted[1] + somethingFcm := strings.Contains(strings.ToLower(modelName), "fcm") // handle names with lowercase e.g. userR, userL or other sqlboiler extra's if IsFirstCharacterLowerCase(modelName) { // It's the relations of the model // let's add them so we can use them later if strings.HasSuffix(modelName, "R") { - modelName = strcase.ToCamel(strings.TrimSuffix(modelName, "R")) - + modelNameBefore := strings.TrimSuffix(modelName, "R") + modelName = strings.ToUpper(string(modelNameBefore[0])) + modelNameBefore[1:] isArray := strings.HasSuffix(boiler.Type, "Slice") boilerType := strings.TrimSuffix(boiler.Type, "Slice") + if somethingFcm { + fmt.Println("boilerType", boilerType) + fmt.Println("boilerFieldName", boilerFieldName) + } + relationField := &structs.BoilerField{ Name: boilerFieldName, RelationshipName: strings.TrimSuffix(boilerFieldName, "ID"), @@ -165,10 +172,10 @@ func GetBoilerModels(dir string) ([]*structs.BoilerModel, []*structs.BoilerEnum) } if field.IsRelation && field.Relationship == nil { - // log.Debug().Str("model", model.Name).Str("field", field.Name).Msg( - // "We could not find the relationship in the generated " + - // "boiler structs this could result in unexpected behavior, we marked this field as " + - // "non-relational \n") + log.Debug().Str("model", model.Name).Str("field", field.Name).Msg( + "We could not find the relationship in the generated " + + "boiler structs this could result in unexpected behavior, we marked this field as " + + "non-relational \n") field.IsRelation = false } diff --git a/plugin_resolver.go b/plugin_resolver.go index 97c1232..aa4884d 100644 --- a/plugin_resolver.go +++ b/plugin_resolver.go @@ -308,7 +308,7 @@ func findModelOrEmpty(models []*structs.Model, modelName string) structs.Model { return structs.Model{} } for _, m := range models { - if m.Name == modelName { + if strings.ToLower(m.Name) == strings.ToLower(modelName) { return *m } } diff --git a/sqlboiler_graphql_schema.go b/sqlboiler_graphql_schema.go index 6874c8e..3680a95 100644 --- a/sqlboiler_graphql_schema.go +++ b/sqlboiler_graphql_schema.go @@ -207,7 +207,6 @@ func SchemaGet( w.br() for _, model := range models { - // enum UserSort { FIRST_NAME, LAST_NAME } w.l("enum " + model.Name + "Sort {") for _, v := range fieldAsEnumStrings(model.Fields) { diff --git a/structs/structs.go b/structs/structs.go index 2ba4a5e..380d31b 100644 --- a/structs/structs.go +++ b/structs/structs.go @@ -26,6 +26,7 @@ type Preload struct { type Model struct { //nolint:maligned Name string + JSONName string PluralName string BoilerModel *BoilerModel HasBoilerModel bool diff --git a/template_files/generated_convert.gotpl b/template_files/generated_convert.gotpl index df94c1a..a71da0c 100755 --- a/template_files/generated_convert.gotpl +++ b/template_files/generated_convert.gotpl @@ -229,8 +229,8 @@ import ( {{- if $field.IsRelation }} {{- if $field.IsPlural }} - if m.R != nil && m.R.{{ $field.Name }} != nil { - r.{{ $field.Name }} = {{ $field.BoilerField.Relationship.PluralName }}ToGraphQL(m.R.{{ $field.Name }}) + if m.R != nil && m.R.{{ $field.BoilerField.Name }} != nil { + r.{{ $field.Name }} = {{ $field.BoilerField.Relationship.PluralName }}ToGraphQL(m.R.{{ $field.BoilerField.Name }}) } {{- else }} {{- if $field.BoilerField.IsForeignKey }} @@ -242,8 +242,8 @@ import ( } } {{- else }} - if m.R != nil && m.R.{{ $field.Name }} != nil { - r.{{ $field.Name }} = {{ $field.BoilerField.Relationship.Name }}ToGraphQL(m.R.{{ $field.Name }}) + if m.R != nil && m.R.{{ $field.BoilerField.Name }} != nil { + r.{{ $field.Name }} = {{ $field.BoilerField.Relationship.Name }}ToGraphQL(m.R.{{ $field.BoilerField.Name }}) } {{- end -}} {{- end -}} diff --git a/template_files/generated_resolver.gotpl b/template_files/generated_resolver.gotpl index 268f0a1..1584491 100644 --- a/template_files/generated_resolver.gotpl +++ b/template_files/generated_resolver.gotpl @@ -136,7 +136,7 @@ const inputKey = "input" } // resolve requested fields after creating - mods := Get{{ .Model.Name }}PreloadModsWithLevel(ctx, {{ .Model.Name }}PayloadPreloadLevels.{{ .Model.Name }}) + mods := Get{{ .Model.Name }}PreloadModsWithLevel(ctx, {{ .Model.Name }}PayloadPreloadLevels.{{ .Model.JSONName }}) mods = append(mods, dm.{{ .Model.Name }}Where.ID.EQ(m.ID)) pM, err := dm.{{ .Model.PluralName }}(mods...).One(ctx, r.db) if err != nil { @@ -144,7 +144,7 @@ const inputKey = "input" return nil, errors.New({{ $resolver.PublicErrorKey }}) } return &fm.{{ .Model.Name }}Payload{ - {{ .Model.Name }}: {{ .Model.Name }}ToGraphQL(pM), + {{ .Model.JSONName }}: {{ .Model.Name }}ToGraphQL(pM), }, nil {{- end -}} @@ -194,7 +194,7 @@ const inputKey = "input" } // resolve requested fields after updating - mods := Get{{ .Model.Name }}PreloadModsWithLevel(ctx, {{ .Model.Name }}PayloadPreloadLevels.{{ .Model.Name }}) + mods := Get{{ .Model.Name }}PreloadModsWithLevel(ctx, {{ .Model.Name }}PayloadPreloadLevels.{{ .Model.JSONName }}) mods = append(mods, dm.{{ .Model.Name }}Where.ID.EQ(dbID)) {{ range $scope := $.AuthorizationScopes -}} {{- if (call $scope.AddHook $resolver.Model.BoilerModel $resolver "updateAfterWhere") }} @@ -208,7 +208,7 @@ const inputKey = "input" return nil, errors.New({{ $resolver.PublicErrorKey }}) } return &fm.{{ .Model.Name }}Payload{ - {{ .Model.Name }}: {{ .Model.Name }}ToGraphQL(pM), + {{ .Model.JSONName }}: {{ .Model.Name }}ToGraphQL(pM), }, nil {{- end -}} @@ -308,7 +308,7 @@ func (r *queryResolver) Node(ctx context.Context, globalGraphID string) (fm.Node {{ range $model := .Models -}} {{ if .IsNormal -}} case "{{$model.Name}}": - return r.{{$model.Name}}(ctx, globalGraphID) + return r.{{$model.JSONName}}(ctx, globalGraphID) {{ end -}} {{ end -}} diff --git a/template_files/generated_sort.gotpl b/template_files/generated_sort.gotpl index 88e4d10..db853e3 100644 --- a/template_files/generated_sort.gotpl +++ b/template_files/generated_sort.gotpl @@ -43,7 +43,7 @@ import ( {{- if eq $field.Name "Sort" -}} var {{ $field.Enum.Name }}Column = map[{{ $.Frontend.PackageName }}.{{$field.Enum.Name}}]string{ {{- range $value := $field.Enum.Values}} - {{ $.Frontend.PackageName }}.{{ $field.Enum.Name|go }}{{ .Name|go }}: {{ $.Backend.PackageName }}.{{ $model.BoilerModel.Name }}Columns.{{ $value.BoilerEnumValue.Name|go }}, + {{ $.Frontend.PackageName }}.{{ $field.Enum.Name|go }}{{ .Name|go }}: {{ $.Backend.PackageName }}.{{ $model.BoilerModel.Name }}Columns.{{ $value.Name|go }}, {{- end }} }