Skip to content

Commit 43f573e

Browse files
committed
Adding section to readme
1 parent f3d3869 commit 43f573e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,28 @@ const resolvers = combineResolvers([
168168
export default resolvers;
169169
```
170170

171+
Conditional resovlers:
172+
```javascript
173+
import { and, or } from 'apollo-resolvers';
174+
175+
import isFooResolver from './foo';
176+
import isBarResolver from './bar';
177+
178+
const banResolver = (root, { input }, { models: { UserModel } })=> UserModel.ban(input);
179+
180+
// Will execute banResolver if either isFooResolver or isBarResolver successfully resolve
181+
// If none of the resolvers succeed, the error from the last conditional resolver will
182+
// be returned
183+
const orBanResolver = or(isFooResolver, isBarResolver)(banResolver);
184+
185+
// Will execute banResolver if both isFooResolver and isBarResolver successfully resolve
186+
// If one of the condition resolvers throws an error, it will stop the execution and
187+
// return the error
188+
const andBanResolver = and(isFooResolver, isBarResolver)(banResolver);
189+
190+
// In both cases, conditions are evaluated from left to right
191+
```
192+
171193
## Resolver context
172194

173195
Resolvers are provided a mutable context object that is shared between all resolvers for a given request. A common pattern with GraphQL is inject request-specific model instances into the resolver context for each request. Models frequently reference one another, and unbinding circular references can be a pain. `apollo-resolvers` provides a request context factory that allows you to bind context disposal to server responses, calling a `dispose` method on each model instance attached to the context to do any sort of required reference cleanup necessary to avoid memory leaks:

0 commit comments

Comments
 (0)