Skip to content

Commit

Permalink
add SUBSCRIPTION Directive
Browse files Browse the repository at this point in the history
  • Loading branch information
asamusev committed Jun 14, 2019
1 parent 32462d0 commit f32571e
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 16 deletions.
18 changes: 12 additions & 6 deletions codegen/generated!.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDe
{{- if .SubscriptionRoot }}
ec := executionContext{graphql.GetRequestContext(ctx), e}

{{ if .MutationDirectives -}}
{{ if .SubscriptionDirectives -}}
next := ec._{{.SubscriptionRoot.Name}}Middleware(ctx, op)
{{- else -}}
next := ec._{{.SubscriptionRoot.Name}}(ctx, op.SelectionSet)
Expand Down Expand Up @@ -247,7 +247,7 @@ func (ec *executionContext) _{{.QueryRoot.Name}}Middleware(ctx context.Context,
{{end}}

{{ if and .SubscriptionDirectives .SubscriptionRoot }}
func (ec *executionContext) _{{.SubscriptionRoot.Name}}Middleware(ctx context.Context, obj *ast.OperationDefinition) graphql.Marshaler {
func (ec *executionContext) _{{.SubscriptionRoot.Name}}Middleware(ctx context.Context, obj *ast.OperationDefinition) func() graphql.Marshaler {

next := func(ctx context.Context) (interface{}, error){
return ec._{{.SubscriptionRoot.Name}}(ctx, obj.SelectionSet),nil
Expand All @@ -261,7 +261,9 @@ func (ec *executionContext) _{{.SubscriptionRoot.Name}}Middleware(ctx context.Co
args, err := ec.{{ $directive.ArgsFunc }}(ctx,rawArgs)
if err != nil {
ec.Error(ctx, err)
return graphql.Null
return func() graphql.Marshaler {
return graphql.Null
}
}
{{- end }}
n := next
Expand All @@ -274,13 +276,17 @@ func (ec *executionContext) _{{.SubscriptionRoot.Name}}Middleware(ctx context.Co
tmp, err := next(ctx)
if err != nil {
ec.Error(ctx, err)
return graphql.Null
return func() graphql.Marshaler {
return graphql.Null
}
}
if data, ok := tmp.(graphql.Marshaler); ok {
if data, ok := tmp.(func() graphql.Marshaler); ok {
return data
}
ec.Errorf(ctx, `unexpected type %T from directive, should be graphql.Marshaler`, tmp)
return graphql.Null
return func() graphql.Marshaler {
return graphql.Null
}
}
{{end}}

Expand Down
5 changes: 3 additions & 2 deletions example/chat/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ func TestChatSubscriptions(t *testing.T) {
srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(New())))
c := client.New(srv.URL)

sub := c.Websocket(`subscription { messageAdded(roomName:"#gophers") { text createdBy } }`)
sub := c.Websocket(`subscription @user(username:"vektah") { messageAdded(roomName:"#gophers") { text createdBy } }`)
defer sub.Close()

go func() {
var resp interface{}
time.Sleep(10 * time.Millisecond)
err := c.Post(`mutation {
a:post(text:"Hello!", roomName:"#gophers", username:"vektah") { id }
b:post(text:"Whats up?", roomName:"#gophers", username:"vektah") { id }
b:post(text:"Hello Vektah!", roomName:"#gophers", username:"andrey") { id }
c:post(text:"Whats up?", roomName:"#gophers", username:"vektah") { id }
}`, &resp)
assert.NoError(t, err)
}()
Expand Down
75 changes: 74 additions & 1 deletion example/chat/generated.go

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

54 changes: 47 additions & 7 deletions example/chat/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
package chat

import (
context "context"
"context"
"math/rand"
"sync"
"time"

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

type resolver struct {
Expand All @@ -31,13 +33,28 @@ func New() Config {
Resolvers: &resolver{
Rooms: map[string]*Chatroom{},
},
Directives: DirectiveRoot{
User: func(ctx context.Context, obj interface{}, next graphql.Resolver, username string) (res interface{}, err error) {
return next(context.WithValue(ctx, "username", username))
},
},
}
}

func getUsername(ctx context.Context) string {
if username, ok := ctx.Value("username").(string); ok {
return username
}
return ""
}

type Chatroom struct {
Name string
Messages []Message
Observers map[string]chan *Message
Observers map[string]struct {
Username string
Message chan *Message
}
}

type mutationResolver struct{ *resolver }
Expand All @@ -46,7 +63,13 @@ func (r *mutationResolver) Post(ctx context.Context, text string, username strin
r.mu.Lock()
room := r.Rooms[roomName]
if room == nil {
room = &Chatroom{Name: roomName, Observers: map[string]chan *Message{}}
room = &Chatroom{
Name: roomName,
Observers: map[string]struct {
Username string
Message chan *Message
}{},
}
r.Rooms[roomName] = room
}
r.mu.Unlock()
Expand All @@ -61,7 +84,9 @@ func (r *mutationResolver) Post(ctx context.Context, text string, username strin
room.Messages = append(room.Messages, message)
r.mu.Lock()
for _, observer := range room.Observers {
observer <- &message
if observer.Username == "" || observer.Username == message.CreatedBy {
observer.Message <- &message
}
}
r.mu.Unlock()
return &message, nil
Expand All @@ -73,7 +98,13 @@ func (r *queryResolver) Room(ctx context.Context, name string) (*Chatroom, error
r.mu.Lock()
room := r.Rooms[name]
if room == nil {
room = &Chatroom{Name: name, Observers: map[string]chan *Message{}}
room = &Chatroom{
Name: name,
Observers: map[string]struct {
Username string
Message chan *Message
}{},
}
r.Rooms[name] = room
}
r.mu.Unlock()
Expand All @@ -87,7 +118,13 @@ func (r *subscriptionResolver) MessageAdded(ctx context.Context, roomName string
r.mu.Lock()
room := r.Rooms[roomName]
if room == nil {
room = &Chatroom{Name: roomName, Observers: map[string]chan *Message{}}
room = &Chatroom{
Name: roomName,
Observers: map[string]struct {
Username string
Message chan *Message
}{},
}
r.Rooms[roomName] = room
}
r.mu.Unlock()
Expand All @@ -103,7 +140,10 @@ func (r *subscriptionResolver) MessageAdded(ctx context.Context, roomName string
}()

r.mu.Lock()
room.Observers[id] = events
room.Observers[id] = struct {
Username string
Message chan *Message
}{Username: getUsername(ctx), Message: events}
r.mu.Unlock()

return events, nil
Expand Down
2 changes: 2 additions & 0 deletions example/chat/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ type Subscription {
}

scalar Time

directive @user(username: String!) on SUBSCRIPTION

0 comments on commit f32571e

Please sign in to comment.