Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
maticzav committed May 25, 2018
1 parent 7cd2cf1 commit f74f362
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ All in one solution to manage middleware in your GraphQL projects.

GraphQL Middleware is a schema wrapper which allows you to manage additional functionality across multiple resolvers efficiently.

* **Easiest way to handle GraphQL middleware:** Intuitive, yet familiar API will get under your skin in a second.
* **Easiest way to handle GraphQL middleware:** An intuitive, yet familiar API that you will pick up in a second.
* **Powerful:** Allows complete control over your resolvers (Before, After).
* **Compatible:** Works with any GraphQL Schema.

Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
defaultFieldResolver,
GraphQLSchema,
getNamedType,
getNullableType,
Expand Down Expand Up @@ -41,9 +42,11 @@ function wrapResolverInMiddleware(
middleware: IMiddlewareFunction,
): GraphQLFieldResolver<any, any> {
return (parent, args, ctx, info) => {
const resolveFn = resolver || defaultFieldResolver

return middleware(
(_parent = parent, _args = args, _ctx = ctx, _info = info) =>
resolver(_parent, _args, _ctx, _info),
resolveFn(_parent, _args, _ctx, _info),
parent,
args,
ctx,
Expand Down
28 changes: 28 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const typeDefs = `
afterNothing: String!
null: String
nested: Nothing!
resolverless: Resolverless!
}
type Mutation {
Expand All @@ -28,6 +29,10 @@ const typeDefs = `
nothing: String!
}
type Resolverless {
someData: String!
}
schema {
query: Query,
mutation: Mutation
Expand All @@ -42,6 +47,7 @@ const resolvers = {
afterNothing: () => 'after',
null: () => null,
nested: () => ({}),
resolverless: () => ({ someData: 'data' }),
},
Mutation: {
before: async (parent, { arg }, ctx, info) => arg,
Expand Down Expand Up @@ -428,6 +434,28 @@ test('Schema middleware - Mutation after', async t => {
})
})

test('Schema middleware - Uses default field resolver', async t => {
const schema = getSchema()
const schemaWithMiddleware = applyMiddleware(schema, schemaMiddlewareBefore)

const query = `
query {
resolverless {
someData
}
}
`
const res = await graphql(schemaWithMiddleware, query)

t.deepEqual(res, {
data: {
resolverless: {
someData: 'data',
},
},
})
})

// Combinations

test('Schema, Field middleware - Query', async t => {
Expand Down

0 comments on commit f74f362

Please sign in to comment.