Skip to content

Commit

Permalink
feat: Support custom cache key function
Browse files Browse the repository at this point in the history
Cache key function
  • Loading branch information
maticzav authored Jun 19, 2019
2 parents 2371ea6 + 75caf22 commit 3363af5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ export class Rule implements IRule {
*
*/
private generateCacheKey(parent, args, ctx: IShieldContext, info): string {
if (typeof this.cache === 'function') {
return `${this.name}-${this.cache(parent, args, ctx, info)}`
}

switch (this.cache) {
case 'strict': {
const key = ctx._shield.hashFunction({ parent, args })
Expand Down
9 changes: 3 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export declare class ILogicRule {
}

export type IFragment = string
export type ICache = 'strict' | 'contextual' | 'no_cache'
export type ICache = 'strict' | 'contextual' | 'no_cache' | ICacheKeyFn
export type ICacheKeyFn = (parent, args, ctx, info) => string
export type IRuleResult = boolean | string | Error
export type IRuleFunction = (
parent?: any,
Expand All @@ -41,11 +42,7 @@ export type IRuleFunction = (

// Rule Constructor Options

export type ICacheContructorOptions =
| 'strict'
| 'contextual'
| 'no_cache'
| boolean
export type ICacheContructorOptions = ICache | boolean

export interface IRuleConstructorOptions {
cache?: ICacheContructorOptions
Expand Down
59 changes: 59 additions & 0 deletions tests/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,65 @@ describe('Caching works as expected', () => {
})
expect(allowMock).toBeCalledTimes(5)
})

test('Custom cache key function - Rule is called based on custom cache key', async () => {
/* Schema */
const typeDefs = `
type Query {
a(arg: String): String!
b(arg: String): String!
}
`
const resolvers = {
Query: {
a: () => 'a',
b: () => 'b',
},
}

const schema = makeExecutableSchema({
typeDefs,
resolvers,
})

/* Tests */

const allowMock = jest.fn().mockResolvedValue(true)

const permissions = shield({
Query: rule({
cache: (parent, args, ctx, info) => {
return JSON.stringify(args)
},
})(allowMock),
})

const schemaWithPermissions = applyMiddleware(schema, permissions)

/* Execution */

const query = `
query {
a(arg: "foo")
b(arg: "bar")
a2: a(arg: "foo")
a3: a(arg: "boo")
}
`
const res = await graphql(schemaWithPermissions, query, undefined, {})

/* Tests */

expect(res).toEqual({
data: {
a: 'a',
b: 'b',
a2: 'a',
a3: 'a',
},
})
expect(allowMock).toBeCalledTimes(3)
})
})

test('Customize hash function', async () => {
Expand Down

0 comments on commit 3363af5

Please sign in to comment.