Skip to content

Commit

Permalink
Update test & examples to use new resolver pattern
Browse files Browse the repository at this point in the history
* chat
* dataloader
* scalar
* selection
* starwars
* todo
  • Loading branch information
creativej committed Jul 23, 2018
1 parent 3870896 commit 1ba61fc
Show file tree
Hide file tree
Showing 23 changed files with 267 additions and 460 deletions.
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.

34 changes: 26 additions & 8 deletions example/dataloader/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,37 @@ type Order struct {

type Resolver struct{}

func (r *Resolver) Customer_address(ctx context.Context, it *Customer) (*Address, error) {
return ctxLoaders(ctx).addressByID.Load(it.AddressID)
func (r *Resolver) Customer() CustomerResolver {
return &customerResolver{r}
}

func (r *Resolver) Customer_orders(ctx context.Context, it *Customer) ([]Order, error) {
return ctxLoaders(ctx).ordersByCustomer.Load(it.ID)
func (r *Resolver) Order() OrderResolver {
return &orderResolver{r}
}

func (r *Resolver) Order_items(ctx context.Context, it *Order) ([]Item, error) {
return ctxLoaders(ctx).itemsByOrder.Load(it.ID)
func (r *Resolver) Query() QueryResolver {
return &queryResolver{r}
}

func (r *Resolver) Query_customers(ctx context.Context) ([]Customer, error) {
type customerResolver struct{ *Resolver }

func (r *customerResolver) Address(ctx context.Context, obj *Customer) (*Address, error) {
return ctxLoaders(ctx).addressByID.Load(obj.AddressID)
}

func (r *customerResolver) Orders(ctx context.Context, obj *Customer) ([]Order, error) {
return ctxLoaders(ctx).ordersByCustomer.Load(obj.ID)
}

type orderResolver struct{ *Resolver }

func (r *orderResolver) Items(ctx context.Context, obj *Order) ([]Item, error) {
return ctxLoaders(ctx).itemsByOrder.Load(obj.ID)
}

type queryResolver struct{ *Resolver }

func (r *queryResolver) Customers(ctx context.Context) ([]Customer, error) {
fmt.Println("SELECT * FROM customer")

time.Sleep(5 * time.Millisecond)
Expand All @@ -47,7 +65,7 @@ func (r *Resolver) Query_customers(ctx context.Context) ([]Customer, error) {
}

// this method is here to test code generation of nested arrays
func (r *Resolver) Query_torture(ctx context.Context, customerIds [][]int) ([][]Customer, error) {
func (r *queryResolver) Torture(ctx context.Context, customerIds [][]int) ([][]Customer, error) {
result := make([][]Customer, len(customerIds))
for i := range customerIds {
inner := make([]Customer, len(customerIds[i]))
Expand Down
2 changes: 1 addition & 1 deletion example/dataloader/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {

router.Handle("/", handler.Playground("Dataloader", "/query"))
router.Handle("/query", handler.GraphQL(
dataloader.MakeExecutableSchema(&dataloader.Resolver{}),
dataloader.NewExecutableSchema(&dataloader.Resolver{}),
handler.ResolverMiddleware(gqlopentracing.ResolverMiddleware()),
handler.RequestMiddleware(gqlopentracing.RequestMiddleware()),
))
Expand Down
Loading

0 comments on commit 1ba61fc

Please sign in to comment.