Skip to content

Commit

Permalink
fix(core): return subscription resolver as object with 'subscribe' key
Browse files Browse the repository at this point in the history
  • Loading branch information
mjdickinson committed Jun 14, 2018
1 parent 4b96789 commit 7bfa6b9
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ export class MiddlewareError extends Error {
function applyMiddlewareToField(
field: GraphQLField<any, any, any>,
middleware: IMiddlewareFunction,
): GraphQLFieldResolver<any, any, any> {
let resolver = field.resolve

):
| GraphQLFieldResolver<any, any, any>
| { subscribe: GraphQLFieldResolver<any, any, any> } {
if (field.subscribe) {
resolver = field.subscribe
return { subscribe: wrapResolverInMiddleware(field.subscribe, middleware) }
}

return wrapResolverInMiddleware(resolver, middleware)
return wrapResolverInMiddleware(field.resolve, middleware)
}

function applyMiddlewareToType(
Expand Down
99 changes: 97 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import test from 'ava'
import { graphql } from 'graphql'
import { graphql, subscribe, parse } from 'graphql'
import { makeExecutableSchema } from 'graphql-tools'
import { $$asyncIterator } from 'iterall'
import { applyMiddleware, MiddlewareError } from './dist'

// Setup ---------------------------------------------------------------------
Expand All @@ -24,6 +25,10 @@ const typeDefs = `
null: String
nested: Nothing!
}
type Subscription {
sub: String
}
type Nothing {
nothing: String!
Expand All @@ -35,7 +40,8 @@ const typeDefs = `
schema {
query: Query,
mutation: Mutation
mutation: Mutation,
subscription: Subscription
}
`

Expand All @@ -57,6 +63,19 @@ const resolvers = {
null: () => null,
nested: () => ({}),
},
Subscription: {
sub: {
subscribe: async (parent, { arg }, ctx, info) => {
const iterator = {
next: () => Promise.resolve({ done: false, value: { sub: arg } }),
return: () => {},
throw: () => {},
[$$asyncIterator]: () => iterator,
}
return iterator
},
},
},
Nothing: {
nothing: () => 'nothing',
},
Expand Down Expand Up @@ -89,6 +108,12 @@ const fieldMiddleware = {
return 'changed'
},
},
Subscription: {
sub: async (resolve, parent, args, context, info) => {
const _args = { arg: 'changed' }
return resolve(parent, _args)
},
},
}

// Type Middleware
Expand All @@ -102,6 +127,10 @@ const typeMiddlewareBefore = {
const _args = { arg: 'changed' }
return resolve(parent, _args)
},
Subscription: async (resolve, parent, args, context, info) => {
const _args = { arg: 'changed' }
return resolve(parent, _args)
},
}

const typeMiddlewareAfter = {
Expand Down Expand Up @@ -205,6 +234,28 @@ test('Field middleware - Mutation', async t => {
})
})

test('Field middleware - Subscription', async t => {
const schema = getSchema()
const schemaWithMiddleware = applyMiddleware(schema, fieldMiddleware)

const query = `
subscription {
sub
}
`
const iterator = await subscribe(schemaWithMiddleware, parse(query))
const res = await iterator.next()

t.deepEqual(res, {
done: false,
value: {
data: {
sub: 'changed',
},
},
})
})

// Type

test('Type middleware - Query before', async t => {
Expand Down Expand Up @@ -319,6 +370,28 @@ test('Type middleware - Mutation after', async t => {
})
})

test('Type middleware - Subscription', async t => {
const schema = getSchema()
const schemaWithMiddleware = applyMiddleware(schema, typeMiddlewareBefore)

const query = `
subscription {
sub
}
`
const iterator = await subscribe(schemaWithMiddleware, parse(query))
const res = await iterator.next()

t.deepEqual(res, {
done: false,
value: {
data: {
sub: 'changed',
},
},
})
})

// Schema

test('Schema middleware - Query before', async t => {
Expand Down Expand Up @@ -434,6 +507,28 @@ test('Schema middleware - Mutation after', async t => {
})
})

test('Schema middleware - Subscription', async t => {
const schema = getSchema()
const schemaWithMiddleware = applyMiddleware(schema, schemaMiddlewareBefore)

const query = `
subscription {
sub
}
`
const iterator = await subscribe(schemaWithMiddleware, parse(query))
const res = await iterator.next()

t.deepEqual(res, {
done: false,
value: {
data: {
sub: 'changed',
},
},
})
})

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

0 comments on commit 7bfa6b9

Please sign in to comment.