mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-17 01:17:43 +00:00
Add environment variables to steer the query cache.
This commit is contained in:
@@ -23,12 +23,15 @@ Therefore, I present you the simple client to which you can copy & paste your gr
|
|||||||
* Executing GraphQL queries as they are, without types declaration
|
* Executing GraphQL queries as they are, without types declaration
|
||||||
* HTTP2 support!
|
* HTTP2 support!
|
||||||
* Support for additional headers
|
* Support for additional headers
|
||||||
|
* Query cache
|
||||||
|
|
||||||
## Usage example
|
## Usage example
|
||||||
|
|
||||||
### Environment variables
|
### Environment variables
|
||||||
|
|
||||||
* `GRAPHQL_ENDPOINT` - Your GraphQL endpoint. Default: `http://127.0.0.1:9090/v1/graphql`
|
* `GRAPHQL_ENDPOINT` - Your GraphQL endpoint. Default: `http://127.0.0.1:9090/v1/graphql`
|
||||||
|
* `GRAPHQL_CACHE` - Should the query cache be enabled? Default: `false`
|
||||||
|
* `GRAPHQL_CACHE_TTL` - Cache TTL in seconds for SELECT type of queries. Default: `5`
|
||||||
* `LOG_LEVEL` - Logging level. Default: `info`
|
* `LOG_LEVEL` - Logging level. Default: `info`
|
||||||
|
|
||||||
### Example reader code
|
### Example reader code
|
||||||
@@ -37,7 +40,7 @@ Therefore, I present you the simple client to which you can copy & paste your gr
|
|||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
fmt
|
fmt
|
||||||
gql "github.com/lukaszraczylo/simple-gql-client"
|
gql "github.com/lukaszraczylo/go-simple-graphql"
|
||||||
)
|
)
|
||||||
|
|
||||||
headers := map[string]interface{}{
|
headers := map[string]interface{}{
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/allegro/bigcache/v3"
|
"github.com/allegro/bigcache/v3"
|
||||||
@@ -49,6 +50,32 @@ func pickGraphqlEndpoint() (graphqlEndpoint string) {
|
|||||||
return graphqlEndpoint
|
return graphqlEndpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setCacheTTL() int {
|
||||||
|
value, present := os.LookupEnv("GRAPHQL_CACHE_TTL")
|
||||||
|
if present && value != "" {
|
||||||
|
i, err := strconv.Atoi(value)
|
||||||
|
if err != nil {
|
||||||
|
panic("Invalid value for query cache ttl")
|
||||||
|
}
|
||||||
|
return int(i)
|
||||||
|
} else {
|
||||||
|
return 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func setCacheEnabled() bool {
|
||||||
|
value, present := os.LookupEnv("GRAPHQL_CACHE")
|
||||||
|
if present && value != "" {
|
||||||
|
b, err := strconv.ParseBool(value)
|
||||||
|
if err != nil {
|
||||||
|
panic("Invalid value for query cache")
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func NewConnection() *GraphQL {
|
func NewConnection() *GraphQL {
|
||||||
return &GraphQL{
|
return &GraphQL{
|
||||||
Endpoint: pickGraphqlEndpoint(),
|
Endpoint: pickGraphqlEndpoint(),
|
||||||
@@ -63,13 +90,13 @@ func NewConnection() *GraphQL {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
Log: logging.NewLogger(),
|
Log: logging.NewLogger(),
|
||||||
Cache: false,
|
Cache: setCacheEnabled(),
|
||||||
CacheStore: setupCache(),
|
CacheStore: setupCache(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupCache() *bigcache.BigCache {
|
func setupCache() *bigcache.BigCache {
|
||||||
cache, err := bigcache.NewBigCache(bigcache.DefaultConfig(10 * time.Second))
|
cache, err := bigcache.NewBigCache(bigcache.DefaultConfig(time.Duration(setCacheTTL()) * time.Second))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Error creating cache: " + err.Error())
|
panic("Error creating cache: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user