Skip to content

Commit

Permalink
Merge pull request 99designs#123 from vektah/drop-fk-generation
Browse files Browse the repository at this point in the history
BC Break: Stop generating foreign keys in models
  • Loading branch information
vektah authored May 30, 2018
2 parents 36cb610 + 1f81d09 commit c7b9c2a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 34 deletions.
21 changes: 2 additions & 19 deletions codegen/models_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,13 @@ func (cfg *Config) obj2Model(obj *Object) Model {

for i := range obj.Fields {
field := &obj.Fields[i]
mf := ModelField{Type: field.Type,GQLName:field.GQLName}
mf := ModelField{Type: field.Type, GQLName: field.GQLName}

mf.GoVarName = ucFirst(field.GQLName)
if mf.IsScalar {
mf.GoVarName = ucFirst(field.GQLName)
if mf.GoVarName == "Id" {
mf.GoVarName = "ID"
}
} else if mf.IsInput {
mf.GoVarName = ucFirst(field.GQLName)
} else if mf.IsSlice() {
// one to many, we don't need a prop, we need a resolver
} else {
mf.GoFKName = ucFirst(field.GQLName) + "ID"
mf.GoFKType = "string"

if obj, ok := cfg.schema.Types[field.GQLType].(*schema.Object); ok {
for _, f := range obj.Fields {
if strings.EqualFold(f.Name, "id") {
if strings.Contains(f.Type.String(), "Int") {
mf.GoFKType = "int"
}
}
}
}
}

model.Fields = append(model.Fields, mf)
Expand Down
14 changes: 0 additions & 14 deletions example/dataloader/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion example/dataloader/resolvers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate gorunpkg github.com/vektah/gqlgen -out generated.go
//go:generate gorunpkg github.com/vektah/gqlgen -out generated.go -typemap types.json

package dataloader

Expand All @@ -9,6 +9,17 @@ import (
"time"
)

type Customer struct {
ID int `json:"id"`
Name string `json:"name"`
AddressID int
}
type Order struct {
ID int `json:"id"`
Date time.Time `json:"date"`
Amount float64 `json:"amount"`
}

type Resolver struct{}

func (r *Resolver) Customer_address(ctx context.Context, it *Customer) (*Address, error) {
Expand Down
4 changes: 4 additions & 0 deletions example/dataloader/types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Order": "github.com/vektah/gqlgen/example/dataloader.Order",
"Customer": "github.com/vektah/gqlgen/example/dataloader.Customer"
}

0 comments on commit c7b9c2a

Please sign in to comment.