Skip to content

Commit 1193dd0

Browse files
Merge pull request #28 from Corjen/master
4th option parameter
2 parents 0c778b5 + 75b3dcc commit 1193dd0

File tree

6 files changed

+60
-15
lines changed

6 files changed

+60
-15
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ const AuthenticationRequiredError = createError('AuthenticationRequiredError', {
6565

6666
export const isAuthenticatedResolver = baseResolver.createResolver(
6767
// Extract the user from context (undefined if non-existent)
68-
(root, args, { user }) => {
68+
(root, args, { user }, info) => {
6969
if (!user) throw new AuthenticationRequiredError();
7070
}
7171
);
7272

7373
export const isAdminResolver = isAuthenticatedResolver.createResolver(
7474
// Extract the user and make sure they are an admin
75-
(root, args, { user }) => {
75+
(root, args, { user }, info) => {
7676
/*
7777
If thrown, this error will bubble up to baseResolver's
7878
error callback (if present). If unhandled, the error is returned to
@@ -100,7 +100,7 @@ const NotYourUserError = createError('NotYourUserError', {
100100
});
101101

102102
const updateMyProfile = isAuthenticatedResolver.createResolver(
103-
(root, { input }, { user, models: { UserModel } }) => {
103+
(root, { input }, { user, models: { UserModel } }, info) => {
104104
/*
105105
If thrown, this error will bubble up to isAuthenticatedResolver's error callback
106106
(if present) and then to baseResolver's error callback. If unhandled, the error
@@ -128,7 +128,7 @@ const ExposedError = createError('ExposedError', {
128128
});
129129

130130
const banUser = isAdminResolver.createResolver(
131-
(root, { input }, { models: { UserModel } }) => UserModel.ban(input),
131+
(root, { input }, { models: { UserModel } }, info) => UserModel.ban(input),
132132
(root, args, context, error) => {
133133
/*
134134
For admin users, let's tell the user what actually broke
@@ -175,7 +175,7 @@ import { and, or } from 'apollo-resolvers';
175175
import isFooResolver from './foo';
176176
import isBarResolver from './bar';
177177

178-
const banResolver = (root, { input }, { models: { UserModel } })=> UserModel.ban(input);
178+
const banResolver = (root, { input }, { models: { UserModel } }, info)=> UserModel.ban(input);
179179

180180
// Will execute banResolver if either isFooResolver or isBarResolver successfully resolve
181181
// If none of the resolvers succeed, the error from the last conditional resolver will

dist/resolver.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export declare const createResolver: (resFn: any, errFn: any) => (root: any, args?: {}, context?: {}) => Promise<any>;
1+
export declare const createResolver: (resFn: any, errFn: any) => (root: any, args?: {}, context?: {}, info?: {}) => Promise<any>;

dist/resolver.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/resolver.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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)