Skip to content

Commit

Permalink
adjusted some comments and rewrote some parts of readme to reflect th…
Browse files Browse the repository at this point in the history
…e new feature to add target resolver types to the use method
  • Loading branch information
mckchan13 committed May 8, 2022
1 parent 65f3f56 commit 20c2826
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ const desolverConfig = {

- `cacheDesolver`: Set to `true` to enable Redis caching, by default if nothing is passed, the default redis instance will be started. Set to `false` to disable this behavior.

- `applyResolverType`: Takes a string value that represents either a root query ( `Query`, `Mutation` ) or some other custom type as defined in your schema. Specify `Root` to chain both `Query` and `Mutation`. Set to `All` to wrap every resolver. By default, if none is specified, all resolvers will be chained to the middleware pipeline.

The desolverConfig object can also be defined with configuration options from Redis. See [node-redis](https://github.com/redis/node-redis) for a list of additional custom configuration options that can be defined from Redis.
<br><br>

Expand Down Expand Up @@ -123,7 +121,13 @@ The final three parameters are additional parameters provided by the DeSolver fr

### **Creating the middleware pipeline**

Utilize the `desolver.use()` method on the desolver instance to generate your pipeline of prehook functions. Any number of function arguments passed to `desolver.use()` will be pushed to the function pipeline. Multiple successive invocations of `desolver.use()` will also add additional functions to the pipeline.
Utilize the `desolver.use()` method on the desolver instance to generate your pipeline of prehook functions.

The `desolver.use()` method has the following parameters:
- `ResolverType`: A string which matches the Resolver Type in your Resolver Map object. Pass the string `'All'` if you want to chain the prehook functions to all your resolvers.
- `DeSolverFragments`: Add any number of functions in the form of DeSolver Fragments to chain to the target Resolver Type.

Multiple successive invocations of `desolver.use()` will also add additional functions to the pipeline.

The following is an example use case for the desolver middleware pipeline involving guarding root queries with authentication logic:

Expand All @@ -136,9 +140,9 @@ const authentication = (parent, args, context, info, next, escapeHatch, ds) => {
// throw error if not authenticated
}

// Add to the authentication function to pipeline with desolver.use()
// This function will execute prior to all resolvers
desolver.use(authentication)
// Add the authentication function to pipeline with desolver.use()
// This function will execute prior to all Query resolvers
desolver.use('Query', authentication)

// Invoke desolver.apply() method with the resolver map object passed
const resolvers = desolver.apply({
Expand Down Expand Up @@ -170,7 +174,7 @@ See the example below:
// desolver.use(), desolver.apply() and desolver.useRoute() can
// be used together to create longer chains

desolver.use(authentication)
desolver.use('Query', authentication)

const resolvers = desolver.apply({
Query: {
Expand All @@ -195,23 +199,22 @@ const resolvers = desolver.apply({

### **Targeting a specific resolver or type**

To chain resolvers to a specific root type or field resolvers, specify the root query or field to chain the middleware pipeline into the configuration object passed to the DeSolver constructor.
To chain Desolver Fragments to a specific root type or field resolvers, multiple invocations of `desolver.use()` can be called with different Resolver Type targets.

See the example below:

```javascript
// Specify when instantiating Desolver which resolvers to chain to in the
// configuration object
const desolver = new Desolver({
applyResolverType: 'Query'
})
const desolver = new Desolver()

desolver.use(authentication)
desolver.use('Query', authentication)

desolver.use('Mutation', authentication, authorization)

const resolvers = desolver.apply({
Query: {
// Only these root query resolvers will be guarded by the authentication
// function
// Query resolvers are guarded only by authentication function
getUserById: desolver.useRoute(desolverFragment1, desolverFragment2),

getPostsById: (parent, { id }, context, info) => {
Expand All @@ -221,9 +224,7 @@ const resolvers = desolver.apply({

Mutation: {
createUser: (parent, root, args, context, info) => {
// This mutation is not guarded by the authentication logic
// set applyResolverType to 'Root' to wrap chain to both
// 'Query' and 'Mutation'
// This mutation is now guarded by both authentication and authorization functions
}
}
})
Expand Down
3 changes: 1 addition & 2 deletions server/ResolverBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ export class ResolverBuilder {
}

// Method to load DesolverFragments into the pipeline
// Return 'this' so that multiple load methods and buildResolverWrapper method can be chained
public load(...desolvers: DesolverFragment[]): this {
this.desolverPipeline.push(...desolvers);

// return 'this' so that multiple load methods and buildResolverWrapper method can be chained
return this;
}

Expand Down

0 comments on commit 20c2826

Please sign in to comment.