Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix 'or' resolver #47

Merged
merged 2 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ export const and = (...conditions) => resolver => {

// Accepts multiple authentication resolvers and returns a function which will be called
// if any of the authentication resolvers succeed, or throw an error if all of them fail
export const or = (...conditions) => resolver => (...query) => {
export const or = (...conditions) => resolver => (root, args, context, info) => {
return new Promise((resolve, reject) => {
let limit = conditions.length - 1;
const attempt = (i) =>
conditions[i].createResolver(resolver)(...query)
.then(res => resolve(res))
createResolver(conditions[i])(root, args, context, info)
.then(() => {
createResolver(resolver)(root, args, context, info)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return createResolver(resolver)(root, args, context, info)

.then(res => resolve(res))
.catch(err => reject(err));
})
.catch(err => {
if(i === limit) reject(err);
else attempt(i + 1);
Expand Down
12 changes: 12 additions & 0 deletions test/unit/helper_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ describe('(unit) src/helper.js', () => {
expect(r1.handle.calledOnce).to.be.true;
});
});

it('(true, false) should return local error from wrapped resolver', () => {
const resolver = or(successResolver, failureResolver);
const localError = new Error('test');
const finalResolver = resolver(() => {
throw localError;
});
return finalResolver()
.catch(err => {
expect(err).to.equal(localError);
});
});
});

});
Expand Down