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

mock unions and interfaces #102

Merged
merged 1 commit into from
Aug 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 10 additions & 2 deletions src/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,19 @@ function addMockFunctionsToSchema({ schema, mocks = {}, preserveResolvers = fals
// XXX we recommend a generic way for resolve type here, which is defining
// typename on the object.
if (fieldType instanceof GraphQLUnionType) {
return { typename: getRandomElement(fieldType.getTypes()) };
const randomType = getRandomElement(fieldType.getTypes());
return {
typename: randomType,
...mockType(randomType)(...args),
};
}
if (fieldType instanceof GraphQLInterfaceType) {
const possibleTypes = schema.getPossibleTypes(fieldType);
return { typename: getRandomElement(possibleTypes) };
const randomType = getRandomElement(possibleTypes);
return {
typename: randomType,
...mockType(randomType)(...args),
};
}
if (fieldType instanceof GraphQLEnumType) {
return getRandomElement(fieldType.getValues()).value;
Expand Down
7 changes: 5 additions & 2 deletions test/testMocking.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ describe('Mock', () => {
}`;
const mockMap = {
Int: () => 12345,
Bird: () => ({ returnInt: () => 54321 }),
Bee: () => ({ returnInt: () => 54321 }),
};
return mockServer(shorthand, mockMap).query(testQuery).then((res) => {
expect(res.data.returnInt).to.equal(12345);
Expand All @@ -148,8 +150,9 @@ describe('Mock', () => {
expect(res.data.returnString).to.be.a('string');
expect(res.data.returnID).to.be.a('string');
// tests that resolveType is correctly set for unions and interfaces
expect(res.data.returnBirdsAndBees[0].returnInt).to.equal(12345);
expect(res.data.returnBirdsAndBees[1].returnInt).to.equal(12345);
// and that the correct mock function is used
expect(res.data.returnBirdsAndBees[0].returnInt).to.equal(54321);
expect(res.data.returnBirdsAndBees[1].returnInt).to.equal(54321);
});
});
it('does not mask resolveType functions if you tell it not to', () => {
Expand Down