Skip to content

Commit

Permalink
allow binding a GraphQL Any field to a struct method returning `*an…
Browse files Browse the repository at this point in the history
…y` (#2644)

* allow binding GQL `Any` field to struct method returning `*any`

* add singlefile tests for binding to `*any` case

* add followschema tests for binding to `*any` case

* make ptr_to_any binding tests follow binding conventions better
  • Loading branch information
Tooni authored May 24, 2023
1 parent c313bf3 commit 22deb8b
Show file tree
Hide file tree
Showing 17 changed files with 693 additions and 5 deletions.
8 changes: 8 additions & 0 deletions codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@ func (t *TypeReference) IsPtrToSlice() bool {
return false
}

func (t *TypeReference) IsPtrToIntf() bool {
if t.IsPtr() {
_, isPointerToInterface := t.GO.(*types.Pointer).Elem().(*types.Interface)
return isPointerToInterface
}
return false
}

func (t *TypeReference) IsNamed() bool {
_, isSlice := t.GO.(*types.Named)
return isSlice
Expand Down
199 changes: 199 additions & 0 deletions codegen/testserver/followschema/ptr_to_any.generated.go

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

9 changes: 9 additions & 0 deletions codegen/testserver/followschema/ptr_to_any.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package followschema

type PtrToAnyContainer struct {
PtrToAny *any
}

func (c *PtrToAnyContainer) Binding() *any {
return c.PtrToAny
}
10 changes: 10 additions & 0 deletions codegen/testserver/followschema/ptr_to_any.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
scalar Any

type PtrToAnyContainer {
ptrToAny: Any
binding: Any
}

extend type Query {
ptrToAnyContainer: PtrToAnyContainer!
}
37 changes: 37 additions & 0 deletions codegen/testserver/followschema/ptr_to_any_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package followschema

import (
"context"
"testing"

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

func TestPtrToAny(t *testing.T) {
resolvers := &Stub{}

c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers})))

var a any = `{"some":"thing"}`
resolvers.QueryResolver.PtrToAnyContainer = func(ctx context.Context) (wrappedStruct *PtrToAnyContainer, e error) {
ptrToAnyContainer := PtrToAnyContainer{
PtrToAny: &a,
}
return &ptrToAnyContainer, nil
}

t.Run("binding to pointer to any", func(t *testing.T) {
var resp struct {
PtrToAnyContainer struct {
Binding *any
}
}

err := c.Post(`query { ptrToAnyContainer { binding }}`, &resp)
require.NoError(t, err)

require.Equal(t, &a, resp.PtrToAnyContainer.Binding)
})
}
5 changes: 5 additions & 0 deletions codegen/testserver/followschema/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ func (r *queryResolver) PrimitiveStringObject(ctx context.Context) ([]PrimitiveS
panic("not implemented")
}

// PtrToAnyContainer is the resolver for the ptrToAnyContainer field.
func (r *queryResolver) PtrToAnyContainer(ctx context.Context) (*PtrToAnyContainer, error) {
panic("not implemented")
}

// PtrToSliceContainer is the resolver for the ptrToSliceContainer field.
func (r *queryResolver) PtrToSliceContainer(ctx context.Context) (*PtrToSliceContainer, error) {
panic("not implemented")
Expand Down
30 changes: 29 additions & 1 deletion codegen/testserver/followschema/root_.generated.go

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

Loading

0 comments on commit 22deb8b

Please sign in to comment.