Skip to content

Commit

Permalink
Merge pull request #180 from vektah/import-alias-before-finalize
Browse files Browse the repository at this point in the history
Fix a bug custom scalar marshallers in external packages
  • Loading branch information
vektah authored Jul 10, 2018
2 parents 43212c0 + 160ebab commit ac9e5a6
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 118 deletions.
6 changes: 3 additions & 3 deletions codegen/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,19 @@ func (cfg *Config) bind() (*Build, error) {
// Poke a few magic methods into query
q := b.Objects.ByName(b.QueryRoot.GQLType)
q.Fields = append(q.Fields, Field{
Type: &Type{namedTypes["__Schema"], []string{modPtr}, ""},
Type: &Type{namedTypes["__Schema"], []string{modPtr}, nil},
GQLName: "__schema",
NoErr: true,
GoMethodName: "ec.introspectSchema",
Object: q,
})
q.Fields = append(q.Fields, Field{
Type: &Type{namedTypes["__Type"], []string{modPtr}, ""},
Type: &Type{namedTypes["__Type"], []string{modPtr}, nil},
GQLName: "__type",
NoErr: true,
GoMethodName: "ec.introspectType",
Args: []FieldArgument{
{GQLName: "name", Type: &Type{namedTypes["String"], []string{}, ""}, Object: &Object{}},
{GQLName: "name", Type: &Type{namedTypes["String"], []string{}, nil}, Object: &Object{}},
},
Object: q,
})
Expand Down
17 changes: 13 additions & 4 deletions codegen/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
)

type Import struct {
Name string
Alias string
Path string
Name string
Path string

alias string
}

type Imports struct {
Expand All @@ -16,5 +17,13 @@ type Imports struct {
}

func (i *Import) Write() string {
return i.Alias + " " + strconv.Quote(i.Path)
return i.Alias() + " " + strconv.Quote(i.Path)
}

func (i *Import) Alias() string {
if i.alias == "" {
panic("alias called before imports are finalized")
}

return i.alias
}
6 changes: 3 additions & 3 deletions codegen/import_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ func (s Imports) finalize() []*Import {
alias = imp.Name + strconv.Itoa(i)
i++
if i > 10 {
panic(fmt.Errorf("too many collisions, last attempt was %s", imp.Alias))
panic(fmt.Errorf("too many collisions, last attempt was %s", alias))
}
}
imp.Alias = alias
imp.alias = alias
}

return s.imports
Expand All @@ -108,7 +108,7 @@ func (s Imports) findByPath(importPath string) *Import {

func (s Imports) findByAlias(alias string) *Import {
for _, imp := range s.imports {
if imp.Alias == alias {
if imp.alias == alias {
return imp
}
}
Expand Down
12 changes: 6 additions & 6 deletions codegen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Type struct {
*NamedType

Modifiers []string
CastType string // the type to cast to when unmarshalling
CastType *Ref // the type to cast to when unmarshalling
}

const (
Expand All @@ -40,10 +40,10 @@ func (t Ref) FullName() string {
}

func (t Ref) PkgDot() string {
if t.Import == nil || t.Import.Alias == "" {
if t.Import == nil || t.Import.Alias() == "" {
return ""
}
return t.Import.Alias + "."
return t.Import.Alias() + "."
}

func (t Type) Signature() string {
Expand Down Expand Up @@ -125,7 +125,7 @@ func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) s
}

realResult := result
if t.CastType != "" {
if t.CastType != nil {
result = "castTmp"
}

Expand All @@ -140,7 +140,7 @@ func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) s
err = (&{{.result}}).UnmarshalGQL({{.raw}})
{{- end }}
{{- if .t.CastType }}
{{ .realResult }} = {{.t.CastType}}(castTmp)
{{ .realResult }} = {{.t.CastType.FullName}}(castTmp)
{{- end }}`, map[string]interface{}{
"realResult": realResult,
"result": result,
Expand All @@ -150,7 +150,7 @@ func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) s
}

func (t Type) Marshal(val string) string {
if t.CastType != "" {
if t.CastType != nil {
val = t.GoType + "(" + val + ")"
}

Expand Down
5 changes: 1 addition & 4 deletions codegen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,7 @@ func bindObject(t types.Type, object *Object, imports *Imports) error {
case normalizeVendor(structField.Type().Underlying().String()):
pkg, typ := pkgAndType(structField.Type().String())
imp := imports.findByPath(pkg)
field.CastType = typ
if imp != nil {
field.CastType = imp.Alias + "." + typ
}
field.CastType = &Ref{GoType: typ, Import: imp}

default:
// type mismatch, require custom resolver for field
Expand Down
15 changes: 9 additions & 6 deletions example/scalars/.gqlgen.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
model:
filename: model/generated.go

models:
User:
model: github.com/vektah/gqlgen/example/scalars.User
model: github.com/vektah/gqlgen/example/scalars/model.User
Timestamp:
model: github.com/vektah/gqlgen/example/scalars.Timestamp
model: github.com/vektah/gqlgen/example/scalars/model.Timestamp
SearchArgs:
model: github.com/vektah/gqlgen/example/scalars.SearchArgs
model: github.com/vektah/gqlgen/example/scalars/model.SearchArgs
Point:
model: github.com/vektah/gqlgen/example/scalars.Point
model: github.com/vektah/gqlgen/example/scalars/model.Point
ID:
model: github.com/vektah/gqlgen/example/scalars.ID
model: github.com/vektah/gqlgen/example/scalars/model.ID
Tier:
model: github.com/vektah/gqlgen/example/scalars.Tier
model: github.com/vektah/gqlgen/example/scalars/model.Tier
Loading

0 comments on commit ac9e5a6

Please sign in to comment.