Coder Social home page Coder Social logo

gaybro8777 / apollo-storybook-decorator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from abhiaiyer91/apollo-storybook-decorator

1.0 0.0 0.0 2.65 MB

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

JavaScript 73.08% HTML 2.97% TypeScript 22.19% Vue 1.76%

apollo-storybook-decorator's Introduction

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

apollo-storybook-decorator's People

Contributors

1999 avatar abhiaiyer91 avatar austinamorusocfc avatar ezralalonde avatar jschlieber avatar lucidpaper avatar m3hari avatar macrozone avatar ndelangen avatar pixelstew avatar remolueoend avatar sky-franciscogoncalves avatar travis avatar xavxyz avatar xfumihiro avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.