• Stars
    star
    332
  • Rank 126,957 (Top 3 %)
  • Language
    JavaScript
  • Created over 7 years ago
  • Updated almost 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Wrap your storybook environment with Apollo Client, provide mocks for isolated UI testing with GraphQL

Apollo Storybook Decorator

logo

Wrap your React Storybook stories with Apollo Client.

Supports

React React Native Vue Angular
Apollo Client V2 Npm download Npm download Npm download Coming Soon
Apollo Client V1 Npm download X X X

The Gist

Take this:

import React from "react";
import { Query } from "react-apollo";
import gql from "graphql-tag";
import { storiesOf } from "@storybook/react";

const userQuery = gql`
  query getUser = {
    currentUser {
      name
      lastAction {
        message
      }
      avatar
      city
    }
  }
`;

function CurrentUser() {
  return (
    <Query query={userQuery}>
      {({ loading, data }) => {
        const user = data && data.currentUser;
        if (loading) {
          return <h1>Loading one second please!</h1>;
        }
        return (
          <div>
            <img src={user.avatar} />
            <h1>
              {user.name}
              from {user.city}
              said "{user.lastAction.message}"{" "}
            </h1>
          </div>
        );
      }}
    </Query>
  );
}

storiesOf("Apollo Client", module).add("Current User", () => {
  return <CurrentUser />;
});

To Render this:

example1Book

Getting Started

For Apollo Client 2.x (React)

yarn add apollo-storybook-react -D

npm install apollo-storybook-react --save-dev

Full Example

import React from "react";
import { Query } from "react-apollo";
import gql from "graphql-tag";
import { storiesOf } from "@storybook/react";
import apolloStorybookDecorator from "apollo-storybook-react";

const typeDefs = `
  type Query {
    helloWorld: String
  }

  schema {
    query: Query
  }
`;

const mocks = {
  Query: () => {
    return {
      helloWorld: () => {
        return "Hello from Apollo!!";
      }
    };
  }
};

function HelloWorld() {
  return (
    <Query
      query={gql`
        query hello {
          helloWorld
        }
      `}
    >
      {({ loading, data }) => {
        const hello = data && data.helloWorld;

        if (loading) {
          return <h1>Loading one second please!</h1>;
        }

        return <h1>{hello}</h1>;
      }}
    </Query>
  );
}

storiesOf("Apollo Storybook Decorator", module)
  .addDecorator(
    apolloStorybookDecorator({
      typeDefs,
      mocks
    })
  )
  .add("Hello World Test", () => {
    return <HelloWorld />;
  });

Usage

You can add the decorator at a per story basis:

storiesOf("Apollo Client", module).addDecorator(
  apolloStorybookDecorator({
    typeDefs,
    mocks
  })
);

or you can add it to all stories, head to your storybook config.js

import { configure, addDecorator } from "@storybook/react";
import apolloStorybookDecorator from "apollo-storybook-react";
import typeDefs from "../wherever/your/typeDefs/live";
import mocks from "../wherever/your/mocks/live";

addDecorator(
  apolloStorybookDecorator({
    typeDefs,
    mocks
  })
);

function loadStories() {
  // stories...
}

configure(loadStories, module);

Options

type DecoratorType = {
  //string representing your graphql schema, if you use tools like `babel-plugin-inline-import` you can import this from a  .graphql file
  typeDefs: string | Array<string>,
  // object that resolves the keys of your graphql schema
  mocks: Object,
  apolloClientOptions?: Object,
  apolloLinkOptions?: Object,
  // optional typeResolvers for complex mocking
  typeResolvers?: Object,
  // optional context
  context?: Object,
  // optional root value
  rootValue?: Object,
  // optional resolver validation options, see: https://git.io/fALf4
  resolverValidationOptions?: Object
};

resolverValidationOptions

This option gets passed directly to makeExecutableSchema of graphql-tools, as described at https://git.io/fALf4. This allows you to override requireResolversForResolveType and other validation flags:

storiesOf("Apollo Client", module).addDecorator(
  apolloStorybookDecorator({
    typeDefs,
    mocks,
    resolverValidationOptions: {
      requireResolversForResolveType: false
    }
  })
);

Development

This repo is split up using the lerna monorepo module.

To get started, clone this repo and run the following command:

$ yarn # install node deps
$ lerna bootstrap

To run the project's examples, run:

Current storybook is enabled in apollo-storybook-react and apollo-storybook-v1

$ yarn storybookReact

or for v1

$ yarn storybookV1

More Repositories

1

How-We-Redux-Todos

A Todo App using Redux and Meteor.js
JavaScript
135
star
2

apollo-fragment

Use Apollo Link State to connect components to GraphQL fragments in the Apollo Cache
TypeScript
111
star
3

GraphQL-Chat

Demonstrate GraphQL Subscriptions with a Chatroom Service
JavaScript
80
star
4

sample-graphql-meteor-server

GraphQL Server via Apollo & Meteor
JavaScript
52
star
5

redux-reset

Higher Order Reducer for resetting multiple parts of your state tree
JavaScript
51
star
6

meteor-pg-apollo-shopping-cart

Shopping Cart example with Meteor, Postgres, and Apollo
JavaScript
46
star
7

apollo-forms

Apollo Form Library
JavaScript
37
star
8

recompose-apollo

Recompose HOCs for React Apollo
JavaScript
36
star
9

apollo-youtube-search

Use Youtube video api to search and embed videos
JavaScript
34
star
10

prisma-lrucache-middleware

Prisma Middleware for caching results of prisma queries
TypeScript
24
star
11

prisma-opentracing-middleware

Middleware for prisma that traces client operations using any opentracing based tracer
TypeScript
22
star
12

meteor-apollo-counter

Simple counter example with Meteor + Mongo + Apollo
JavaScript
21
star
13

GraphQL-Client-Exploration

Simple exploration of GraphQL Clients
JavaScript
18
star
14

graphql-currency-scalars

Format Currency in your GraphQL servers
JavaScript
16
star
15

GraphQL-Chat-React

Explore GraphQL Subscriptions with a React Chat UI
JavaScript
14
star
16

ZenActions

Action Creators and mixin interface for MeteorJS
JavaScript
13
star
17

meteor-instagram-apollo

Instagram API Search with Apollo
JavaScript
11
star
18

DopeGraphqlServer

Collection of Connectors and Resolvers to show off Apollo server
JavaScript
11
star
19

Minesweeper-ReactRedux

Minesweeper in React Redux
JavaScript
8
star
20

redux-method-middleware

Similar to Promise Middleware, handle asynchronous callback functions via Redux Middleware
JavaScript
8
star
21

Meteor-Twilio

Twilio Bindings for Meteorjs
JavaScript
6
star
22

YourFirstGraphQLComponent

JavaScript
5
star
23

prisma-credits

Creating an in-app credits system with Prisma :)
JavaScript
5
star
24

exploring-moralis

JavaScript
3
star
25

useful-system

Pluggable and Useful system abstractions
TypeScript
2
star
26

abhi-plugin-fastly

Experiment
JavaScript
1
star
27

Zen

Flux Architecture specifically for MeteorJS Applications
JavaScript
1
star
28

clique

An application that helps you assemble groups of people to go to events in your area.
JavaScript
1
star
29

jekyll-prac

JavaScript
1
star
30

AnthroDocsOfficial

JavaScript
1
star
31

countrycapitals

JavaScript
1
star
32

ZenStore

A Class that represents state on the client via MeteorJS and Minimongo
JavaScript
1
star
33

graphql-service-generator

CLI to generate GraphQL Service Bindings and Typescript Definitions
TypeScript
1
star
34

abhi-content-cloud-test

1
star
35

MeteorReactWebpackBoilerplate

A Boilerplate Repo using MeteorJS, ReactJS, and Webpack as the module loader.
JavaScript
1
star