Skip to content

Commit

Permalink
Test mapping object types onto go primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed May 12, 2019
1 parent 9ca43ba commit cab4bab
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 0 deletions.
218 changes: 218 additions & 0 deletions codegen/testserver/generated.go

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

2 changes: 2 additions & 0 deletions codegen/testserver/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ models:
model: "github.com/99designs/gqlgen/codegen/testserver.MarshalPanic"
Autobind:
model: "github.com/99designs/gqlgen/codegen/testserver.Autobind"
Primitive:
model: "github.com/99designs/gqlgen/codegen/testserver.Primitive"
MapStringInterfaceInput:
model: "map[string]interface{}"
MapStringInterfaceType:
Expand Down
6 changes: 6 additions & 0 deletions codegen/testserver/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,9 @@ const (
FallbackToStringEncodingB FallbackToStringEncoding = "B"
FallbackToStringEncodingC FallbackToStringEncoding = "C"
)

type Primitive int

func (p Primitive) Squared() int {
return int(p) * int(p)
}
8 changes: 8 additions & 0 deletions codegen/testserver/primitive_objects.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extend type Query {
primitiveObject: [Primitive!]!
}

type Primitive {
value: Int!
squared: Int!
}
40 changes: 40 additions & 0 deletions codegen/testserver/primitive_objects_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package testserver

import (
"context"
"net/http/httptest"
"testing"

"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/handler"
"github.com/stretchr/testify/assert"
)

func TestPrimitiveObjects(t *testing.T) {
resolvers := &Stub{}
resolvers.QueryResolver.PrimitiveObject = func(ctx context.Context) (out []Primitive, e error) {
return []Primitive{2, 4}, nil
}

resolvers.PrimitiveResolver.Value = func(ctx context.Context, obj *Primitive) (i int, e error) {
return int(*obj), nil
}

srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(Config{Resolvers: resolvers})))
c := client.New(srv.URL)

t.Run("can fetch value", func(t *testing.T) {
var resp struct {
PrimitiveObject []struct {
Value int
Squared int
}
}
c.MustPost(`query { primitiveObject { value, squared } }`, &resp)

assert.Equal(t, 2, resp.PrimitiveObject[0].Value)
assert.Equal(t, 4, resp.PrimitiveObject[0].Squared)
assert.Equal(t, 4, resp.PrimitiveObject[1].Value)
assert.Equal(t, 16, resp.PrimitiveObject[1].Squared)
})
}
Loading

0 comments on commit cab4bab

Please sign in to comment.