Mock GraphQL with Ember CLI Mirage
This addon is for mocking GraphQL with Ember CLI Mirage.
Note
This addon is very early in its development and the code currently supports limited use cases. If you would like to contribute, don't be afraid to trample any existing code. It's quite trampleable.
Installation
ember install ember-cli-mirage
ember install ember-cli-mirage-graphql
How It Works
This addon creates a request handler for use with Mirage that takes your GraphQL scehma and creates mocks accordingly. In its simplest form, the mocks query data from Mirage's database, by type, and optionally filter records by matching query variables.
Example Usage
Quick Example
See the dummy app in this addon's tests folder for a complete example. Open the Mirage folder and notice the added handlers
folder. This is where we created the GraphQL request handler callback. It can be imported in the Mirage config for use with your GraphQL request handler.
Note: There is no need to create a separate module for your handler, as we did, but we think it's nice to keep all the handler options out of the Mirage config.
Basic Example
If you're like us, you'll want to create your handler aside from your Mirage config:
import createGraphQLHandler from 'ember-cli-mirage-graphql/handler';
import schema from 'app-name/path-to-your-schema';
export default createGraphQLHandler(schema, );
Note: We use a simple command line tool to download our schema and then we wrap it in a template string and save it as a JavaScript module. The tool can be found here: get-graphql-schema.
Then import your handler for use in your Mirage config:
import graphQLHandler from './handlers/grapqhl';
export default function() {
this.post('/path-to-graphql', graphQLHandler);
}
Handler Options
You may pass in options when creating a request handler. The options take the form of a hash and may contain the following:
{
fieldsMap: {
Person: {
pets: 'animals'
}
},
mutations: {
updatePerson: (people, vars, db) => {
let { id, personAttributes } = vars;
return [ people.update(id, personAttributes) ];
}
},
varsMap: {
Person: {
pageSize: (records, _, pageSize) => records.slice(0, pageSize)
}
}
}
Contributing
Any contributors are most welcome!
Please file issues, as appropriate. Feature requests would be nice in the form of use cases. Knowing how you are using GraphQL should help contributors make the addon more capable.
PRs are also welcome, provided they relate to an issue or add functionality for a certain use case, and should generally follow the same coding style as the rest of the addon.