Skip to content

Commit

Permalink
Merge pull request #28 from Corjen/master
Browse files Browse the repository at this point in the history
4th option parameter
  • Loading branch information
thebigredgeek authored May 13, 2018
2 parents 0c778b5 + 75b3dcc commit 1193dd0
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 15 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ const AuthenticationRequiredError = createError('AuthenticationRequiredError', {

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

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

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

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

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

// Will execute banResolver if either isFooResolver or isBarResolver successfully resolve
// If none of the resolvers succeed, the error from the last conditional resolver will
Expand Down
2 changes: 1 addition & 1 deletion dist/resolver.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export declare const createResolver: (resFn: any, errFn: any) => (root: any, args?: {}, context?: {}) => Promise<any>;
export declare const createResolver: (resFn: any, errFn: any) => (root: any, args?: {}, context?: {}, info?: {}) => Promise<any>;
8 changes: 4 additions & 4 deletions dist/resolver.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/resolver.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { isFunction, Promisify, isNotNullOrUndefined } from './util';

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

const childResFn = (root, args, context) => {
const childResFn = (root, args, context, info = {}) => {
// Start with either the parent resolver function or a no-op (returns null)
const entry = isFunction(resFn) ? Promisify(resFn)(root, args, context) : Promise.resolve(null);
const entry = isFunction(resFn) ? Promisify(resFn)(root, args, context, info) : Promise.resolve(null);
return entry.then(r => {
// If the parent returns a value, continue
if (isNotNullOrUndefined(r)) return r;
Expand Down
45 changes: 45 additions & 0 deletions test/unit/resolver_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,49 @@ describe('(unit) dist/resolver.js', () => {
});
})
});
describe('info parameter', () => {
it('info parameter should be an empty object', () => {
const r1 = {
handle: (root, args, context, info) => {
expect(typeof info).to.equal('object')
expect(Object.keys(info).length).to.equal(0)
},
};
const resolver = createResolver(r1.handle);

resolver(null, null, null)
})
it('should pass the info parameter', () => {
const r1 = {
handle: (root, args, context, info) => {
expect(typeof info).to.equal('object')
expect(info.info).to.equal('info')
},
};
const resolver = createResolver(r1.handle);

resolver(null, null, null, { info: 'info' })
})
it('should pass the info parameter on a chained resolver', () => {
const r1 = {
handle: (root, args, context, info) => {
expect(typeof info).to.equal('object')
expect(info.info).to.equal('info')
},
};

const r2 = {
handle: (root, args, context, info) => {
expect(typeof info).to.equal('object')
expect(info.chained).to.equal('info')
},
};

const baseResolver = createResolver(r1.handle);
const chainedResolver = createResolver(r2.handle)

baseResolver(null, null, null, { info: 'info' })
chainedResolver(null, null, null, { chained: 'info' })
})
})
});

0 comments on commit 1193dd0

Please sign in to comment.