Skip to content

Commit

Permalink
Update APQ example to reflect newer API
Browse files Browse the repository at this point in the history
The example in APQ relates to the old handlers. This brings it up to
show how extensions can be used - and uses the new API for registering
plugins that come in the graph.

The cache example now implements the graphql.Cache interface
  • Loading branch information
frankywahl committed Mar 31, 2020
1 parent a1a0261 commit 04b120c
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions docs/content/reference/apq.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import (
"context"
"time"

"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/go-redis/redis"
"github.com/pkg/errors"
)

type Cache struct {
Expand All @@ -43,20 +45,20 @@ func NewCache(redisAddress string, password string, ttl time.Duration) (*Cache,

err := client.Ping().Err()
if err != nil {
return nil, errors.WithStack(err)
return nil, fmt.Errorf("could not create cache: %w", err)
}

return &Cache{client: client, ttl: ttl}, nil
}

func (c *Cache) Add(ctx context.Context, hash string, query string) {
c.client.Set(apqPrefix + hash, query, c.ttl)
func (c *Cache) Add(ctx context.Context, key string, value interface{}) {
c.client.Set(apqPrefix+key, value, c.ttl)
}

func (c *Cache) Get(ctx context.Context, hash string) (string, bool) {
s, err := c.client.Get(apqPrefix + hash).Result()
func (c *Cache) Get(ctx context.Context, key string) (interface{}, bool) {
s, err := c.client.Get(apqPrefix + key).Result()
if err != nil {
return "", false
return struct{}{}, false
}
return s, true
}
Expand All @@ -68,10 +70,11 @@ func main() {
}

c := Config{ Resolvers: &resolvers{} }
gqlHandler := handler.GraphQL(
blog.NewExecutableSchema(c),
handler.EnablePersistedQueryCache(cache),
gqlHandler := handler.New(
generated.NewExecutableSchema(c),
)
gqlHandler.AddTransport(transport.POST{})
gqlHandler.Use(extension.AutomaticPersistedQuery{Cache: cache})
http.Handle("/query", gqlHandler)
}
```

0 comments on commit 04b120c

Please sign in to comment.