-
-
Notifications
You must be signed in to change notification settings - Fork 814
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 preserveResolvers for root resolvers #115
Fix preserveResolvers for root resolvers #115
Conversation
And here is the fix, @helfer |
I spoke too fast. None of this code, or the previous fix for that matter, take into account the fact that mocks and resolvers can return promises. Will fix. |
Alright, there was more than a few problems with the implementations from 3 weeks ago (besides the issue with root resolver fixed in the previous commit). We were trying to merge both mock and resolver. field.resolve = (...args) => {
const mockedValue = mockResolver(...args);
const resolvedValue = oldResolver(...args);
return typeof mockedValue === 'object' && typeof resolvedValue === 'object'
? Object.assign({}, mockedValue, resolvedValue) : resolvedValue;
};
Here is the new version (see commit in that PR): field.resolve = (...args) => Promise.all([
mockResolver(...args),
oldResolver(...args),
]).then(values => {
const [mockedValue, resolvedValue] = values;
if (isObject(mockedValue) && isObject(resolvedValue)) {
return new Proxy({}, {
get: (target, key) => {
const value = resolvedValue[key];
return value === undefined ? mockedValue[key] : value;
},
});
}
return resolvedValue;
});
Now the issue with Proxies is that they can not be transpiled by Babel. They are supported by Node 6. I don't know what kind of Node compatibility you guys want to maintain. You could return a plain object instead of the Proxy:
but you would give up support for properties defined through What do you think @helfer? UPDATE: these is a krzkaczor/babel-plugin-proxy plugin, but it seems to have a performance impact. Maybe speed is not as important in the context of mocking? |
Oh, wow, I clearly didn't look closely enough then. The dangers of relying too much on tests when they don't actually cover enough possible cases... At the moment I think we cannot support only node 6, we also have to support node 4. I think it's fine to not support Thanks a lot, by the way, this is great stuff! Once you rebase the PR, I will take a careful look this time. |
@helfer OK done (hopefully). I reverted back to |
}; | ||
return graphql(jsSchema, testQuery).then((res) => { | ||
expect(res.data).to.deep.equal(expected); | ||
}); | ||
}); | ||
|
||
// it('lets you mock and resolve non-leaf types concurrently, support defineProperty', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a longer comment here that references the code that it would be testing (which is also commented out)
Ok, perfect! I merged another PR, so you need to do another rebase.If you have time, it would be great if you could add that comment I mentioned in the test as well. Ping me on slack when you've rebased, so I can merge it right away so we don't have to do another rebase etc. PS: Oh, and if you could add an entry in the changelog that would be very nice as well. |
Looks like the linter is not happy. Maybe you can try defining the function that runs inside the forEach outside the while loop? Or even better outside the sources.forEach. |
Awesome, thanks so much! |
Here is a test that show that that last commit in #96 broke
preserveResolvers
for root resolvers.Turns out, that test was already there, but no mock was in
mockMap
so nothing could take precedence.@helfer I'll work on a fix.