Skip to content

Commit

Permalink
Finish fleshing out the connection example
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Feb 5, 2018
1 parent e04b1e5 commit 9ab81d6
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 36 deletions.
16 changes: 7 additions & 9 deletions cmd/ggraphqlc/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,9 @@ func (e *extractor) introspect() error {
continue
}

e.findBindTargets(object.Type(), o)
// todo: break!
if e.findBindTargets(object.Type(), o) {
break
}
}
}

Expand All @@ -269,7 +270,7 @@ func (e *extractor) modifiersFromGoType(t types.Type) []string {
}
}

func (e *extractor) findBindTargets(t types.Type, object object) {
func (e *extractor) findBindTargets(t types.Type, object object) bool {
switch t := t.(type) {
case *types.Named:
for i := 0; i < t.NumMethods(); i++ {
Expand Down Expand Up @@ -306,6 +307,7 @@ func (e *extractor) findBindTargets(t types.Type, object object) {
}

e.findBindTargets(t.Underlying(), object)
return true

case *types.Struct:
for i := 0; i < t.NumFields(); i++ {
Expand All @@ -319,14 +321,10 @@ func (e *extractor) findBindTargets(t types.Type, object object) {
}
}
t.Underlying()

case *types.Signature:
// ignored

default:
panic(fmt.Errorf("unknown type %T looking at %s", t, object.Name))
return true
}

return false
}

const (
Expand Down
1 change: 0 additions & 1 deletion cmd/ggraphqlc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func main() {

"mapstructure": "github.com/mitchellh/mapstructure",
"errors": "github.com/vektah/graphql-go/errors",
"starwars": "github.com/vektah/graphql-go/example/starwars",
"introspection": "github.com/vektah/graphql-go/introspection",
"jsonw": "github.com/vektah/graphql-go/jsonw",
"query": "github.com/vektah/graphql-go/query",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gen

import (
"context"
Expand Down Expand Up @@ -143,13 +143,17 @@ func _friendsConnection(ec *executionContext, sel []query.Selection, it *starwar
switch field.Name {
case "totalCount":
ec.json.ObjectKey(field.Alias)
res := it.TotalCount
res := it.TotalCount()
ec.json.Int(res)
continue

case "edges":
ec.json.ObjectKey(field.Alias)
res := it.Edges
res, err := it.Edges()
if err != nil {
ec.Error(err)
continue
}
ec.json.BeginArray()
for _, val := range res {
_friendsEdge(ec, field.Selections, &val)
Expand All @@ -159,7 +163,11 @@ func _friendsConnection(ec *executionContext, sel []query.Selection, it *starwar

case "friends":
ec.json.ObjectKey(field.Alias)
res := it.Friends
res, err := it.Friends()
if err != nil {
ec.Error(err)
continue
}
ec.json.BeginArray()
for _, val := range res {
switch it := val.(type) {
Expand All @@ -182,7 +190,7 @@ func _friendsConnection(ec *executionContext, sel []query.Selection, it *starwar

case "pageInfo":
ec.json.ObjectKey(field.Alias)
res := it.PageInfo
res := it.PageInfo()
_pageInfo(ec, field.Selections, &res)
continue

Expand Down
3 changes: 2 additions & 1 deletion example/starwars/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/vektah/graphql-go/example/starwars"
"github.com/vektah/graphql-go/example/starwars/gen"
"github.com/vektah/graphql-go/relay"
)

Expand All @@ -13,7 +14,7 @@ func main() {
w.Write(page)
}))

http.Handle("/query", relay.Handler(NewResolver(starwars.NewResolver())))
http.Handle("/query", relay.Handler(gen.NewResolver(starwars.NewResolver())))

log.Fatal(http.ListenAndServe(":8080", nil))
}
Expand Down
91 changes: 80 additions & 11 deletions example/starwars/starwars.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
//go:generate ggraphqlc -out server/generated.go -package main
//go:generate ggraphqlc -out gen/generated.go

package starwars

import (
"context"
"encoding/base64"
"strconv"
"strings"

"fmt"
)

type Resolver struct {
Expand All @@ -31,9 +35,7 @@ func (r *Resolver) Human_friends(ctx context.Context, it *Human) ([]Character, e
}

func (r *Resolver) Human_friendsConnection(ctx context.Context, it *Human, first *int, after *string) (FriendsConnection, error) {
return FriendsConnection{
// todo
}, nil
return r.resolveFriendConnection(ctx, it.FriendIds, first, after)
}

func (r *Resolver) Human_starships(ctx context.Context, it *Human) ([]Starship, error) {
Expand All @@ -55,9 +57,7 @@ func (r *Resolver) Droid_friends(ctx context.Context, it *Droid) ([]Character, e
}

func (r *Resolver) Droid_friendsConnection(ctx context.Context, it *Droid, first *int, after *string) (FriendsConnection, error) {
return FriendsConnection{
// todo
}, nil
return r.resolveFriendConnection(ctx, it.FriendIds, first, after)
}

func (r *Resolver) Mutation_createReview(ctx context.Context, episode string, review Review) (*Review, error) {
Expand Down Expand Up @@ -249,11 +249,80 @@ type Droid struct {
type Character interface{}
type SearchResult interface{}

func (r *Resolver) resolveFriendConnection(ctx context.Context, ids []string, first *int, after *string) (FriendsConnection, error) {
from := 0
if after != nil {
b, err := base64.StdEncoding.DecodeString(*after)
if err != nil {
return FriendsConnection{}, err
}
i, err := strconv.Atoi(strings.TrimPrefix(string(b), "cursor"))
if err != nil {
return FriendsConnection{}, err
}
from = i
}

to := len(ids)
if first != nil {
to = from + *first
if to > len(ids) {
to = len(ids)
}
}

return FriendsConnection{
ctx: ctx,
r: r,
ids: ids,
from: from,
to: to,
}, nil
}

type FriendsConnection struct {
TotalCount int
Edges []FriendsEdge
Friends []Character
PageInfo PageInfo
r *Resolver
ctx context.Context
ids []string
from int
to int
}

func (f *FriendsConnection) Edges() ([]FriendsEdge, error) {
friends, err := f.r.resolveCharacters(f.ctx, f.ids)
if err != nil {
return nil, err
}

edges := make([]FriendsEdge, f.to-f.from)
for i := range edges {
edges[i] = FriendsEdge{
Cursor: encodeCursor(f.from + i),
Node: friends[i],
}
}
return edges, nil
}

func (f *FriendsConnection) TotalCount() int {
return len(f.ids)
}

func (f *FriendsConnection) PageInfo() PageInfo {
return PageInfo{
StartCursor: encodeCursor(f.from),
EndCursor: encodeCursor(f.to - 1),
HasNextPage: f.to < len(f.ids),
}
}

func encodeCursor(i int) string {
return base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("cursor%d", i+1)))
}

// A list of the friends, as a convenience when edges are not needed.
func (f *FriendsConnection) Friends() ([]Character, error) {
return f.r.resolveCharacters(f.ctx, f.ids)
}

type FriendsEdge struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package main
package gen

import (
"context"
"fmt"
"github.com/mitchellh/mapstructure"
"github.com/vektah/graphql-go/errors"
"github.com/vektah/graphql-go/example/starwars"
"github.com/vektah/graphql-go/example/todo"
"github.com/vektah/graphql-go/introspection"
"github.com/vektah/graphql-go/jsonw"
Expand Down
3 changes: 2 additions & 1 deletion example/todo/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/vektah/graphql-go/example/todo"
"github.com/vektah/graphql-go/example/todo/gen"
"github.com/vektah/graphql-go/relay"
)

Expand All @@ -13,7 +14,7 @@ func main() {
w.Write(page)
}))

http.Handle("/query", relay.Handler(NewResolver(todo.NewResolver())))
http.Handle("/query", relay.Handler(gen.NewResolver(todo.NewResolver())))

log.Fatal(http.ListenAndServe(":8080", nil))
}
Expand Down
12 changes: 6 additions & 6 deletions example/todo/todo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate ggraphqlc -out server/generated.go -package main
//go:generate ggraphqlc -out gen/generated.go

package todo

Expand Down Expand Up @@ -30,7 +30,7 @@ func NewResolver() *TodoResolver {
}
}

func (r *TodoResolver) Query_todo(ctx context.Context, id int) (*Todo, error) {
func (r *TodoResolver) MyQuery_todo(ctx context.Context, id int) (*Todo, error) {
for _, todo := range r.todos {
if todo.ID == id {
return &todo, nil
Expand All @@ -39,18 +39,18 @@ func (r *TodoResolver) Query_todo(ctx context.Context, id int) (*Todo, error) {
return nil, errors.New("not found")
}

func (r *TodoResolver) Query_lastTodo(ctx context.Context) (*Todo, error) {
func (r *TodoResolver) MyQuery_lastTodo(ctx context.Context) (*Todo, error) {
if len(r.todos) == 0 {
return nil, errors.New("not found")
}
return &r.todos[len(r.todos)-1], nil
}

func (r *TodoResolver) Query_todos(ctx context.Context) ([]Todo, error) {
func (r *TodoResolver) MyQuery_todos(ctx context.Context) ([]Todo, error) {
return r.todos, nil
}

func (r *TodoResolver) Mutation_createTodo(ctx context.Context, text string) (Todo, error) {
func (r *TodoResolver) MyMutation_createTodo(ctx context.Context, text string) (Todo, error) {
newID := r.id()

newTodo := Todo{
Expand All @@ -64,7 +64,7 @@ func (r *TodoResolver) Mutation_createTodo(ctx context.Context, text string) (To
return newTodo, nil
}

func (r *TodoResolver) Mutation_updateTodo(ctx context.Context, id int, done bool) (Todo, error) {
func (r *TodoResolver) MyMutation_updateTodo(ctx context.Context, id int, done bool) (Todo, error) {
var affectedTodo *Todo

for i := 0; i < len(r.todos); i++ {
Expand Down

0 comments on commit 9ab81d6

Please sign in to comment.