Skip to content

Instantly share code, notes, and snippets.

@ticidesign
Created July 8, 2019 04:35
Show Gist options
  • Save ticidesign/e73f6e0a0f06757bf3b3dd8bc07b61ad to your computer and use it in GitHub Desktop.
Save ticidesign/e73f6e0a0f06757bf3b3dd8bc07b61ad to your computer and use it in GitHub Desktop.
Customizable Mocking
// customize mocking per type (i.e. Integer, Float, String)
mockServer(schema, {
Int: () => 6,
Float: () => 22.1,
String: () => 'Hello',
});
// customize mocking per field in the schema (i.e. for Person.name and Person.age)
mockServer(schema, {
Person: () => ({
name: casual.name,
age: () => casual.integer(0,120),
})
});
// mock lists of specific or random length( and lists of lists of lists …)
mockServer(schema, {
Person: () => {
// a list of length between 2 and 6
friends: () => new MockList([2,6]),
// a list of three lists of two items: [[1, 1], [2, 2], [3, 3]]
listOfLists: () => new MockList(3, () => new MockList(2)),
},
});
// customize mocking of a field or type based on the query arguments
mockServer(schema, {
Person: () => {
// the number of friends in the list now depends on numPages
paginatedFriends: (o, { numPages }) => new MockList(numPages * PAGE_SIZE),
},
});
// You can also disable mocking for specific fields, pass through to the backend, etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment