Skip to content

Commit

Permalink
merged with Mike's branch npm-desolver-main
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmiakang committed May 14, 2022
2 parents 396bfca + d3b9c89 commit 7b8765d
Show file tree
Hide file tree
Showing 13 changed files with 30,056 additions and 448 deletions.
14 changes: 14 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"presets": [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript"
],
"env": {
"test": {
"plugins": [
"@babel/plugin-transform-runtime"
]
}
}
}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,7 @@ dist
.tern-port

# Redis RDB
*.rdb
*.rdb

# DS Store
.DS_store
42 changes: 19 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ The DeSolver instance methods allow a pipeline to be loaded with mini-resolver f
<h2 href="#gettingstarted"> </h2>

# Getting Started
Note: DeSolver is currently in beta, and an npm package 0.1.2 is available [here](https://www.npmjs.com/package/desolver). Read on for more information on the intended functionality of the framework.

1. Installing DeSolver

Expand Down Expand Up @@ -76,8 +75,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 @@ -111,7 +108,7 @@ The DeSolver Parameters are as follows:
The first four parameters are the normal parameters for any resolver:
- `parent`: Sometimes referred to as the root object. The same parent/root object which is the result of the previous parent/type.
- `arguments`: Arguments provided to the root or field resolver.
- `context`: a mutable object that is provided to all resolvers.
- `context`: an object that is provided to all resolvers.
- `info`: field specific information relevant to the query.

The final three parameters are additional parameters provided by the DeSolver framework:
Expand All @@ -123,7 +120,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 +139,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 +173,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 @@ -193,25 +196,20 @@ const resolvers = desolver.apply({

<h3 href="#targetatype"></h3>

### **Targeting a specific resolver or type**
### **Targeting a specific resolver 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'
})

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 +219,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 resolver is now guarded by both authentication and authorization functions
}
}
})
Expand Down
Binary file removed assets/desolver_web.gif
Binary file not shown.
Binary file removed assets/desolverbold_web.gif
Binary file not shown.
Binary file removed assets/sphere_web.gif
Binary file not shown.
253 changes: 0 additions & 253 deletions index/desolver.ts

This file was deleted.

Loading

0 comments on commit 7b8765d

Please sign in to comment.