getExecutableSchema
Returns an executable schema with data that's mocked according to the mock providers that are supplied.
API
getExecutableSchema(options)
makeExecutableSchema
takes a single argument: an object of options. Only the typeDefs
option is required.
options | Code |
---|---|
typeDefs * | type Query { ... |
mockProvidersFn | (context) => ({ scenario: ..., builders: ... }) |
mutationResolversFn | (store, buildMocks, context) => ({ [mutationName]: (info, args, ...) => { ... } }) |
mockProviders | { scenario: ..., builders: ... } |
We're now going to go over each option in detail.
typeDefs
*
Required. A GraphQL schema language string that contains the schema definition. Putting it another way, it's what usally resides in your schema.graphql
file.
mockProvidersFn
(context) => ({ scenario: ..., builders: ... })
Optional. A function that receives the resolver context as an argument and needs to return an object containing mock providers that Kimera will be using to generate mocks.
scenario
is an object containing the mocks for theQuery
type. You can read an in depth explanation of what the scenario is in the "What is a scenario?" section of the "Mocking queries" page of the docs.builders
is an object that maps GraphQL types to functions. You can read more about what how they work in the "Mocking types" section of the docs.
note
"Mock providers" is just another way of saying "the scenario and all of the builders that were defined".
mutationResolversFn(store, buildMocks, context)
Optional. A function that returns an object that maps Mutation names to resolvers.
store
is an object which holds all of the mocks for our schema. It defined two methods:store.get(path = '')
: Theget
method will accept an optionalpath
string, and return the mocked value stored at that specific path.store.update(path, updateValue)
: Theupdate
method will update the value at the supplied path with the new value. If the updated value is an object, the new value will be deeply merged over the existing value.
buildMocks('TypeName', scenario)
is a function that mocks a specific type using existing mock providers, and optionally, a custom scenario that we can provide at execution.
You can see examples of store
and buildMocks
in action on the "Mocking Mutations" page.
mockProviders
Optional. An object that needs to have the same structure as the result of mockProvidersFn
. These mock providers will overwrite the ones returned by mockProvidersFn
by performing a deep object merge.
note
The purpose of this argument is to provide a mechanism to overwrite the default mock providers from outside of our server if need be, e.g. from a React app.
tip
It's useful to think of these as custom mock provider defintions, and the ones defined in mockProvidersFn
as default defintions.
The code above will result in having the me
query have the following shape:
note
When merging scenarios the array fields are overwritten, while objects are deeply merged
Builders that collide are replaced with the custom definitition.