@@ -103,17 +103,25 @@ function getUser(req) {
103103
104104// Rules
105105
106- const isAuthenticated = rule ()(async (parent , args , ctx , info ) => {
107- return ctx .user !== null
108- })
106+ /* Read more about cache options down in the `rules/cache` section. */
109107
110- const isAdmin = rule ()(async (parent , args , ctx , info ) => {
111- return ctx .user .role === ' admin'
112- })
108+ const isAuthenticated = rule ({ cache: ' contextual' })(
109+ async (parent , args , ctx , info ) => {
110+ return ctx .user !== null
111+ },
112+ )
113113
114- const isEditor = rule ()(async (parent , args , ctx , info ) => {
115- return ctx .user .role === ' editor'
116- })
114+ const isAdmin = rule ({ cache: ' contextual' })(
115+ async (parent , args , ctx , info ) => {
116+ return ctx .user .role === ' admin'
117+ },
118+ )
119+
120+ const isEditor = rule ({ cache: ' contextual' })(
121+ async (parent , args , ctx , info ) => {
122+ return ctx .user .role === ' editor'
123+ },
124+ )
117125
118126// Permissions
119127
@@ -274,25 +282,34 @@ const admin = bool =>
274282 )
275283` ` `
276284
277- - Cache is enabled by default across all rules . To prevent ` cache ` generation , set ` { cache: 'no_cache' } ` or ` { cache: false } ` when generating a rule .
278- - By default , no rule is executed more than once in complete query execution . This accounts for significantly better load times and quick responses .
285+ - Cache is disabled by default . To enable ` cache ` generation , set cache option when generating a rule .
279286
280287##### Cache
281288
282289You can choose from three different cache options .
283290
2842911. ` no_cache ` - prevents rules from being cached .
285- 1. ` contextual ` - use when rule only relies on ` ctx ` parameter .
286- 1. ` strict ` - use when rule relies on ` parent ` or ` args ` parameter as well .
292+ 1. ` contextual ` - use when rule only relies on ` context ` parameter ( useful for authentication ) .
293+ 1. ` strict ` - use when rule relies on ` parent ` or ` args ` parameter as well ( field specific modifications ) .
287294
288295` ` ` ts
289296// Contextual
290- const admin = rule({ cache: 'contextual' })(async (parent, args, ctx, info) => {
291- return ctx.user.isAdmin
292- })
297+ const isAdmin = rule({ cache: 'contextual' })(
298+ async (parent, args, ctx, info) => {
299+ return ctx.user.isAdmin
300+ },
301+ )
293302
294303// Strict
295- const admin = rule({ cache: 'strict' })(async (parent, args, ctx, info) => {
304+ const canSeeUserSensitiveData = rule({ cache: 'strict' })(
305+ async (parent, args, ctx, info) => {
306+ /* The id of observed User matches the id of authenticated viewer. */
307+ return ctx.viewer.id === parent.id
308+ },
309+ )
310+
311+ // No-cache (defuault)
312+ const admin = rule({ cache: 'no_cache' })(async (parent, args, ctx, info) => {
296313 return ctx.user.isAdmin || args.code === 'secret' || parent.id === 'theone'
297314})
298315` ` `
@@ -629,7 +646,7 @@ See [#126](https://github.com/maticzav/graphql-shield/issues/126#issuecomment-41
629646
630647#### A rule is executed only once even though the dataset contains multiple values (and thus should execute the rule multiple times )
631648
632- This occurs because of caching . When the cache is set to " contextual" only the contextual variable of the rule is expected to be evaluated . Setting the cache to " strict" allows the rule to rely on parent and args parameters as well .
649+ This occurs because of caching . When the cache is set to ` contextual ` only the contextual variable of the rule is expected to be evaluated . Setting the cache to ` strict ` allows the rule to rely on parent and args parameters as well , while setting the cache to ` no_cache ` won ' t cache result at all .
633650
634651## Contributors
635652
0 commit comments