github.com/bewolv/gqlgen@v0.10.12/docs/content/reference/apq.md (about)

     1  ---
     2  title: "Automatic persisted queries"
     3  description:   
     4  linkTitle: "APQ"
     5  menu: { main: { parent: 'reference' } }
     6  ---
     7  
     8  When you work with GraphQL by default your queries are transferred with every request. That can waste significant
     9  bandwidth. To avoid that you can use Automatic Persisted Queries (APQ).
    10  
    11  With APQ you send only query hash to the server. If hash is not found on a server then client makes a second request
    12  to register query hash with original query on a server.
    13  
    14  ## Usage
    15  
    16  In order to enable Automatic Persisted Queries you need to change your client. For more information see 
    17  [Automatic Persisted Queries Link](https://github.com/apollographql/apollo-link-persisted-queries) documentation.
    18  
    19  For the server you need to implement `PersistedQueryCache` interface and pass instance to 
    20  `handler.EnablePersistedQueryCache` option.
    21  
    22  See example using [go-redis](https://github.com/go-redis/redis) package below:
    23  ```go
    24  import (
    25  	"context"
    26  	"time"
    27  
    28  	"github.com/go-redis/redis"
    29  	"github.com/pkg/errors"
    30  )
    31  
    32  type Cache struct {
    33  	client redis.UniversalClient
    34  	ttl    time.Duration
    35  }
    36  
    37  const apqPrefix = "apq:"
    38  
    39  func NewCache(redisAddress string, password string, ttl time.Duration) (*Cache, error) {
    40  	client := redis.NewClient(&redis.Options{
    41  		Addr:     redisAddress,
    42  	})
    43  
    44  	err := client.Ping().Err()
    45  	if err != nil {
    46  		return nil, errors.WithStack(err)
    47  	}
    48  
    49  	return &Cache{client: client, ttl: ttl}, nil
    50  }
    51  
    52  func (c *Cache) Add(ctx context.Context, hash string, query string) {
    53  	c.client.Set(apqPrefix + hash, query, c.ttl)
    54  }
    55  
    56  func (c *Cache) Get(ctx context.Context, hash string) (string, bool) {
    57  	s, err := c.client.Get(apqPrefix + hash).Result()
    58  	if err != nil {
    59  		return "", false
    60  	}
    61  	return s, true
    62  }
    63  
    64  func main() {
    65  	cache, err := NewCache(cfg.RedisAddress, 24*time.Hour)
    66  	if err != nil {
    67  		log.Fatalf("cannot create APQ redis cache: %v", err)
    68  	}
    69  	
    70  	c := Config{ Resolvers: &resolvers{} }
    71  	gqlHandler := handler.GraphQL(
    72  		blog.NewExecutableSchema(c),
    73  		handler.EnablePersistedQueryCache(cache),
    74  	)
    75  	http.Handle("/query", gqlHandler)
    76  }
    77  ```