Skip to content

Commit

Permalink
Merge pull request #1124 from OpenSourceProjects/update-apq-documenta…
Browse files Browse the repository at this point in the history
…tion

Update APQ example to reflect newer API
  • Loading branch information
vektah authored Apr 24, 2020
2 parents 3c126f9 + 04b120c commit ace558b
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 ace558b

Please sign in to comment.