Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
mercurius-cache
Advanced tools
Adds an in-process caching layer to Mercurius. Federation is fully supported.
Based on preliminary testing, it is possible to achieve a significant throughput improvement at the expense of the freshness of the data. Setting the ttl accordingly is of critical importance.
Under the covers it uses async-cache-dedupe
which will also deduplicate the calls.
npm i fastify mercurius mercurius-cache
'use strict'
const fastify = require('fastify')
const mercurius = require('mercurius')
const cache = require('mercurius-cache')
const app = fastify({ logger: true })
const schema = `
type Query {
add(x: Int, y: Int): Int
hello: String
}
`
const resolvers = {
Query: {
async add (_, { x, y }, { reply }) {
reply.log.info('add called')
for (let i = 0; i < 10000000; i++) {} // something that takes time
return x + y
}
}
}
app.register(mercurius, {
schema,
resolvers
})
// cache query "add" responses for 10 seconds
app.register(cache, {
ttl: 10,
policy: {
Query: {
add: true
// note: it cache "add" but it doesn't cache "hello"
}
}
})
app.listen(3000)
// Use the following to test
// curl -X POST -H 'content-type: application/json' -d '{ "query": "{ add(x: 2, y: 2) }" }' localhost:3000/graphql
the time to live in seconds; default is 0
, which means that the cache is disabled.
Example
ttl: 10
the maximum amount of entries to fit in the cache for each query, default 1024
.
Example
cacheSize: 2048
specify queries to cache; default is empty.
Example
policy: {
Query: {
add: true
}
}
extend the key to cache responses by different request, for example to enable custom cache per user; see examples/cache-per-user.js for a complete use case. Example
policy: {
Query: {
welcome: {
extendKey: function (source, args, context, info) {
return context.userId ? `user:${context.userId}` : undefined
}
}
}
}
use a specific ttl for the policy, instead of the default one.
Example
ttl: 10,
policy: {
Query: {
welcome: {
ttl: 5
}
}
}
use a specific cacheSize for the policy, instead of the default one.
Example
policy: {
cacheSize: 2048,
Query: {
welcome: {
cacheSize: 1024
}
}
}
skip cache use for a specific condition.
Example
skip (self, arg, ctx, info) {
if (ctx.reply.request.headers.authorization) {
return true
}
return false
}
use the cache in all resolvers; default is false. Use either policy
or all
but not both.
Example
all: true
default cache is in memory, but a different storage can be used for a larger cache. See examples/redis.js for a complete use case.
Example
storage: {
async get (key) {
// fetch by key from storage
return storage.get(key)
},
async set (key, value) {
// set the value in the storage
return storage.set(key, value)
}
}
called when a cached value is returned.
Example
onHit (type, fieldName) {
console.log(`hit ${type} ${fieldName}`)
}
called when there is no value in the cache; it is not called if a resolver is skipped.
Example
onMiss (type, fieldName) {
console.log(`miss ${type} ${fieldName}`)
}
called when the resolver is skipped, both by skip
or policy.skip
.
Example
onSkip (type, fieldName) {
console.log(`skip ${type} ${fieldName}`)
}
skip cache use for a specific condition.
Example
skip (self, arg, ctx, info) {
if (ctx.reply.request.headers.authorization) {
return true
}
return false
}
MIT
FAQs
Cache the results of your GraphQL resolvers, for Mercurius
We found that mercurius-cache demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.