https://github.com/landrade/gqlgen-cache-control-plugin
cache-control plugin for Gqlgen
https://github.com/landrade/gqlgen-cache-control-plugin
cache-control golang gqlgen graphql
Last synced: about 1 month ago
JSON representation
cache-control plugin for Gqlgen
- Host: GitHub
- URL: https://github.com/landrade/gqlgen-cache-control-plugin
- Owner: landrade
- License: mit
- Created: 2022-05-14T22:55:43.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-08-15T19:59:44.000Z (over 1 year ago)
- Last Synced: 2024-08-15T21:31:43.388Z (over 1 year ago)
- Topics: cache-control, golang, gqlgen, graphql
- Language: Go
- Homepage:
- Size: 34.2 KB
- Stars: 5
- Watchers: 2
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cache control plugin for GQLGEN
Plugin for gqlgen (https://gqlgen.com/) to write cache-control directives. With this plugin, you can write a cache-control HTTP header and cache-control extension, following Apollo's rules. Refs:
- https://www.apollographql.com/docs/apollo-server/performance/caching/
- https://github.com/apollographql/apollo-server/tree/d5015f4ea00cadb2a74b09956344e6f65c084629/packages/apollo-cache-control
### Installation
Install Go module:
```bash
$ go get github.com/landrade/gqlgen-cache-control-plugin
```
### Configuring the plugin
```go
package main
import (
"github.com/landrade/gqlgen-cache-control-plugin/cache"
"github.com/99designs/gqlgen/graphql/handler"
)
func main() {
// Building your server
cfg := generated.Config{Resolvers: &graph.Resolver{}}
srv := handler.NewDefaultServer(generated.NewExecutableSchema(cfg))
// Enable cache extensions
srv.Use(cache.Extension{}) // <----
//...
}
```
### Set cache hints
After you enable `cache.Extension`, you can set cache hints using `cache.SetHint` function in your resolvers.
```go
import (
"github.com/landrade/gqlgen-cache-control-plugin/cache"
// ...
)
func (r *commentResolver) Post(ctx context.Context, obj *model.Comment) (*model.Post, error) {
post, err := // getting post by comment
if err != nil {
return nil, err
}
// Set a CacheHint
cache.SetHint(ctx, cache.ScopePublic, 10*time.Second)
return post, nil
}
```
### CDN Caching
It's possible to enable the Gqlgen to provide a `Cache-Control` header based on your cache hints in `GET` or `POST` requests.
To do it you need wrap your server using `cache.Middleware` function. It will add `Cache-Control` header to all responses that have set cache.
```go
func main() {
// ... setup server
srv = cache.Middleware(srv)
// ... do more things
}
````
Doing it, Gqlgen write the lowest max-age defined in cacheControl extensions.
For more informations, see `_example` folder.