graphql-mocks
Build web applications today against the GraphQL APIs of tomorrow.
Setup once. Run in node, the browser, and
Setup once. Run in node, the browser, and
localhost
with tools that scale mocks with the complexity of your GraphQL API.Highlight
Use Highlight
to flexibly select parts of the schema to mock and modify
Mock
Flexibly define resolver behavior with wrappers and middlewares to cover even the most complex mocking scenarios
Store
Persist mutations and control stateful data with a graphql-paper
in-memory store
Resolve
Resolve GraphQL queries in Node and the Browser with the provided GraphQLHandler
Network
Write once, use everywhere. Network Handlers bring your mocks to node, the browser, and more.
CLI
Use the gqlmocks
CLI to fetch remote schemas, generate boilerplate and run mock servers on localhost
.
Demo
⚠️ Warning: No servers are used, or harmed, in the resolving of these GraphQL queries
To quickly show a few of the features in action here we have:
- Setting up a GraphQL query handler and making a query
- Using
graphql-paper
package, an in-memory graphql store, which allows stateful queries (try a mutation and see the change persist in subsequent queries) - An embedded
logWrapper
highlighted on all root-level Query resolvers for logging. Check the developer console for helpful logging after each query run.
Go ahead and do a few queries and mutations (see changes persist)
GraphiQL
Query Variables
Request Headers
The Code
// 1. Setup your handler
const handler = new GraphQLHandler({
// optionally, provide a base resolver map
resolverMap,
// use middlewares from packages, make your own,
// and embed resolver wrappers
middlewares: [
embed({
// Highlight makes it easy to declaratively select
// what resolvers should be wrapped with the `logWrapper`
highlight: (h) => h.include(field(['Query', '*']))
wrappers: [logWrapper],
}),
]
// add dependencies needed by middlewares or resolvers
dependencies: {
graphqlSchema,
paper: new Paper(graphqlSchema)
},
});
// 2. Run queries (or mutations)!
handler.query(`
query {
...
}
`);