-
-
Notifications
You must be signed in to change notification settings - Fork 820
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
Bugfix for resolving & mocking, when resolve is wrapped #121
Conversation
Are you sure that's a bug with mocking and not with the logger? It seems strange to me that it would change anything whether or not the resolve function is wrapped with another one. |
Yes, if the logger is not present, then the field resolve will be undefined (defaults to default resolver) which mocking will override by mockResolver. |
@DxCx thanks for all the research. Unfortunately I don't have much expertise with the loggers or connectors (yet).
Doesn't fix this test for me: it('can mock object types (again)', () => {
const jsSchema = buildSchemaFromTypeDefinitions(shorthand);
const resolvers = {
RootQuery: {
returnInt: () => 123,
},
};
addResolveFunctionsToSchema(jsSchema, resolvers);
const testQuery = `{
returnObject { returnInt, returnString }
}`;
const expected = {
returnObject: { returnInt: 123, returnString: 'Hello World' },
};
return graphql(jsSchema, testQuery).then((res) => {
expect(res.data).to.deep.equal(expected);
});
}); as I'm getting:
And this slightly altered test, where the mockMap is empty: it('can mock object types (again)', () => {
const jsSchema = buildSchemaFromTypeDefinitions(shorthand);
const mockMap = {};
const resolvers = {
RootQuery: {
returnInt: () => 123,
},
};
addResolveFunctionsToSchema(jsSchema, resolvers);
addMockFunctionsToSchema({ schema: jsSchema, mocks: mockMap, preserveResolvers: true });
const testQuery = `{
returnObject { returnInt, returnString }
}`;
const expected = {
returnObject: { returnInt: 123, returnString: 'Hello World' },
};
return graphql(jsSchema, testQuery).then((res) => {
expect(res.data).to.deep.equal(expected);
});
}); Also fails for me. I'm getting:
Granted, both tests were also failing without your fix. |
hi @sebastienbarre, Please note that your query is to the query => returnObject, @@ -406,10 +406,10 @@ describe('Mock', () => {
};
addResolveFunctionsToSchema(jsSchema, resolvers);
const testQuery = `{
- returnObject { returnInt, returnString }
+ returnInt, returnString
}`;
const expected = {
- returnObject: { returnInt: 123, returnString: 'Hello World' },
+ returnInt: 123, returnString: null,
};
return graphql(jsSchema, testQuery).then((res) => {
expect(res.data).to.deep.equal(expected); so we can see that without mocking, string returns null as expected and for the second: @@ -426,10 +426,10 @@ describe('Mock', () => {
addResolveFunctionsToSchema(jsSchema, resolvers);
addMockFunctionsToSchema({ schema: jsSchema, mocks: mockMap, preserveResolvers: true });
const testQuery = `{
- returnObject { returnInt, returnString }
+ returnInt, returnString
}`;
const expected = {
- returnObject: { returnInt: 123, returnString: 'Hello World' },
+ returnInt: 123, returnString: 'Hello World',
};
return graphql(jsSchema, testQuery).then((res) => {
expect(res.data).to.deep.equal(expected); and with mocking, it is returning the mocked string with the resolved int as expected. |
I think this could potentially lead to confusion if someone defines a resolver which returns undefined by accident. It will be much harder to find the error in that case, because the return value will be replaced with mock data. I don't have a simple solution for that though, so let's just go with this for now. |
I Agree it should be solved in a more proper way, and its not an easy fix, That's the reason i used explicitly undefined ( !== ) so the user can return null for specifying undefined behavior. |
@DxCx sorry, I'm not sure why you are saying my query is incorrect.
If you look at the schema:
Unless I'm mistaken, the above implies that |
Hi @sebastienbarre, - const mockMap = {};
+ const mockMap = {
+ Int: () => 123,
+ };
const resolvers = {
RootQuery: {
- returnInt: () => 123,
+ returnInt: () => 124,
},
};
addResolveFunctionsToSchema(jsSchema, resolvers);
addMockFunctionsToSchema({ schema: jsSchema, mocks: mockMap, preserveResolvers: true });
const testQuery = `{
- returnObject { returnInt, returnString }
+ returnObject { returnInt, returnString }, returnInt
}`;
const expected = {
returnObject: { returnInt: 123, returnString: 'Hello World' },
+ returnInt: 124
};
return graphql(jsSchema, testQuery).then((res) => {
expect(res.data).to.deep.equal(expected);
});
}); which shows that returnObj int is mocked |
I've noted a bug with @sebastienbarre 's new logic to merge resolvers & mocks (#115)
Basiclly, if there is a connector or a logger (or any other wrapper, hidden as a resolve function),
the original resolve function value will be undefined (as it's not provided)
and then an undefined will be returned, instead of the proper mocked value.
TODO: