Skip to content

Instantly share code, notes, and snippets.

@lorensr
Forked from helfer/customizable-mocking.js
Last active April 11, 2016 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lorensr/d2035f6563b5f37a074a242b8faf3008 to your computer and use it in GitHub Desktop.
Save lorensr/d2035f6563b5f37a074a242b8faf3008 to your computer and use it in GitHub Desktop.
import { mockServer, MockList } from 'graphql-tools';
import casual from 'casual-browserify';
// 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