-
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.
Test mapping object types onto go primitives
- Loading branch information
Showing
7 changed files
with
302 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,8 @@ | ||
extend type Query { | ||
primitiveObject: [Primitive!]! | ||
} | ||
|
||
type Primitive { | ||
value: Int! | ||
squared: Int! | ||
} |
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,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) | ||
}) | ||
} |
Oops, something went wrong.