Skip to content

Commit 904d21d

Browse files
author
Corjen Moll
committed
Add 4th info parameter (and test cases) to resolvers
1 parent 0c778b5 commit 904d21d

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

src/resolver.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { isFunction, Promisify, isNotNullOrUndefined } from './util';
44

55
export const createResolver = (resFn, errFn) => {
66
const Promise = getPromise();
7-
const baseResolver = (root, args = {}, context = {}) => {
7+
const baseResolver = (root, args = {}, context = {}, info = {}) => {
88
// Return resolving promise with `null` if the resolver function param is not a function
99
if (!isFunction(resFn)) return Promise.resolve(null);
10-
return Promisify(resFn)(root, args, context).catch(e => {
10+
return Promisify(resFn)(root, args, context, info).catch(e => {
1111
// On error, check if there is an error handler. If not, throw the original error
1212
if (!isFunction(errFn)) throw e;
1313
// Call the error handler.
@@ -23,9 +23,9 @@ export const createResolver = (resFn, errFn) => {
2323
baseResolver['createResolver'] = (cResFn, cErrFn) => {
2424
const Promise = getPromise();
2525

26-
const childResFn = (root, args, context) => {
26+
const childResFn = (root, args, context, info = {}) => {
2727
// Start with either the parent resolver function or a no-op (returns null)
28-
const entry = isFunction(resFn) ? Promisify(resFn)(root, args, context) : Promise.resolve(null);
28+
const entry = isFunction(resFn) ? Promisify(resFn)(root, args, context, info) : Promise.resolve(null);
2929
return entry.then(r => {
3030
// If the parent returns a value, continue
3131
if (isNotNullOrUndefined(r)) return r;

test/unit/resolver_spec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,49 @@ describe('(unit) dist/resolver.js', () => {
141141
});
142142
})
143143
});
144+
describe('info parameter', () => {
145+
it('info parameter should be an empty object', () => {
146+
const r1 = {
147+
handle: (root, args, context, info) => {
148+
expect(typeof info).to.equal('object')
149+
expect(Object.keys(info).length).to.equal(0)
150+
},
151+
};
152+
const resolver = createResolver(r1.handle);
153+
154+
resolver(null, null, null)
155+
})
156+
it('should pass the info parameter', () => {
157+
const r1 = {
158+
handle: (root, args, context, info) => {
159+
expect(typeof info).to.equal('object')
160+
expect(info.info).to.equal('info')
161+
},
162+
};
163+
const resolver = createResolver(r1.handle);
164+
165+
resolver(null, null, null, { info: 'info' })
166+
})
167+
it('should pass the info parameter on a chained resolver', () => {
168+
const r1 = {
169+
handle: (root, args, context, info) => {
170+
expect(typeof info).to.equal('object')
171+
expect(info.info).to.equal('info')
172+
},
173+
};
174+
175+
const r2 = {
176+
handle: (root, args, context, info) => {
177+
expect(typeof info).to.equal('object')
178+
expect(info.chained).to.equal('info')
179+
},
180+
};
181+
182+
const baseResolver = createResolver(r1.handle);
183+
const chainedResolver = createResolver(r2.handle)
184+
185+
baseResolver(null, null, null, { info: 'info' })
186+
chainedResolver(null, null, null, { chained: 'info' })
187+
})
188+
})
144189
});

0 commit comments

Comments
 (0)