-
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.
- Loading branch information
Showing
13 changed files
with
202 additions
and
146 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package codegen | ||
|
||
import ( | ||
"go/types" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/vektah/gqlgen/neelance/schema" | ||
"golang.org/x/tools/go/loader" | ||
) | ||
|
||
func buildInputs(namedTypes NamedTypes, s *schema.Schema, prog *loader.Program) Objects { | ||
var inputs Objects | ||
|
||
for _, typ := range s.Types { | ||
switch typ := typ.(type) { | ||
case *schema.InputObject: | ||
input := buildInput(namedTypes, typ) | ||
|
||
if def := findGoType(prog, input.Package, input.GoType); def != nil { | ||
input.Marshaler = buildInputMarshaler(typ, def) | ||
bindObject(def.Type(), input) | ||
} | ||
|
||
inputs = append(inputs, input) | ||
} | ||
} | ||
|
||
sort.Slice(inputs, func(i, j int) bool { | ||
return strings.Compare(inputs[i].GQLType, inputs[j].GQLType) == -1 | ||
}) | ||
|
||
return inputs | ||
} | ||
|
||
func buildInput(types NamedTypes, typ *schema.InputObject) *Object { | ||
obj := &Object{NamedType: types[typ.TypeName()]} | ||
|
||
for _, field := range typ.Values { | ||
obj.Fields = append(obj.Fields, Field{ | ||
GQLName: field.Name.Name, | ||
Type: types.getType(field.Type), | ||
Object: obj, | ||
}) | ||
} | ||
return obj | ||
} | ||
|
||
// if user has implemented an UnmarshalGQL method on the input type manually, use it | ||
// otherwise we will generate one. | ||
func buildInputMarshaler(typ *schema.InputObject, def types.Object) *Ref { | ||
switch def := def.(type) { | ||
case *types.TypeName: | ||
namedType := def.Type().(*types.Named) | ||
for i := 0; i < namedType.NumMethods(); i++ { | ||
method := namedType.Method(i) | ||
if method.Name() == "UnmarshalGQL" { | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
return &Ref{GoType: typ.Name} | ||
} |
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
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 was deleted.
Oops, something went wrong.
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.