Skip to content

Commit

Permalink
Bugfix for wrong graphql type when using custom scalars in gqlgen
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLindhout committed May 20, 2020
1 parent b04cd12 commit 66c3f47
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
40 changes: 31 additions & 9 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,19 +457,32 @@ func enhanceModelsWithFields(enums []*Enum, schema *ast.Schema, cfg *config.Conf
}
}

var ignoreTypePrefixes = []string{"graphql_models", "models"}

func getShortType(longType string) string {

// longType e.g = gitlab.com/decicify/app/backend/graphql_models.FlowWhere
splittedBySlash := strings.Split(longType, "/")
// gitlab.com, decicify, app, backend, graphql_models.FlowWhere

lastPart := splittedBySlash[len(splittedBySlash)-1]
splitted := strings.Split(lastPart, ".")
isPointer := strings.HasPrefix(longType, "*")
isStructInPackage := strings.Count(lastPart, ".") > 0

if isStructInPackage {
// if packages are deeper they don't have pointers but *time.Time will since it's not deep
returnType := strings.TrimPrefix(lastPart, "*")
for _, ignoreType := range ignoreTypePrefixes {
fullIgnoreType := ignoreType + "."
returnType = strings.TrimPrefix(returnType, fullIgnoreType)
}

if len(splitted) > 1 {
if isPointer {
return "*" + splitted[1]
return "*" + returnType
}
return splitted[1]
return returnType
}

return longType
}

Expand Down Expand Up @@ -814,6 +827,10 @@ func getConvertConfig(enums []*Enum, model *Model, field *Field) (cc ConvertConf
} else if graphType != boilType {
cc.IsCustom = true

if strings.ToLower(model.Name) == "blockchoice" {
fmt.Println(graphType, boilType)
}

if field.IsPrimaryNumberID || field.IsNumberID {

cc.ToGraphQL = "VALUE"
Expand Down Expand Up @@ -879,16 +896,21 @@ func getToGraphQL(boilType, graphType string) string {
}

func getBoilerTypeAsText(boilType string) string {
if strings.HasPrefix(boilType, "null.") {
boilType = strings.TrimPrefix(boilType, "null.")
boilType = strcase.ToCamel(boilType)
boilType = "NullDot" + boilType
}

// backward compatible missed Dot
if strings.HasPrefix(boilType, "types.") {
boilType = strings.TrimPrefix(boilType, "types.")
boilType = strcase.ToCamel(boilType)
boilType = "Types" + boilType
}

// if strings.HasPrefix(boilType, "null.") {
// boilType = strings.TrimPrefix(boilType, "null.")
// boilType = strcase.ToCamel(boilType)
// boilType = "NullDot" + boilType
// }
boilType = strings.Replace(boilType, ".", "Dot", -1)

return strcase.ToCamel(boilType)
}

Expand Down
21 changes: 21 additions & 0 deletions convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// testing really need more improvements!
// Please add more tests if you use this

package gqlgen_sqlboiler

import "testing"

func TestShortType(t *testing.T) {
testShortType(t, "gitlab.com/decicify/app/backend/graphql_models.FlowWhere", "FlowWhere")
testShortType(t, "*gitlab.com/decicify/app/backend/graphql_models.FlowWhere", "*FlowWhere")
testShortType(t, "*string", "*string")
testShortType(t, "string", "string")
testShortType(t, "*time.Time", "*time.Time")
}

func testShortType(t *testing.T, input, output string) {
result := getShortType(input)
if result != output {
t.Errorf("%v should result in %v but did result in %v", input, output, result)
}
}
2 changes: 1 addition & 1 deletion preload.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
var TablePreloadMap = map[string]map[string]boilergql.ColumnSetting{
{{ range $model := .Models -}}
{{ if $model.IsPreloadable -}}
models.TableNames.{{- $model.Name }}: map[string]boilergql.ColumnSetting{
models.TableNames.{{- $model.Name }}: {
{{- range $value := $model.PreloadArray }}
"{{$value.Key}}": {
Name: {{$value.ColumnSetting.Name}},
Expand Down

0 comments on commit 66c3f47

Please sign in to comment.