Skip to content

Commit

Permalink
docs(readme): add usage for directives
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Jul 27, 2020
1 parent 170c86d commit ca24dba
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 13 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,23 @@ type Mutation {
You can also purge cache in the resolver:

```ts
const schema = makeExecutableSchema({
schemaDirectives: {
purgeCache: PurgeCacheDirective({ extraNodesPath: '__invalid_nodes__' }),
},
})

const resolvers = {
Mutation: {
archiveArticle: (parent, args, context) => {
// ...
article[CACHE_KEYWORD] = [
article.__invalid_nodes__ = [
{
id: article.id,
id: '2',
type: 'Article',
},
{
id: comment.id,
id: '3',
type: 'Comment',
},
]
Expand All @@ -90,3 +96,10 @@ const resolvers = {
},
}
```

### TODOs

- [x] responseCachePlugin
- [x] @logCache
- [x] @purgeCache
- [ ] Unit Test
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@matters/apollo-response-cache",
"version": "1.0.0",
"version": "1.1.0",
"description": "Caching and invalidation mechanisms (plugins, directives) of Apollo GraphQL",
"author": "Matters <hi@matters.news>",
"license": "MIT",
Expand Down
32 changes: 29 additions & 3 deletions src/directives/logCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,33 @@ import { get } from 'lodash'
import { toNodeFQCKey } from '../utils'

interface LogCacheDirectiveProps {
// Custom function to resolve node type
/**
* Custom function for resolving type from union and interface, or any other use cases.
*
* ```
* // define
* const typeResolver = (type: string, result: any) => {
* if (['Node', 'Response'].indexOf(type) >= 0) {
* return result.__type
* }
* return type
* }
*
* const schema = makeExecutableSchema({
* schemaDirectives: {
* purgeCache: PurgeCacheDirective({ typeResolver }),
* }
* })
*
* type Query {
* node(input: NodeInput!): Node @logCache(type: "Node")
* }
*
* // resolved as `Article`
* const nodeResult = { id: '2', __type: 'Article' }
*
* ```
*/
typeResolver?: (type: string, node: any) => string
}

Expand All @@ -15,9 +41,9 @@ export const LogCacheDirective = ({
class BaseLogCacheDirective extends SchemaDirectiveVisitor {
visitFieldDefinition(field: GraphQLField<any, any>): void {
const { resolve = defaultFieldResolver } = field
const { type, identifier } = this.args

field.resolve = async function (...args) {
field.resolve = async (...args) => {
const { type, identifier } = this.args
const [root, _, { __nodeFQCKeySet, __redis }] = args
const result = await resolve.apply(this, args)

Expand Down
37 changes: 33 additions & 4 deletions src/directives/purgeCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,38 @@ import { get } from 'lodash'
import { invalidateFQC, Node } from '../utils'

interface PurgeCacheDirectiveProps {
// The path to get extra nodes from result.
/**
* The path to get extra nodes from result object.
*
* ```
* // define
* const schema = makeExecutableSchema({
* schemaDirectives: {
* purgeCache: PurgeCacheDirective({ extraNodesPath: '__invalid_nodes__' }),
* }
* })
*
* type Mutation {
* editArticle(id: ID!): Article! @purgeCache(type: "Article")
* }
*
* // @purgeCache will invalidate three nodes: Article:1, Article:2 and Comment:3.
* const editArticleResult = {
* id: '1',
* content: '...',
* __invalid_nodes__: [
* { id: '2', type: 'Article' },
* { id: '3', type: 'Comment' }
* ]
* }
* ```
*/
extraNodesPath?: string
// Custom function to resolve node type
/**
* Custom function for resolving type from union and interface, or any other use cases.
*
* Same as `@logCache`, see `logCache.ts` for details.
**/
typeResolver?: (type: string, node: any) => string
}

Expand All @@ -18,9 +47,9 @@ export const PurgeCacheDirective = ({
class BasePurgeCacheDirective extends SchemaDirectiveVisitor {
visitFieldDefinition(field: GraphQLField<any, any>): void {
const { resolve = defaultFieldResolver } = field
const { type, identifier } = this.args

field.resolve = async function (...args) {
field.resolve = async (...args) => {
const { type, identifier } = this.args
const [root, _, { __redis }] = args
const result = await resolve.apply(this, args)

Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import responseCachePlugin from './plugins/responseCachePlugin'
import { LogCacheDirective } from './directives/logCache'
import { PurgeCacheDirective } from './directives/purgeCache'
import { invalidateFQC } from './utils'

export { responseCachePlugin, LogCacheDirective, PurgeCacheDirective }
export {
responseCachePlugin,
LogCacheDirective,
PurgeCacheDirective,
invalidateFQC,
}

0 comments on commit ca24dba

Please sign in to comment.