Skip to content

Commit

Permalink
Merge pull request 99designs#244 from vektah/array-coercion
Browse files Browse the repository at this point in the history
Add implicit value to array coercion
  • Loading branch information
vektah authored Aug 2, 2018
2 parents a6daa3f + 2b177c3 commit 6d625b6
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 16 deletions.
2 changes: 2 additions & 0 deletions codegen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) s
if {{.raw}} != nil {
if tmp1, ok := {{.raw}}.([]interface{}); ok {
{{.rawSlice}} = tmp1
} else {
{{.rawSlice}} = []interface{}{ {{.raw}} }
}
}
{{.result}} = make({{.type}}, len({{.rawSlice}}))
Expand Down
38 changes: 32 additions & 6 deletions example/dataloader/dataloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,42 @@ func TestTodo(t *testing.T) {
}`, &resp)
})

t.Run("customer array torture", func(t *testing.T) {
t.Run("2d array marshaling", func(t *testing.T) {
var resp struct {
Torture [][]Customer
Torture2d [][]Customer
}
c.MustPost(`{ torture(customerIds:[[1,2],[3,4,5]]) { id name } }`, &resp)
c.MustPost(`{ torture2d(customerIds:[[1,2],[3,4,5]]) { id name } }`, &resp)

require.EqualValues(t, [][]Customer{
{{ID: 1, Name: "0 0"}, {ID: 2, Name: "0 1"}},
{{ID: 3, Name: "1 0"}, {ID: 4, Name: "1 1"}, {ID: 5, Name: "1 2"}},
}, resp.Torture)
}, resp.Torture2d)
})

// Input coercion on arrays should convert non array values into an array of the appropriate depth
// http://facebook.github.io/graphql/June2018/#sec-Type-System.List
t.Run("array coercion", func(t *testing.T) {
t.Run("1d", func(t *testing.T) {
var resp struct {
Torture1d []Customer
}
c.MustPost(`{ torture1d(customerIds: 1) { id name } }`, &resp)

require.EqualValues(t, []Customer{
{ID: 1, Name: "0"},
}, resp.Torture1d)
})

t.Run("2d", func(t *testing.T) {
var resp struct {
Torture2d [][]Customer
}
c.MustPost(`{ torture2d(customerIds: 1) { id name } }`, &resp)

require.EqualValues(t, [][]Customer{
{{ID: 1, Name: "0 0"}},
}, resp.Torture2d)
})
})

t.Run("introspection", func(t *testing.T) {
Expand All @@ -56,9 +82,9 @@ func TestTodo(t *testing.T) {
var resp struct {
Torture [][]Customer
}
c.MustPost(`{ torture(customerIds:{}) { id name } }`, &resp)
err := c.Post(`{ torture2d(customerIds:{}) { id name } }`, &resp)

require.EqualValues(t, [][]Customer{}, resp.Torture)
require.EqualError(t, err, "[{\"message\":\"map[string]interface {} is not an int\"}]")
})

}
80 changes: 73 additions & 7 deletions example/dataloader/generated.go

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

11 changes: 10 additions & 1 deletion example/dataloader/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,16 @@ func (r *queryResolver) Customers(ctx context.Context) ([]Customer, error) {
}

// this method is here to test code generation of nested arrays
func (r *queryResolver) Torture(ctx context.Context, customerIds [][]int) ([][]Customer, error) {
func (r *queryResolver) Torture1d(ctx context.Context, customerIds []int) ([]Customer, error) {
result := make([]Customer, len(customerIds))
for i, id := range customerIds {
result[i] = Customer{ID: id, Name: fmt.Sprintf("%d", i), AddressID: rand.Int() % 10}
}
return result, nil
}

// this method is here to test code generation of nested arrays
func (r *queryResolver) Torture2d(ctx context.Context, customerIds [][]int) ([][]Customer, error) {
result := make([][]Customer, len(customerIds))
for i := range customerIds {
inner := make([]Customer, len(customerIds[i]))
Expand Down
5 changes: 3 additions & 2 deletions example/dataloader/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
type Query {
customers: [Customer!]

# this method is here to test code generation of nested arrays
torture(customerIds: [[Int!]]): [[Customer!]]
# these methods are here to test code generation of nested arrays
torture1d(customerIds: [Int!]): [Customer!]
torture2d(customerIds: [[Int!]]): [[Customer!]]
}

type Customer {
Expand Down

0 comments on commit 6d625b6

Please sign in to comment.