I am using graphql-tool to mock up data for testing.
I hope to simulate when I select a user, it opens a detail page and shows the user company info.
Query
const query = `
query User($id: ID!) {
user(id: $id) {
id
company
}
}
`;
Mock server
import { addMockFunctionsToSchema } from 'graphql-tools';
import casual from 'casual';
const allUserIds = ['u1', 'u2', 'u3'];
const mocks = {
User: () => ({
id: casual.random_element(allUserIds),
name: casual.name,
company: casual.company_name
})
};
addMockFunctionsToSchema({ schema, mocks });
However, right now, when I query with argument id 'u1'
, it will return a random user id for example 'u2'
, which gives me a little trouble to show it in front end.
I thought I can do something like this below, but turns out I am wrong. user
is undefined
in the code below.
const mocks = {
User: (user) => ({
id: user.id || casual.random_element(allUserIds),
name: casual.name,
company: casual.company_name
})
};
Is there a way to pass the query arguments in graphql-tools? Thanks
casual.seed(123);
. First, I tried to put on top just after import, it does not have any effect. Then I tried to put in mocks likeconst mocks = { User: () => { casual.seed(0); return { id: casual.random_element(allUserIds), ... } } };
, it will give me all users with same info. – Haematothermal