Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

graphql-mock-object

ericclemmons2MIT2.0.0

Prototype your UI with GraphQL mock { ... } objects before defining complex types & resolvers.

readme

graphql-mock-object

Prototype your UI with GraphQL mock { ... } objects before defining complex types & resolvers.

GraphQL works best as a backend for the frontend (BFF) that matches the structure of your UI. graphql-mock-object makes it simple for your UI to request data however you'd like. Once you're done prototyping, you can replace mocks with real resolvers and return real data.

Installation

I recommend saving into devDependencies, as prototyping is typically done in development, not production.

yarn add --dev graphql-mock-object
# ...or...
npm install --save-dev graphql-mock-object

Then add it to your application:

import { graphiqlExpress, graphqlExpress } from "apollo-server-express"
import bodyParser from "body-parser"
import express from "express"
import { makeExecutableSchema } from "graphql-tools"

// 👇 All the dependencies we need
import { mock, MockObject, typeDefs } from "graphql-mock-object"

const schema = makeExecutableSchema({
  typeDefs: [
    ...typeDefs, // 👈 All the mock types we're dependent on
    `type Query { version: String }`, // 👈 Your existing `Query`
    `extend type Query { mock: MockObject! }`, // 👈 Add `mock` to `Query`
  ],
  resolvers: {
    MockObject, // 👈 This resolves all mock properties
    Query: {
      mock, // 👈 This is needed to query `mock`
      version() {
        return require("../../../package.json").version
      },
    },
  },
})

// The rest of your app...
export const app = express()
  .get("/", (req, res) => res.redirect("/graphiql"))
  .use("/graphql", bodyParser.json(), graphqlExpress({ schema }))
  .use("/graphiql", graphiqlExpress({ endpointURL: "/graphql" }))
  .listen(3000, function(err) {
    if (err) {
      console.error(err)
      return
    }

    console.log("Listening at http://localhost:3000")
  })

Author

Eric Clemmons (@ericclemmons)

changelog

Change Log

All notable changes to this project will be documented in this file.

This project adheres to Semantic Versioning.

2018-06-01 - [3.3.0 - current version]

  • 3.3.0 - (5) - Add all faker functions to MockObject (@ericclemmons)

2018-05-22

  • 3.2.1 - (bc350aa) - v3.2.0 (@ericclemmons)
  • 3.2.0 - (4) - Add "seed" to Mock (@ericclemmons)
  • 3.1.6 - (1734308) - Add jest tests (@ericclemmons)
  • 3.1.5 - (989de4e) - Upgrade dependencies (@ericclemmons)

2018-05-20

  • 3.1.4 - (6faaf84) - Revert "Merge pull request #6 from ericclemmons/6-graphql-yoga" This reverts commit 812554ff5e25434bcd51441a95f07a109a3af2bc, reversing changes made to 88bfa8bab36c19f5b24c0e89698a2b5c146cd679. (@ericclemmons)
  • 3.1.3 - (53da9e3) - Add "yarn dev" (@ericclemmons)
  • 3.1.2 - (57a9847) - 3.1.0 (@ericclemmons)
  • 3.1.1 - (733b244) - 3.1.0 (@ericclemmons)
  • 3.1.0 - (6) - Add graphql-yoga example (@ericclemmons)
  • 3.0.1 - (88bfa8b) - 3.0.0 (@ericclemmons)
  • 3.0.0 - (3) - Simplify usage (@ericclemmons)
  • 2.0.3 - (edcee13) - v2.0.0 (@ericclemmons)
  • 2.0.2 - (d048696) - Convert faker ES5 => ES6 (@ericclemmons)
  • 2.0.1 - (9c7bae4) - v2.0.0 (@ericclemmons)
  • 2.0.0 - (2) - Faker integration (@ericclemmons)

2018-05-06

  • 1.0.7 - (24ba681) - Bump to v1.0.6 to match changelog (@ericclemmons)
  • 1.0.6 - (71535f1) - v1.0.0 (@ericclemmons)
  • 1.0.5 - (f3724c3) - yarn github-semantic-version --init (@ericclemmons)
  • 1.0.4 - (0a990eb) - Add scripts.changelog (@ericclemmons)
  • 1.0.3 - (88fdf1b) - yarn github-semantic-version --init (@ericclemmons)
  • 1.0.2 - (e0dbb5b) - Attempt to fix oao publish version (@ericclemmons)
  • 1.0.1 - (ca35224) - yarn github-semantic-version --init (@ericclemmons)
  • 1.0.0 - (1) - MockObject (@ericclemmons)

2018-05-04

  • 0.0.2 - (e6535e8) - Initial working schema (@ericclemmons)

2018-04-30

  • 0.0.1 - (62dc904) - first commit (@ericclemmons)