-
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 #10 from vektah/newtypes
User defined custom types
- Loading branch information
Showing
43 changed files
with
2,250 additions
and
1,614 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,70 @@ | ||
package codegen | ||
|
||
import ( | ||
"fmt" | ||
"go/types" | ||
"os" | ||
"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) | ||
|
||
def, err := findGoType(prog, input.Package, input.GoType) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, err.Error()) | ||
} | ||
if 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
Oops, something went wrong.