Skip to content

Commit

Permalink
feat(core): Type definitions, Renovate
Browse files Browse the repository at this point in the history
  • Loading branch information
maticzav committed May 26, 2018
1 parent caf35b6 commit e580c1c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

> GraphQL Middleware plugin for forwarding request to GraphQL Bindings.
```bash
yarn add graphql-middleware-forward-binding
```

## Usage

> With GraphQL Yoga and Prisma
Expand All @@ -18,10 +14,11 @@ import { GraphQLServer } from 'graphql-yoga'
import { forward } from 'graphql-middleware-forward-binding'
import { Prisma } from 'prisma-binding'

const bindingForwardMiddleware = forward('db')
const bindingForwardMiddleware = forward('Query', 'Mutation')('db')

const server = GraphQLServer({
typeDefs: 'generated-schema.graphql',
resolvers: {},
middlewares: [bindingForwardMiddleware],
context: req => ({
...req,
Expand All @@ -38,7 +35,7 @@ serve.start(() => `Server running on http://localhost:4000`)
## API

```ts
function forward(database: string): IMiddleware
function forward(types: string[])(database: string): IMiddleware
```

## License
Expand Down
6 changes: 6 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": ["config:base", "docker:disable"],
"packageRules": {
"rangeStrategy": "bump"
}
}
20 changes: 18 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import { IMiddleware } from 'graphql-middleware'
import { forwardTo } from 'graphql-binding'

export const forward = (database: string): IMiddleware => {
export const forward = (...types: string[]) => (
database: string,
): IMiddleware => {
if (!database) {
throw new Error(`Missing database name.`)
}

return async function(resolve, parent, args, ctx, info) {
if (!types) {
throw new Error(`Missing forwarded types list.`)
}

const fn = async function(resolve, parent, args, ctx, info) {
return forwardTo(database)(parent, args, ctx, info)
}

const middleware = types.reduce(
(_types, type) => ({
..._types,
[type]: fn,
}),
{},
)

return middleware
}

0 comments on commit e580c1c

Please sign in to comment.