Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove old resolvers #218

Merged
merged 3 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,16 @@ func (f *Field) IsResolver() bool {
func (f *Field) IsConcurrent() bool {
return f.IsResolver() && !f.Object.DisableConcurrency
}

func (f *Field) ShortInvocation() string {
if !f.IsResolver() {
return ""
}
shortName := strings.ToUpper(f.GQLName[:1]) + f.GQLName[1:]
res := fmt.Sprintf("%s().%s(ctx", f.Object.GQLType, shortName)
if !f.Object.Root {
res += fmt.Sprintf(", obj")
}
for _, arg := range f.Args {
res += fmt.Sprintf(", %s", arg.GoVarName)
}
res += ")"
return res

return fmt.Sprintf("%s().%s(%s)", f.Object.GQLType, shortName, f.CallArgs())
}

func (f *Field) ShortResolverDeclaration() string {
if !f.IsResolver() {
return ""
Expand Down
4 changes: 2 additions & 2 deletions codegen/templates/data.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions codegen/templates/field.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
func (ec *executionContext) _{{$object.GQLType}}_{{$field.GQLName}}(ctx context.Context, field graphql.CollectedField) func() graphql.Marshaler {
{{- template "args.gotpl" $field.Args }}
ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{Field: field})
results, err := ec.resolvers.{{ $object.GQLType }}_{{ $field.GQLName }}({{ $field.CallArgs }})
results, err := ec.resolvers.{{ $field.ShortInvocation }}
if err != nil {
ec.Error(ctx, err)
return nil
Expand Down Expand Up @@ -49,7 +49,7 @@

{{- if $field.IsResolver }}
resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) {
return ec.resolvers.{{ $object.GQLType }}_{{ $field.GQLName }}({{ $field.CallArgs }})
return ec.resolvers.{{ $field.ShortInvocation }}
})
if err != nil {
ec.Error(ctx, err)
Expand Down
33 changes: 3 additions & 30 deletions codegen/templates/generated.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,9 @@ import (
{{ end }}
)

// MakeExecutableSchema creates an ExecutableSchema from the Resolvers interface.
func MakeExecutableSchema(resolvers Resolvers) graphql.ExecutableSchema {
return &executableSchema{resolvers: resolvers}
}

// NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface.
func NewExecutableSchema(resolvers ResolverRoot) graphql.ExecutableSchema {
return MakeExecutableSchema(shortMapper{r: resolvers})
}

type Resolvers interface {
{{- range $object := .Objects -}}
{{ range $field := $object.Fields -}}
{{ $field.ResolverDeclaration }}
{{ end }}
{{- end }}
return &executableSchema{resolvers: resolvers}
}

type ResolverRoot interface {
Expand All @@ -44,22 +31,8 @@ type ResolverRoot interface {
{{- end }}
{{- end }}

type shortMapper struct {
r ResolverRoot
}

{{- range $object := .Objects -}}
{{ range $field := $object.Fields -}}
{{- if $field.IsResolver }}
func (s shortMapper) {{ $field.ResolverDeclaration }} {
return s.r.{{$field.ShortInvocation}}
}
{{- end }}
{{ end }}
{{- end }}

type executableSchema struct {
resolvers Resolvers
resolvers ResolverRoot
}

func (e *executableSchema) Schema() *ast.Schema {
Expand Down Expand Up @@ -141,7 +114,7 @@ func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDe
type executionContext struct {
*graphql.RequestContext

resolvers Resolvers
resolvers ResolverRoot
}

{{- range $object := .Objects }}
Expand Down
2 changes: 1 addition & 1 deletion example/chat/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestChat(t *testing.T) {
srv := httptest.NewServer(handler.GraphQL(MakeExecutableSchema(New())))
srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(New())))
c := client.New(srv.URL)

t.Run("subscribe to chat events", func(t *testing.T) {
Expand Down
40 changes: 6 additions & 34 deletions example/chat/generated.go

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

34 changes: 26 additions & 8 deletions example/chat/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,25 @@ import (
"time"
)

type resolvers struct {
type resolver struct {
Rooms map[string]*Chatroom
mu sync.Mutex
mu sync.Mutex // nolint: structcheck
}

func New() *resolvers {
return &resolvers{
func (r *resolver) Mutation() MutationResolver {
return &mutationResolver{r}
}

func (r *resolver) Query() QueryResolver {
return &queryResolver{r}
}

func (r *resolver) Subscription() SubscriptionResolver {
return &subscriptionResolver{r}
}

func New() *resolver {
return &resolver{
Rooms: map[string]*Chatroom{},
}
}
Expand All @@ -26,7 +38,9 @@ type Chatroom struct {
Observers map[string]chan Message
}

func (r *resolvers) Mutation_post(ctx context.Context, text string, userName string, roomName string) (Message, error) {
type mutationResolver struct{ *resolver }

func (r *mutationResolver) Post(ctx context.Context, text string, username string, roomName string) (Message, error) {
r.mu.Lock()
room := r.Rooms[roomName]
if room == nil {
Expand All @@ -39,7 +53,7 @@ func (r *resolvers) Mutation_post(ctx context.Context, text string, userName str
ID: randString(8),
CreatedAt: time.Now(),
Text: text,
CreatedBy: userName,
CreatedBy: username,
}

room.Messages = append(room.Messages, message)
Expand All @@ -51,7 +65,9 @@ func (r *resolvers) Mutation_post(ctx context.Context, text string, userName str
return message, nil
}

func (r *resolvers) Query_room(ctx context.Context, name string) (*Chatroom, error) {
type queryResolver struct{ *resolver }

func (r *queryResolver) Room(ctx context.Context, name string) (*Chatroom, error) {
r.mu.Lock()
room := r.Rooms[name]
if room == nil {
Expand All @@ -63,7 +79,9 @@ func (r *resolvers) Query_room(ctx context.Context, name string) (*Chatroom, err
return room, nil
}

func (r *resolvers) Subscription_messageAdded(ctx context.Context, roomName string) (<-chan Message, error) {
type subscriptionResolver struct{ *resolver }

func (r *subscriptionResolver) MessageAdded(ctx context.Context, roomName string) (<-chan Message, error) {
r.mu.Lock()
room := r.Rooms[roomName]
if room == nil {
Expand Down
2 changes: 1 addition & 1 deletion example/chat/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {
startAppdashServer()

http.Handle("/", handler.Playground("Todo", "/query"))
http.Handle("/query", handler.GraphQL(chat.MakeExecutableSchema(chat.New()),
http.Handle("/query", handler.GraphQL(chat.NewExecutableSchema(chat.New()),
handler.ResolverMiddleware(gqlopentracing.ResolverMiddleware()),
handler.RequestMiddleware(gqlopentracing.RequestMiddleware()),
handler.WebsocketUpgrader(websocket.Upgrader{
Expand Down
2 changes: 1 addition & 1 deletion example/dataloader/dataloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestTodo(t *testing.T) {
srv := httptest.NewServer(LoaderMiddleware(handler.GraphQL(MakeExecutableSchema(&Resolver{}))))
srv := httptest.NewServer(LoaderMiddleware(handler.GraphQL(NewExecutableSchema(&Resolver{}))))
c := client.New(srv.URL)

t.Run("create a new todo", func(t *testing.T) {
Expand Down
54 changes: 8 additions & 46 deletions example/dataloader/generated.go

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

Loading