-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from vektah/multi-stage-model-build
Split model generation into its own stage
- Loading branch information
Showing
19 changed files
with
252 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package codegen | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
|
||
"github.com/vektah/gqlgen/neelance/schema" | ||
) | ||
|
||
func buildModels(types NamedTypes, s *schema.Schema) Objects { | ||
var models Objects | ||
|
||
for _, typ := range s.Types { | ||
var model *Object | ||
switch typ := typ.(type) { | ||
case *schema.Object: | ||
model = buildObject(types, typ, s) | ||
|
||
case *schema.InputObject: | ||
model = buildInput(types, typ) | ||
} | ||
|
||
if model == nil || model.Root || model.GoType != "" { | ||
continue | ||
} | ||
|
||
bindGenerated(types, model) | ||
|
||
models = append(models, model) | ||
} | ||
|
||
sort.Slice(models, func(i, j int) bool { | ||
return strings.Compare(models[i].GQLType, models[j].GQLType) == -1 | ||
}) | ||
|
||
return models | ||
} | ||
|
||
func bindGenerated(types NamedTypes, object *Object) { | ||
object.GoType = ucFirst(object.GQLType) | ||
object.Marshaler = &Ref{GoType: object.GoType} | ||
|
||
for i := range object.Fields { | ||
field := &object.Fields[i] | ||
|
||
if field.IsScalar { | ||
field.GoVarName = ucFirst(field.GQLName) | ||
if field.GoVarName == "Id" { | ||
field.GoVarName = "ID" | ||
} | ||
} else if object.Input { | ||
field.GoFKName = ucFirst(field.GQLName) | ||
field.GoFKType = types[field.GQLType].GoType | ||
} else { | ||
field.GoFKName = ucFirst(field.GQLName) + "ID" | ||
field.GoFKType = "int" // todo: use schema to determine type of id? | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ type Object struct { | |
Root bool | ||
DisableConcurrency bool | ||
Stream bool | ||
Input bool | ||
} | ||
|
||
type Field struct { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.