Skip to content

Commit

Permalink
add test for FieldContext.IsResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
skaji committed Aug 31, 2020
1 parent 1524989 commit 3e5dd95
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions codegen/testserver/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testserver

import (
"context"
"sync"
"testing"

"github.com/99designs/gqlgen/client"
Expand All @@ -26,7 +27,13 @@ func TestMiddleware(t *testing.T) {
return []*User{{ID: 1}}, nil
}

areMethods := []bool{}
resolvers.QueryResolver.ModelMethods = func(ctx context.Context) (methods *ModelMethods, e error) {
return &ModelMethods{}, nil
}

var mu sync.Mutex
areMethods := map[string]bool{}
areResolvers := map[string]bool{}
srv := handler.NewDefaultServer(
NewExecutableSchema(Config{Resolvers: resolvers}),
)
Expand All @@ -41,7 +48,11 @@ func TestMiddleware(t *testing.T) {
})

srv.AroundFields(func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
areMethods = append(areMethods, graphql.GetFieldContext(ctx).IsMethod)
fc := graphql.GetFieldContext(ctx)
mu.Lock()
areMethods[fc.Field.Name] = fc.IsMethod
areResolvers[fc.Field.Name] = fc.IsResolver
mu.Unlock()
return next(ctx)
})

Expand All @@ -54,6 +65,9 @@ func TestMiddleware(t *testing.T) {
ID int
}
}
ModelMethods struct {
NoContext bool
}
}

called := false
Expand All @@ -63,12 +77,32 @@ func TestMiddleware(t *testing.T) {
return []*User{}, nil
}

err := c.Post(`query { user(id: 1) { id, friends { id } } }`, &resp)

// First resolves user which is a method
// Next resolves id which is not a method
// Finally resolves friends which is a method
assert.Equal(t, []bool{true, false, true}, areMethods)
err := c.Post(`query {
user(id: 1) {
id,
friends {
id
}
}
modelMethods {
noContext
}
}`, &resp)

assert.Equal(t, map[string]bool{
"user": true,
"id": false,
"friends": true,
"modelMethods": true,
"noContext": true,
}, areMethods)
assert.Equal(t, map[string]bool{
"user": true,
"id": false,
"friends": true,
"modelMethods": true,
"noContext": false,
}, areResolvers)

require.NoError(t, err)
require.True(t, called)
Expand Down

0 comments on commit 3e5dd95

Please sign in to comment.