#Mocks
Mocks are the simplest way to customize scalar types (such as String
or Int
) and type fields (such as Book.title
).
#Scalar types
To customize scalar types, specify a function for each type:
Poser already mocks a few scalar types:
You can overwrite these defaults by specifying your own, but they will only have effect in the service they live in.
#Type fields
Every field of every type (including Query
and Mutation
) can have a specific mock to return something else than the scalar type mock.
To customize type fields, specify a function for each field:
This can also be done for Query fields, mostly to modify the default list length of 2:
#Constructor
In some cases, some fields depend on others, so mocking them one by one in isolation won't produce a result realistic enough.
For example, let's assume a type Task
with the following fields:
If these 2 fields must be coherent, we can't have a date in completedAt
unless progress
is set to 1.
This can be solved by using the __constructor
function:
The __constructor
function will be called everytime a new instance is created, including store.insert()
. When calling store.insert()
, the second parameter contains values and is passed as the first parameter of __constructor
. These values should be included in the return value of __constructor
as shown above.