Skip to content

Commit

Permalink
Fix string filters + case where null.String ids where converted in th…
Browse files Browse the repository at this point in the history
…e wrong way
  • Loading branch information
RichardLindhout committed May 9, 2020
1 parent 9107c0a commit 3bde6d9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
30 changes: 22 additions & 8 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ func init() {
}

type ModelBuild struct {
BackendModelsPath string
FrontendModelsPath string
PackageName string
Interfaces []*Interface
Models []*Model
Enums []*Enum
Scalars []string
BackendModelsPath string
FrontendModelsPath string
HasStringPrimaryIDs bool
PackageName string
Interfaces []*Interface
Models []*Model
Enums []*Enum
Scalars []string
}

type Interface struct {
Expand Down Expand Up @@ -181,6 +182,7 @@ func (m *ConvertPlugin) MutateConfig(originalCfg *config.Config) error {
models := GetModelsWithInformation(enums, originalCfg, boilerModels)

b.Models = models
b.HasStringPrimaryIDs = HasStringPrimaryIDsInModels(models)
b.Interfaces = interfaces
b.Enums = enums
b.Scalars = scalars
Expand Down Expand Up @@ -260,6 +262,14 @@ func getTemplate(filename string) string {
}
return string(content)
}
func HasStringPrimaryIDsInModels(models []*Model) bool {
for _, model := range models {
if model.HasStringPrimaryID {
return true
}
}
return false
}

// getFieldType check's if user has defined a
func getFieldType(binder *config.Binder, schema *ast.Schema, cfg *config.Config, field *ast.FieldDefinition) (types.Type, error) {
Expand Down Expand Up @@ -369,10 +379,14 @@ func enhanceModelsWithFields(enums []*Enum, schema *ast.Schema, cfg *config.Conf

// get sqlboiler information of the field
boilerField := findBoilerFieldOrForeignKey(m.BoilerModel.Fields, golangName, isRelation)
isString := strings.Contains(boilerField.Type, "string")
isString := strings.Contains(strings.ToLower(boilerField.Type), "string")
isNumberID := strings.Contains(golangName, "ID") && !isString
isPrimaryNumberID := isPrimaryID && !isString

if isNumberID || isPrimaryNumberID {
fmt.Println(isNumberID, isPrimaryNumberID, boilerField)
}

isPrimaryStringID := isPrimaryID && isString
// enable simpler code in resolvers

Expand Down
15 changes: 15 additions & 0 deletions filter.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ func IDFilterToMods(m *graphql_models.IDFilter, column string) []qm.QueryMod {
return nil
}
var queryMods []qm.QueryMod
{{- if .HasStringPrimaryIDs }}
if m.EqualTo != nil {
queryMods = append(queryMods, qmhelper.Where(column, qmhelper.EQ, *m.EqualTo))
}
if m.NotEqualTo != nil {
queryMods = append(queryMods, qmhelper.Where(column, qmhelper.NEQ, *m.NotEqualTo))
}
if len(m.In) > 0 {
queryMods = append(queryMods, qm.WhereIn(column + in, boilergql.StringsToInterfaces(m.In)...))
}
if len(m.NotIn) > 0 {
queryMods = append(queryMods, qm.WhereIn(column + notIn, boilergql.StringsToInterfaces(m.NotIn)...))
}
{{- else }}
if m.EqualTo != nil {
queryMods = append(queryMods, qmhelper.Where(column, qmhelper.EQ, boilergql.IDToBoiler(*m.EqualTo)))
}
Expand All @@ -66,6 +80,7 @@ func IDFilterToMods(m *graphql_models.IDFilter, column string) []qm.QueryMod {
if len(m.NotIn) > 0 {
queryMods = append(queryMods, qm.WhereIn(column + notIn, boilergql.IDsToBoilerInterfaces(m.NotIn)...))
}
{{- end }}
return queryMods
}

Expand Down

0 comments on commit 3bde6d9

Please sign in to comment.