Skip to content

Commit 4029ffe

Browse files
authored
Merge pull request #428 from Sytten/fix/missing-ctx
Add check for missing context and document it
2 parents f75df58 + 6bff2e8 commit 4029ffe

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Please see the related [issue](https://github.com/Sytten/nexus-shield/issues/50)
1111

1212
## Overview
1313

14-
Nexus Shield is a [nexus](https://github.com/graphql-nexus/nexus) plugin that helps you create an authorization layer for your application. It is a replacement for the provided authorization plugin. It is heavily inspired by [Graphql Shield](https://github.com/maticzav/graphql-shield) and reuses most of it's familiar ruling system. It takes full advantage of the type safety provided by nexus.
14+
Nexus Shield is a [nexus](https://github.com/graphql-nexus/nexus) plugin that helps you create an authorization layer for your application. It is a replacement for the provided authorization plugin. It is heavily inspired by [Graphql Shield](https://github.com/maticzav/graphql-shield) and reuses most of its familiar ruling system. It takes full advantage of the type safety provided by nexus.
1515

1616
## Install
1717

@@ -50,9 +50,30 @@ const schema = makeSchema({
5050
});
5151
```
5252

53+
#### Subscriptions configuration
54+
55+
- When using subscriptions with a server that is not integrated directly into your "main" GraphQL server, you **must** make sure that you pass in a valid context.
56+
- This context should contain all the information needed to evaluate the rules. Ideally, it is the same as the context for your "main" server otherwise the typing won't reflect the data available to the rules.
57+
58+
For example, using [GraphQL-WS](https://github.com/enisdenjo/graphql-ws):
59+
60+
```typescript
61+
useServer(
62+
{
63+
schema,
64+
context: (ctx, msg, args) => {
65+
// That will return the same context that was passed when the
66+
// server received the subscription request
67+
return ctx;
68+
},
69+
},
70+
wsServer
71+
);
72+
```
73+
5374
### Styles
5475

55-
Two interfaces styles are provided for convenience: `Graphql-Shield` and `Nexus`.
76+
Two interface styles are provided for convenience: `Graphql-Shield` and `Nexus`.
5677

5778
#### Graphql-Shield
5879

@@ -234,7 +255,7 @@ const viewerIsAuthorized = partial<'Product'>(
234255
);
235256
```
236257

237-
However, if you specify it directly in the `shield` field, there is not need for an helper thus no need for a parameter.
258+
However, if you specify it directly in the `shield` field, there is no need for a helper thus no need for a parameter.
238259

239260
```typescript
240261
t.string('prop', {
@@ -244,7 +265,7 @@ t.string('prop', {
244265

245266
### Caching
246267

247-
- The result of a rule can be cached to maximize performances. This is important when using generic or partial rules that require access to external data.
268+
- The result of a rule can be cached to maximize performance. This is important when using generic or partial rules that require access to external data.
248269
- The caching is **always** scoped to the request
249270

250271
The plugin offers 3 levels of caching:
@@ -270,7 +291,7 @@ ruleType({
270291

271292
### Known issues / limitations
272293

273-
- Currently the typing of the `shield` parameter on `objectType` doesn't work. Tracked by issue: https://github.com/Sytten/nexus-shield/issues/50
294+
- Currently, the typing of the `shield` parameter on `objectType` doesn't work. Tracked by issue: https://github.com/Sytten/nexus-shield/issues/50
274295

275296
- It is not possible to pass directly an `objectType` to the parameter `type` of a `ruleType`. Tracked by issue: https://github.com/graphql-nexus/schema/issues/451
276297

src/plugin.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ export const nexusShield = (settings: ShieldPluginSettings) => {
7676

7777
return async (root, args, ctx, info, next) => {
7878
// Cache
79-
const shieldCtx = ctx as ShieldContext;
79+
const shieldCtx = ctx as ShieldContext | undefined;
80+
if (!shieldCtx) {
81+
throw new Error('Missing resolver context, aborting!');
82+
}
8083
if (!shieldCtx._shield) {
8184
shieldCtx._shield = {
8285
cache: {},

0 commit comments

Comments
 (0)