Skip to content

Commit

Permalink
Add test case for custom scalar to slice
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathew Byrne committed Mar 18, 2019
1 parent 2284a3e commit 515f225
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 0 deletions.
27 changes: 27 additions & 0 deletions codegen/testserver/bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package testserver

import (
"fmt"
"io"

"github.com/99designs/gqlgen/graphql"
)

func MarshalBytes(b []byte) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, _ = fmt.Fprintf(w, "%q", string(b))
})
}

func UnmarshalBytes(v interface{}) ([]byte, error) {
switch v := v.(type) {
case string:
return []byte(v), nil
case *string:
return []byte(*v), nil
case []byte:
return v, nil
default:
return nil, fmt.Errorf("%T is not []byte", v)
}
}
61 changes: 61 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 @@ -68,3 +68,5 @@ models:
oldFoo: { fieldName: foo, resolver: true }
FallbackToStringEncoding:
model: "github.com/99designs/gqlgen/codegen/testserver.FallbackToStringEncoding"
Bytes:
model: "github.com/99designs/gqlgen/codegen/testserver.Bytes"
3 changes: 3 additions & 0 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ func (r *queryResolver) DefaultScalar(ctx context.Context, arg string) (string,
func (r *queryResolver) Slices(ctx context.Context) (*Slices, error) {
panic("not implemented")
}
func (r *queryResolver) ScalarSlice(ctx context.Context) ([]byte, error) {
panic("not implemented")
}
func (r *queryResolver) Fallback(ctx context.Context, arg FallbackToStringEncoding) (FallbackToStringEncoding, error) {
panic("not implemented")
}
Expand Down
3 changes: 3 additions & 0 deletions codegen/testserver/slices.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extend type Query {
slices: Slices
scalarSlice: Bytes!
}

type Slices {
Expand All @@ -8,3 +9,5 @@ type Slices {
test3: [String]!
test4: [String!]!
}

scalar Bytes
12 changes: 12 additions & 0 deletions codegen/testserver/slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ func TestSlices(t *testing.T) {
require.NotNil(t, resp.Slices.Test3)
require.NotNil(t, resp.Slices.Test4)
})

t.Run("custom scalars to slices work", func(t *testing.T) {
resolvers.QueryResolver.ScalarSlice = func(ctx context.Context) ([]byte, error) {
return []byte("testing"), nil
}

var resp struct {
ScalarSlice string
}
c.MustPost(`query { scalarSlice }`, &resp)
require.Equal(t, "testing", resp.ScalarSlice)
})
}
4 changes: 4 additions & 0 deletions codegen/testserver/stub.go

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

0 comments on commit 515f225

Please sign in to comment.