Skip to content

Commit

Permalink
Separate resolvers for queries and mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
sanae10001 committed Mar 15, 2018
1 parent 5fa468a commit df9ba5a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 23 deletions.
2 changes: 1 addition & 1 deletion example/starwars/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
var schema *graphql.Schema

func init() {
schema = graphql.MustParseSchema(starwars.Schema, &starwars.Resolver{})
schema = graphql.MustParseSchema(starwars.Schema, &starwars.RootResolver{})
}

func main() {
Expand Down
28 changes: 19 additions & 9 deletions example/starwars/starwars.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,24 +284,34 @@ type review struct {

var reviews = make(map[string][]*review)

type Resolver struct{}
type RootResolver struct{}
type QueryResolver struct{}
type MutationResolver struct{}

func (r *Resolver) Hero(args struct{ Episode string }) *characterResolver {
func (r *RootResolver) Query() *QueryResolver {
return &QueryResolver{}
}

func (r *RootResolver) Mutation() *MutationResolver {
return &MutationResolver{}
}

func (r *QueryResolver) Hero(args struct{ Episode string }) *characterResolver {
if args.Episode == "EMPIRE" {
return &characterResolver{&humanResolver{humanData["1000"]}}
}
return &characterResolver{&droidResolver{droidData["2001"]}}
}

func (r *Resolver) Reviews(args struct{ Episode string }) []*reviewResolver {
func (r *QueryResolver) Reviews(args struct{ Episode string }) []*reviewResolver {
var l []*reviewResolver
for _, review := range reviews[args.Episode] {
l = append(l, &reviewResolver{review})
}
return l
}

func (r *Resolver) Search(args struct{ Text string }) []*searchResultResolver {
func (r *QueryResolver) Search(args struct{ Text string }) []*searchResultResolver {
var l []*searchResultResolver
for _, h := range humans {
if strings.Contains(h.Name, args.Text) {
Expand All @@ -321,7 +331,7 @@ func (r *Resolver) Search(args struct{ Text string }) []*searchResultResolver {
return l
}

func (r *Resolver) Character(args struct{ ID graphql.ID }) *characterResolver {
func (r *QueryResolver) Character(args struct{ ID graphql.ID }) *characterResolver {
if h := humanData[args.ID]; h != nil {
return &characterResolver{&humanResolver{h}}
}
Expand All @@ -331,28 +341,28 @@ func (r *Resolver) Character(args struct{ ID graphql.ID }) *characterResolver {
return nil
}

func (r *Resolver) Human(args struct{ ID graphql.ID }) *humanResolver {
func (r *QueryResolver) Human(args struct{ ID graphql.ID }) *humanResolver {
if h := humanData[args.ID]; h != nil {
return &humanResolver{h}
}
return nil
}

func (r *Resolver) Droid(args struct{ ID graphql.ID }) *droidResolver {
func (r *QueryResolver) Droid(args struct{ ID graphql.ID }) *droidResolver {
if d := droidData[args.ID]; d != nil {
return &droidResolver{d}
}
return nil
}

func (r *Resolver) Starship(args struct{ ID graphql.ID }) *starshipResolver {
func (r *QueryResolver) Starship(args struct{ ID graphql.ID }) *starshipResolver {
if s := starshipData[args.ID]; s != nil {
return &starshipResolver{s}
}
return nil
}

func (r *Resolver) CreateReview(args *struct {
func (r *MutationResolver) CreateReview(args *struct {
Episode string
Review *reviewInput
}) *reviewResolver {
Expand Down
2 changes: 1 addition & 1 deletion graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (r *timeResolver) AddHour(args struct{ Time graphql.Time }) graphql.Time {
return graphql.Time{Time: args.Time.Add(time.Hour)}
}

var starwarsSchema = graphql.MustParseSchema(starwars.Schema, &starwars.Resolver{})
var starwarsSchema = graphql.MustParseSchema(starwars.Schema, &starwars.RootResolver{})

func TestHelloWorld(t *testing.T) {
gqltesting.RunTests(t, []*gqltesting.Test{
Expand Down
11 changes: 10 additions & 1 deletion internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ func (r *Request) Execute(ctx context.Context, s *resolvable.Schema, op *query.O
func() {
defer r.handlePanic(ctx)
sels := selected.ApplyOperation(&r.Request, s, op)
r.execSelections(ctx, sels, nil, s.Resolver, &out, op.Type == query.Mutation)
var resolver reflect.Value
switch op.Type {
case query.Query:
resolver = s.QueryResolver
case query.Mutation:
resolver = s.MutationResolver
default:
panic("unknown query operation")
}
r.execSelections(ctx, sels, nil, resolver, &out, op.Type == query.Mutation)
}()

if err := ctx.Err(); err != nil {
Expand Down
41 changes: 31 additions & 10 deletions internal/exec/resolvable/resolvable.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import (
"reflect"
"strings"

"errors"

"github.com/graph-gophers/graphql-go/internal/common"
"github.com/graph-gophers/graphql-go/internal/exec/packer"
"github.com/graph-gophers/graphql-go/internal/schema"
)

type Schema struct {
schema.Schema
Query Resolvable
Mutation Resolvable
Resolver reflect.Value
Query Resolvable
Mutation Resolvable
QueryResolver reflect.Value
MutationResolver reflect.Value
}

type Resolvable interface {
Expand Down Expand Up @@ -57,16 +60,33 @@ func (*Scalar) isResolvable() {}
func ApplyResolver(s *schema.Schema, resolver interface{}) (*Schema, error) {
b := newBuilder(s)

var query, mutation Resolvable
root := reflect.ValueOf(resolver)

var (
query, mutation Resolvable
queryResolver, mutationResolver reflect.Value
)

if t, ok := s.EntryPoints["query"]; ok {
if err := b.assignExec(&query, t, reflect.TypeOf(resolver)); err != nil {
rFunc := root.MethodByName("Query")
if !rFunc.IsValid() {
return nil, errors.New("not found query resolver")
}

queryResolver = rFunc.Call(nil)[0]
if err := b.assignExec(&query, t, queryResolver.Type()); err != nil {
return nil, err
}
}

if t, ok := s.EntryPoints["mutation"]; ok {
if err := b.assignExec(&mutation, t, reflect.TypeOf(resolver)); err != nil {
rFunc := root.MethodByName("Mutation")
if !rFunc.IsValid() {
return nil, errors.New("not found mutation resolver")
}

mutationResolver = rFunc.Call(nil)[0]
if err := b.assignExec(&mutation, t, mutationResolver.Type()); err != nil {
return nil, err
}
}
Expand All @@ -76,10 +96,11 @@ func ApplyResolver(s *schema.Schema, resolver interface{}) (*Schema, error) {
}

return &Schema{
Schema: *s,
Resolver: reflect.ValueOf(resolver),
Query: query,
Mutation: mutation,
Schema: *s,
QueryResolver: queryResolver,
MutationResolver: mutationResolver,
Query: query,
Mutation: mutation,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion relay/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/graph-gophers/graphql-go/relay"
)

var starwarsSchema = graphql.MustParseSchema(starwars.Schema, &starwars.Resolver{})
var starwarsSchema = graphql.MustParseSchema(starwars.Schema, &starwars.RootResolver{})

func TestServeHTTP(t *testing.T) {
w := httptest.NewRecorder()
Expand Down

0 comments on commit df9ba5a

Please sign in to comment.