-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcaching_request.go
66 lines (52 loc) · 1.65 KB
/
caching_request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package gbox
import (
"net/http"
"github.com/jensneuse/graphql-go-tools/pkg/ast"
"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/pquerna/cachecontrol/cacheobject"
)
type cachingRequest struct {
httpRequest *http.Request
schema *graphql.Schema
gqlRequest *graphql.Request
definition, operation *ast.Document
cacheControl *cacheobject.RequestCacheDirectives
}
func newCachingRequest(r *http.Request, d *ast.Document, s *graphql.Schema, gr *graphql.Request) *cachingRequest {
cr := &cachingRequest{
httpRequest: r,
schema: s,
definition: d,
gqlRequest: gr,
}
cacheControlString := r.Header.Get("cache-control")
cr.cacheControl, _ = cacheobject.ParseRequestCacheControl(cacheControlString)
return cr
}
func (r *cachingRequest) initOperation() error {
if r.operation != nil {
return nil
}
operation, report := astparser.ParseGraphqlDocumentString(r.gqlRequest.Query)
if report.HasErrors() {
return &report
}
operation.Input.Variables = r.gqlRequest.Variables
normalizer := astnormalization.NewWithOpts(
astnormalization.WithExtractVariables(),
astnormalization.WithRemoveFragmentDefinitions(),
astnormalization.WithRemoveUnusedVariables(),
)
if r.gqlRequest.OperationName != "" {
normalizer.NormalizeNamedOperation(&operation, r.definition, []byte(r.gqlRequest.OperationName), &report)
} else {
normalizer.NormalizeOperation(&operation, r.definition, &report)
}
if report.HasErrors() {
return &report
}
r.operation = &operation
return nil
}