@@ -16,7 +16,7 @@ import (
16
16
"github.com/labstack/echo/v4"
17
17
)
18
18
19
- var gc = graphcache .NewGraphCache ()
19
+ var Cache = graphcache .NewGraphCache ("redis" )
20
20
21
21
func CacheMiddleware (next echo.HandlerFunc ) echo.HandlerFunc {
22
22
return func (c echo.Context ) error {
@@ -46,12 +46,59 @@ func CacheMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
46
46
47
47
if astQuery .Operations [0 ].Operation == "mutation" {
48
48
// if the operation is a mutation, we don't cache it
49
- c .Request ().Body = io .NopCloser (bytes .NewBuffer (requestBytes ))
49
+ // c.Request().Body = io.NopCloser(bytes.NewBuffer(requestBytes))
50
+ // c.Request().ContentLength = -1
51
+ transformedBody , err := transformer .TransformBody (request .Query , astQuery )
52
+ if err != nil {
53
+ fmt .Println ("Error transforming body:" , err )
54
+ return nil
55
+ }
56
+
57
+ fmt .Println ("time taken to transform body " , time .Since (start ))
58
+
59
+ transformedRequest := request
60
+ transformedRequest .Query = transformedBody
61
+
62
+ c .Request ().Body = io .NopCloser (bytes .NewBuffer (transformedRequest .Bytes ()))
50
63
c .Request ().ContentLength = - 1
51
- return next (c )
64
+
65
+ resBody := new (bytes.Buffer )
66
+ mw := io .MultiWriter (c .Response ().Writer , resBody )
67
+ writer := & bodyDumpResponseWriter {Writer : mw , ResponseWriter : c .Response ().Writer }
68
+ c .Response ().Writer = writer
69
+ err = next (c )
70
+ if err != nil {
71
+ return err
72
+ }
73
+ fmt .Println ("found a mutation, invalidating cache..." )
74
+
75
+ responseMap := make (map [string ]interface {})
76
+ err = json .Unmarshal (resBody .Bytes (), & responseMap )
77
+ if err != nil {
78
+ fmt .Println ("Error unmarshalling response:" , err )
79
+ }
80
+
81
+ // go through the response. Every object that has a __typename field, and an id field cache it in the format of typename:id
82
+ // for example, if the response has an object with __typename: "Organisation" and id: "1234", cache it as Organisation:1234
83
+ // if the object has a nested object with __typename: "User" and id: "5678", cache
84
+ // it as User:5678
85
+
86
+ Cache .InvalidateCache ("data" , responseMap , nil )
87
+
88
+ // newResponse := &graphcache.GraphQLResponse{}
89
+ // newResponse.FromBytes(resBody.Bytes())
90
+ // res, err := Cache.RemoveTypenameFromResponse(newResponse)
91
+ // if err != nil {
92
+ // fmt.Println("Error removing __typename:", err)
93
+ // return nil
94
+ // }
95
+ // fmt.Println("response bytes ", string(res.Bytes()))
96
+ // c.Response().Write(res.Bytes())
97
+ // c.Response().Header().Set("X-Proxy", "GraphQL Cache")
98
+ return nil
52
99
}
53
100
54
- cachedResponse , err := gc .ParseASTBuildResponse (astQuery , request )
101
+ cachedResponse , err := Cache .ParseASTBuildResponse (astQuery , request )
55
102
if err == nil && cachedResponse != nil {
56
103
fmt .Println ("serving response from cache..." )
57
104
br , err := json .Marshal (cachedResponse )
@@ -60,12 +107,7 @@ func CacheMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
60
107
}
61
108
fmt .Println ("time taken to serve response from cache " , time .Since (start ))
62
109
graphqlresponse := graphcache.GraphQLResponse {Data : json .RawMessage (br )}
63
- res , err := gc .RemoveTypenameFromResponse (& graphqlresponse )
64
- if err != nil {
65
- fmt .Println ("Error removing __typename:" , err )
66
- return nil
67
- }
68
- return c .JSON (200 , res )
110
+ return c .JSON (200 , graphqlresponse )
69
111
}
70
112
71
113
transformedBody , err := transformer .TransformBody (request .Query , astQuery )
@@ -116,10 +158,10 @@ func CacheMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
116
158
// for the operation op we need to traverse the response and the ast together to build a graph of the relations
117
159
118
160
// build the relation graph
119
- responseKey := gc .GetQueryResponseKey (op , responseMap , variables )
161
+ responseKey := Cache .GetQueryResponseKey (op , responseMap , variables )
120
162
for key , value := range responseKey {
121
163
if value != nil {
122
- gc .SetQueryCache (key , value )
164
+ Cache .SetQueryCache (key , value )
123
165
}
124
166
}
125
167
}
@@ -131,7 +173,7 @@ func CacheMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
131
173
// if the object has a nested object with __typename: "User" and id: "5678", cache
132
174
// it as User:5678
133
175
134
- gc .CacheResponse ("data" , responseMap , nil )
176
+ Cache .CacheResponse ("data" , responseMap , nil )
135
177
136
178
fmt .Println ("time taken to cache response " , time .Since (start ))
137
179
@@ -149,10 +191,10 @@ func CacheMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
149
191
// fmt.Println(string(queryCacheState))
150
192
151
193
fmt .Println ("time taken to finish completely " , time .Since (start ))
152
- newResponse := & graphcache.GraphQLResponse {}
153
- newResponse .FromBytes (resBody .Bytes ())
154
- gc .RemoveTypenameFromResponse (newResponse )
155
- c .Response ().Header ().Set ("X-Proxy" , "GraphQL Cache" )
194
+ // newResponse := &graphcache.GraphQLResponse{}
195
+ // newResponse.FromBytes(resBody.Bytes())
196
+ // Cache .RemoveTypenameFromResponse(newResponse)
197
+ // c.Response().Header().Set("X-Proxy", "GraphQL Cache")
156
198
return nil
157
199
}
158
200
}
0 commit comments