Skip to content

Commit

Permalink
feat: improve performance when creating caching request.
Browse files Browse the repository at this point in the history
  • Loading branch information
vuongxuongminh committed Apr 21, 2022
1 parent aba5ed7 commit e2354e0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 42 deletions.
4 changes: 2 additions & 2 deletions caching_purger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"strconv"
)

func (c *Caching) purgeQueryResultByMutationResult(request *cachingRequest, result []byte) error {
func (c *Caching) purgeQueryResultByMutationResult(request *cachingRequest, result []byte) (err error) {
foundTags := make(cachingTags)
tagAnalyzer := newCachingTagAnalyzer(request, c.TypeKeys)

if err := tagAnalyzer.AnalyzeResult(result, nil, foundTags); err != nil {
if err = tagAnalyzer.AnalyzeResult(result, nil, foundTags); err != nil {
return err
}

Expand Down
52 changes: 17 additions & 35 deletions caching_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import (
"github.com/jensneuse/graphql-go-tools/pkg/astnormalization"
"github.com/jensneuse/graphql-go-tools/pkg/astparser"
"github.com/jensneuse/graphql-go-tools/pkg/graphql"
"github.com/jensneuse/graphql-go-tools/pkg/operationreport"
"github.com/pquerna/cachecontrol/cacheobject"
"net/http"
"strings"
)

type cachingRequest struct {
operationName string
httpRequest *http.Request
schema *graphql.Schema
gqlRequest *graphql.Request
Expand All @@ -28,56 +25,41 @@ func newCachingRequest(r *http.Request, d *ast.Document, s *graphql.Schema, gr *
gqlRequest: gr,
}

operation, report := astparser.ParseGraphqlDocumentString(gr.Query)

if report.HasErrors() {
return nil, &report
}

report.Reset()

operation.Input.Variables = gr.Variables
numOfOperations := operation.NumOfOperationDefinitions()
operationName := strings.TrimSpace(gr.OperationName)

if len(operationName) == 0 && numOfOperations > 1 {
report.AddExternalError(operationreport.ErrRequiredOperationNameIsMissing())
cacheControlString := r.Header.Get("cache-control")
cr.cacheControl, _ = cacheobject.ParseRequestCacheControl(cacheControlString)

return nil, &report
}
return cr, nil
}

if len(operationName) == 0 && numOfOperations == 1 {
operationName = operation.OperationDefinitionNameString(0)
func (r *cachingRequest) initOperation() error {
if r.operation != nil {
return nil
}

if !operation.OperationNameExists(operationName) {
report.AddExternalError(operationreport.ErrOperationWithProvidedOperationNameNotFound(operationName))
operation, report := astparser.ParseGraphqlDocumentString(r.gqlRequest.Query)

return nil, &report
if report.HasErrors() {
return &report
}

report.Reset()

operation.Input.Variables = r.gqlRequest.Variables
normalizer := astnormalization.NewWithOpts(
astnormalization.WithExtractVariables(),
astnormalization.WithRemoveFragmentDefinitions(),
astnormalization.WithRemoveUnusedVariables(),
)

if len(operationName) > 0 {
normalizer.NormalizeNamedOperation(&operation, d, []byte(operationName), &report)
if r.gqlRequest.OperationName != "" {
normalizer.NormalizeNamedOperation(&operation, r.definition, []byte(r.gqlRequest.OperationName), &report)
} else {
normalizer.NormalizeOperation(&operation, d, &report)
normalizer.NormalizeOperation(&operation, r.definition, &report)
}

if report.HasErrors() {
return nil, &report
return &report
}

cr.operationName = operationName
cr.operation = &operation
cacheControlString := r.Header.Get("cache-control")
cr.cacheControl, _ = cacheobject.ParseRequestCacheControl(cacheControlString)
r.operation = &operation

return cr, nil
return nil
}
4 changes: 2 additions & 2 deletions caching_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ func (c *Caching) getCachingQueryResult(ctx context.Context, plan *cachingPlan)
return result, nil
}

func (c *Caching) cachingQueryResult(request *cachingRequest, plan *cachingPlan, body []byte, header http.Header) error {
func (c *Caching) cachingQueryResult(request *cachingRequest, plan *cachingPlan, body []byte, header http.Header) (err error) {
tags := make(cachingTags)
tagAnalyzer := newCachingTagAnalyzer(request, c.TypeKeys)

if err := tagAnalyzer.AnalyzeResult(body, plan.Types, tags); err != nil {
if err = tagAnalyzer.AnalyzeResult(body, plan.Types, tags); err != nil {
return err
}

Expand Down
10 changes: 7 additions & 3 deletions caching_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,23 @@ func newCachingTagAnalyzer(r *cachingRequest, t graphql.RequestTypes) *cachingTa
return &cachingTagAnalyzer{r, t}
}

func (c *cachingTagAnalyzer) AnalyzeResult(result []byte, onlyTypes map[string]struct{}, tags cachingTags) error {
func (c *cachingTagAnalyzer) AnalyzeResult(result []byte, onlyTypes map[string]struct{}, tags cachingTags) (err error) {
normalizedQueryResult := &struct {
Data map[string]interface{} `json:"data,omitempty"`
}{}

if err := json.Unmarshal(result, normalizedQueryResult); err != nil {
if err = json.Unmarshal(result, normalizedQueryResult); err != nil {
return err
}

if normalizedQueryResult.Data == nil || len(normalizedQueryResult.Data) == 0 {
return errors.New("query result: `data` field missing")
}

if err = c.request.initOperation(); err != nil {
return err
}

report := &operationreport.Report{}
walker := astvisitor.NewWalker(48)
visitor := &cachingTagVisitor{
Expand All @@ -166,7 +170,7 @@ func (c *cachingTagAnalyzer) AnalyzeResult(result []byte, onlyTypes map[string]s

schemaHash, _ := c.request.schema.Hash()
schemaHashTag := fmt.Sprintf(cachingTagSchemaHashPattern, schemaHash)
operationTag := fmt.Sprintf(cachingTagOperationPattern, c.request.operationName)
operationTag := fmt.Sprintf(cachingTagOperationPattern, c.request.gqlRequest.OperationName)
tags[schemaHashTag] = struct{}{}
tags[operationTag] = struct{}{}

Expand Down
26 changes: 26 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import (
"github.com/gbox-proxy/gbox/admin/generated"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/jensneuse/graphql-go-tools/pkg/astparser"
"github.com/jensneuse/graphql-go-tools/pkg/graphql"
"github.com/jensneuse/graphql-go-tools/pkg/operationreport"
"go.uber.org/zap"
"io/ioutil"
"net/http"
"strings"
"time"
)

Expand Down Expand Up @@ -170,6 +173,29 @@ func (h *Handler) unmarshalHttpRequest(r *http.Request) (*graphql.Request, error
return nil, result.Errors
}

operation, _ := astparser.ParseGraphqlDocumentString(gqlRequest.Query)
numOfOperations := operation.NumOfOperationDefinitions()
operationName := strings.TrimSpace(gqlRequest.OperationName)
report := &operationreport.Report{}

if operationName == "" && numOfOperations > 1 {
report.AddExternalError(operationreport.ErrRequiredOperationNameIsMissing())

return nil, report
}

if operationName == "" && numOfOperations == 1 {
operationName = operation.OperationDefinitionNameString(0)
}

if !operation.OperationNameExists(operationName) {
report.AddExternalError(operationreport.ErrOperationWithProvidedOperationNameNotFound(operationName))

return nil, report
}

gqlRequest.OperationName = operationName

return gqlRequest, nil
}

Expand Down

0 comments on commit e2354e0

Please sign in to comment.