Skip to content

Commit

Permalink
cache key fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan Deren committed Jun 11, 2018
1 parent 366a16d commit f63394b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
19 changes: 6 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,32 @@ module.exports = class ApolloServerRedisCache {
*/
const names = req.body.filter(q => q.operationName).map(q => q.operationName)
queryOperationName = names && names.length ? names.join(',') : null
queryHash = _hashSum(queryOperationName + req.body)
queryHash = _hashSum(queryOperationName + JSON.stringify(req.body))
} else if (req.method === 'POST' && req.body && req.body.operationName) {
/**
* POST single query
*/

queryOperationName = req.body.operationName
queryHash = _hashSum(queryOperationName + req.body)
queryHash = _hashSum(queryOperationName + JSON.stringify(req.body))
} else if (req.method === 'POST' && req.body && !req.body.operationName) {
/**
* POST query without operationName (it is possible and we need to cache them)
*/
queryOperationName = 'unknown'
queryHash = _hashSum(queryOperationName + req.body)
queryHash = _hashSum(queryOperationName + JSON.stringify(req.body))
} else if (req.query && req.query.operationName) {
/**
* GET query
*/
queryOperationName = req.query.operationName
if (req.query.query && req.query.variables) {
queryHash = _hashSum(queryOperationName + req.query.query + req.query.variables)
} else if (req.query.query && !req.query.variables) {
queryHash = _hashSum(queryOperationName + req.query.query)
} else if (!req.query.query && req.query.variables) {
queryHash = _hashSum(queryOperationName + req.query.variables)
} else {
queryHash = _hashSum(queryOperationName)
}
queryHash = _hashSum(queryOperationName + JSON.stringify(req.query))
} else if (req.query.query && !req.query.operationName) {
/**
* GET query without operationName (it is possible and we need to cache them)
*/
queryOperationName = 'unknown'
queryHash = _hashSum(queryOperationName + req.query.query)
queryHash = _hashSum(queryOperationName + JSON.stringify(req.query))
} else {
/**
* Unknown query, I give up
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-server-redis-cache",
"version": "0.1.13",
"version": "0.1.14",
"structureVersion": "0.1.13",
"description": "Apollo GraphQL server redis cache middleware",
"main": "index.js",
Expand Down

0 comments on commit f63394b

Please sign in to comment.