Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mercurius-cache

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mercurius-cache - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

23

index.js

@@ -6,3 +6,3 @@ 'use strict'

module.exports = fp(async function (app, { all, policy, ttl, cacheSize, skip, remoteCache }) {
module.exports = fp(async function (app, { all, policy, ttl, cacheSize, skip, remoteCache, onHit, onMiss }) {
if (typeof policy !== 'object' && !all) {

@@ -17,2 +17,5 @@ throw new Error('policy must be an object')

onHit = onHit || noop
onMiss = onMiss || noop
let cache = null

@@ -23,3 +26,3 @@

buildCache()
setupSchema(app.graphql.schema, policy, all, cache, skip, remoteCache)
setupSchema(app.graphql.schema, policy, all, cache, skip, remoteCache, onHit, onMiss)
},

@@ -39,3 +42,3 @@

buildCache()
setupSchema(schema, policy, all, cache, skip, remoteCache)
setupSchema(schema, policy, all, cache, skip, remoteCache, onHit, onMiss)
})

@@ -51,3 +54,3 @@

function setupSchema (schema, policy, all, cache, skip, remoteCache) {
function setupSchema (schema, policy, all, cache, skip, remoteCache, onHit, onMiss) {
const schemaTypeMap = schema.getTypeMap()

@@ -67,3 +70,3 @@ for (const schemaType of Object.values(schemaTypeMap)) {

const originalFieldResolver = field.resolve
field.resolve = makeCachedResolver(schemaType.toString(), fieldName, cache, originalFieldResolver, skip, remoteCache)
field.resolve = makeCachedResolver(schemaType.toString(), fieldName, cache, originalFieldResolver, skip, remoteCache, onHit, onMiss)
}

@@ -76,5 +79,9 @@ }

function makeCachedResolver (prefix, fieldName, cache, originalFieldResolver, skip, remoteCache) {
function makeCachedResolver (prefix, fieldName, cache, originalFieldResolver, skip, remoteCache, onHit, onMiss) {
const name = prefix + '.' + fieldName
onHit = onHit.bind(null, prefix, fieldName)
onMiss = onMiss.bind(null, prefix, fieldName)
cache.define(name, {
onHit,
serialize ({ self, arg, info }) {

@@ -101,5 +108,7 @@ // We need to cache only for the selected fields to support Federation

if (val) {
onHit()
return val
}
}
onMiss()
const res = await originalFieldResolver(self, arg, ctx, info)

@@ -118,1 +127,3 @@ if (remoteCache) {

}
function noop () {}
{
"name": "mercurius-cache",
"version": "0.2.0",
"version": "0.3.0",
"description": "Cache the results of your GraphQL resolvers, for Mercurius",

@@ -34,3 +34,3 @@ "main": "index.js",

"dependencies": {
"async-cache-dedupe": "^0.3.0",
"async-cache-dedupe": "^0.4.0",
"fastify-plugin": "^3.0.0"

@@ -37,0 +37,0 @@ },

@@ -65,2 +65,9 @@ # mercurius-cache

},
onHit (type, fieldName) {
// Called when a cached value is returned
},
onMiss (type, fieldName) {
// Called when there is no value in the cache
// It is not called if a resolver is skipped
},
// Useful to skip the cache for authenticated users or in some other condition

@@ -67,0 +74,0 @@ skip (self, arg, ctx, info) {

@@ -11,3 +11,3 @@ 'use strict'

test('cache a resolver', async ({ equal, same, pass, plan, teardown }) => {
plan(5)
plan(11)

@@ -38,3 +38,16 @@ const app = fastify()

let hits = 0
let misses = 0
app.register(cache, {
onHit (type, name) {
equal(type, 'Query')
equal(name, 'add')
hits++
},
onMiss (type, name) {
equal(type, 'Query')
equal(name, 'add')
misses++
},
policy: {

@@ -82,2 +95,5 @@ Query: {

}
equal(hits, 1)
equal(misses, 1)
})

@@ -84,0 +100,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc