Skip to main content

Quick Example

This example will show how to:

  1. Create a Resolver Map with Resolver functions
  2. Apply a Resolver Wrapper to Resolver functions, the Sinon Spy Wrapper spyWrapper (via @graphql-mocks/sinon) by using embed. The spy wrapper will capture all calls to our resolver function which can be useful for testing.
  3. Setup a GraphQL Handler with the the Resolver Wrapper to be able to execute queries
  4. Execute a Query with the GraphQL Handler
  5. Check the state object for the results of the Sinon Spies
import { GraphQLHandler, embed } from "graphql-mocks";
import { spyWrapper } from "@graphql-mocks/sinon";

// this string represents our schema formatted in
// GraphQL SDL (Schema Definition Language), but
// a GraphQL Instance or SDL String can be used
const graphqlSchema = `
schema {
query: Query

type Query {
helloWorld: String!

const resolverMap = {
Query: {
helloWorld() {
return "Hello from our test resolver!";

// Create a query handler with the GraphQL Schema, Resolver Map, and embedded wrappers
const handler = new GraphQLHandler({
middlewares: [
wrappers: [spyWrapper],

dependencies: {

// Send the query
const query = handler.query(`

// console.log the result and the sinon spies that were applied to
// the resolver
query.then((result) => {

First console.log

  "data": {
    "helloWorld": "Hello from our test resolver!"

Second console.log

"Hello from our test resolver!"

And that's an end-to-end example using the library to create a quick mock GraphQL API with the Sinon Spy Wrapper for introspection. Use the existing out-of-the-box Resolver Wrapper and Resolver Map Middlewares for even more functionality, or create your own.