dataloaders_to_context shouldn't cache servers, instead it should fetch only two columns from database: lang_version_tag and key

This commit is contained in:
Dawid Wysokiński 2020-08-07 10:09:45 +02:00
parent 0779ae4658
commit 7d2f75db52
4 changed files with 17 additions and 19 deletions

View File

@ -13,7 +13,6 @@ import (
"github.com/go-redis/redis/v8"
"github.com/pkg/errors"
"github.com/tribalwarshelp/shared/cache/allservers"
"github.com/tribalwarshelp/shared/mode"
httpdelivery "github.com/tribalwarshelp/api/graphql/delivery/http"
@ -92,7 +91,6 @@ func main() {
log.Fatal(err)
}
}()
allServersCache := allservers.New(redisClient)
langversionRepo, err := langversionrepo.NewPGRepository(db)
if err != nil {
@ -137,8 +135,7 @@ func main() {
})
graphql := router.Group("")
graphql.Use(middleware.DataLoadersToContext(middleware.DataLoadersToContextConfig{
ServerRepo: serverRepo,
AllServersCache: allServersCache,
ServerRepo: serverRepo,
},
dataloaders.Config{
PlayerRepo: playerRepo,

View File

@ -8,7 +8,7 @@ import (
"github.com/tribalwarshelp/api/graphql/dataloaders"
"github.com/tribalwarshelp/api/server"
"github.com/tribalwarshelp/shared/cache/allservers"
"github.com/tribalwarshelp/api/utils"
"github.com/tribalwarshelp/shared/models"
"github.com/gin-gonic/gin"
@ -19,8 +19,7 @@ var langVersionLoadersContextKey ContextKey = "langVersionLoaders"
var dataloadersContextKey ContextKey = "dataloaders"
type DataLoadersToContextConfig struct {
ServerRepo server.Repository
AllServersCache allservers.Cache
ServerRepo server.Repository
}
func DataLoadersToContext(dltcc DataLoadersToContextConfig, cfg dataloaders.Config) gin.HandlerFunc {
@ -28,18 +27,15 @@ func DataLoadersToContext(dltcc DataLoadersToContextConfig, cfg dataloaders.Conf
ctx := c.Request.Context()
serverDataLoaders := make(map[string]*dataloaders.ServerDataLoaders)
langVersionDataLoaders := make(map[models.LanguageTag]*dataloaders.LangVersionDataLoaders)
servers, ok := dltcc.AllServersCache.Get()
if !ok {
var err error
servers, _, err = dltcc.ServerRepo.Fetch(c.Request.Context(), server.FetchConfig{})
if err != nil {
c.JSON(http.StatusOK, &gqlerror.Error{
Message: err.Error(),
})
c.Abort()
return
}
go dltcc.AllServersCache.Set(servers)
servers, _, err := dltcc.ServerRepo.Fetch(c.Request.Context(), server.FetchConfig{
Columns: []string{utils.Underscore("langVersionTag"), "key"},
})
if err != nil {
c.JSON(http.StatusOK, &gqlerror.Error{
Message: err.Error(),
})
c.Abort()
return
}
for _, server := range servers {
serverDataLoaders[server.Key] = dataloaders.NewServerDataLoaders(server.Key, cfg)

View File

@ -8,6 +8,7 @@ import (
type FetchConfig struct {
Filter *models.ServerFilter
Columns []string
Count bool
}

View File

@ -40,6 +40,10 @@ func (repo *pgRepository) Fetch(ctx context.Context, cfg server.FetchConfig) ([]
}
}
if len(cfg.Columns) > 0 {
query = query.Column(cfg.Columns...)
}
if cfg.Count {
total, err = query.SelectAndCount()
} else {