Skip to content

Commit

Permalink
Arg type binding
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr committed Feb 6, 2019
1 parent 6af3d85 commit 956d030
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 29 deletions.
7 changes: 6 additions & 1 deletion codegen/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ nextArg:
param := params.At(j)
for _, oldArg := range field.Args {
if strings.EqualFold(oldArg.Name, param.Name()) {
oldArg.TypeReference.GO = param.Type()
tr, err := b.Binder.TypeReference(oldArg.Type, param.Type())
if err != nil {
return err
}
oldArg.TypeReference = tr

newArgs = append(newArgs, oldArg)
continue nextArg
}
Expand Down
2 changes: 1 addition & 1 deletion codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (c *Config) InjectBuiltins(s *ast.Schema) {
"ID": {
Model: StringList{
"github.com/99designs/gqlgen/graphql.ID",
"github.com/99designs/gqlgen/graphql.Int",
"github.com/99designs/gqlgen/graphql.IntID",
},
},
}
Expand Down
15 changes: 15 additions & 0 deletions example/chat/generated.go

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

15 changes: 15 additions & 0 deletions example/config/generated.go

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

15 changes: 15 additions & 0 deletions example/dataloader/generated.go

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

15 changes: 15 additions & 0 deletions example/scalars/generated.go

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

15 changes: 15 additions & 0 deletions example/selection/generated.go

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

15 changes: 15 additions & 0 deletions example/starwars/generated.go

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

20 changes: 6 additions & 14 deletions example/todo/generated.go

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

4 changes: 4 additions & 0 deletions example/todo/gqlgen.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
models:
Todo:
model: github.com/99designs/gqlgen/example/todo.Todo
ID:
model: # override the default id marshaller to use ints
- github.com/99designs/gqlgen/graphql.IntID
- github.com/99designs/gqlgen/graphql.ID
4 changes: 2 additions & 2 deletions example/todo/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ schema {
}

type MyQuery {
todo(id: Int!): Todo
todo(id: ID!): Todo
lastTodo: Todo
todos: [Todo!]!
}

type MyMutation {
createTodo(todo: TodoInput!): Todo!
updateTodo(id: Int!, changes: Map!): Todo
updateTodo(id: ID!, changes: Map!): Todo
}

type Todo {
Expand Down
22 changes: 11 additions & 11 deletions example/todo/todo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ func TestTodo(t *testing.T) {
c := client.New(srv.URL)

var resp struct {
CreateTodo struct{ ID int }
CreateTodo struct{ ID string }
}
c.MustPost(`mutation { createTodo(todo:{text:"Fery important"}) { id } }`, &resp)

require.Equal(t, 5, resp.CreateTodo.ID)
require.Equal(t, "5", resp.CreateTodo.ID)

t.Run("update the todo text", func(t *testing.T) {
var resp struct {
UpdateTodo struct{ Text string }
}
c.MustPost(
`mutation($id: Int!, $text: String!) { updateTodo(id: $id, changes:{text:$text}) { text } }`,
`mutation($id: ID!, $text: String!) { updateTodo(id: $id, changes:{text:$text}) { text } }`,
&resp,
client.Var("id", 5),
client.Var("text", "Very important"),
Expand Down Expand Up @@ -58,12 +58,12 @@ func TestTodo(t *testing.T) {
t.Run("select with alias", func(t *testing.T) {
var resp struct {
A struct{ Text string }
B struct{ ID int }
B struct{ ID string }
}
c.MustPost(`{ a: todo(id:1) { text } b: todo(id:2) { id } }`, &resp)

require.Equal(t, "A todo not to forget", resp.A.Text)
require.Equal(t, 2, resp.B.ID)
require.Equal(t, "2", resp.B.ID)
})

t.Run("find a missing todo", func(t *testing.T) {
Expand Down Expand Up @@ -101,17 +101,17 @@ func TestTodo(t *testing.T) {
t.Run("select all", func(t *testing.T) {
var resp struct {
Todo struct {
ID int
ID string
Text string
Done bool
}
LastTodo struct {
ID int
ID string
Text string
Done bool
}
Todos []struct {
ID int
ID string
Text string
}
}
Expand All @@ -121,11 +121,11 @@ func TestTodo(t *testing.T) {
todos { id text }
}`, &resp)

require.Equal(t, 1, resp.Todo.ID)
require.Equal(t, 5, resp.LastTodo.ID)
require.Equal(t, "1", resp.Todo.ID)
require.Equal(t, "5", resp.LastTodo.ID)
require.Len(t, resp.Todos, 5)
require.Equal(t, "Very important", resp.LastTodo.Text)
require.Equal(t, 5, resp.LastTodo.ID)
require.Equal(t, "5", resp.LastTodo.ID)
})

t.Run("introspection", func(t *testing.T) {
Expand Down
15 changes: 15 additions & 0 deletions example/type-system-extension/generated.go

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

21 changes: 21 additions & 0 deletions graphql/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,24 @@ func UnmarshalID(v interface{}) (string, error) {
return "", fmt.Errorf("%T is not a string", v)
}
}

func MarshalIntID(i int) Marshaler {
return WriterFunc(func(w io.Writer) {
writeQuotedString(w, strconv.Itoa(i))
})
}

func UnmarshalIntID(v interface{}) (int, error) {
switch v := v.(type) {
case string:
return strconv.Atoi(v)
case int:
return v, nil
case int64:
return int(v), nil
case json.Number:
return strconv.Atoi(string(v))
default:
return 0, fmt.Errorf("%T is not an int", v)
}
}
15 changes: 15 additions & 0 deletions integration/generated.go

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

0 comments on commit 956d030

Please sign in to comment.