From 04b120c9044a20ac8c14f8329c529862ccdc6b6f Mon Sep 17 00:00:00 2001 From: Franky W Date: Mon, 30 Mar 2020 19:31:59 -0700 Subject: [PATCH] Update APQ example to reflect newer API 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 --- docs/content/reference/apq.md | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/docs/content/reference/apq.md b/docs/content/reference/apq.md index 79c98b55eb9..6ea571be963 100644 --- a/docs/content/reference/apq.md +++ b/docs/content/reference/apq.md @@ -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 { @@ -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 } @@ -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) } ```