Apollo Storybook Decorator
Wrap your React Storybook stories with Apollo Client.
The Gist
Getting Started
yarn add apollo-storybook-decorator -D
npm install apollo-storybook-decorator --save-dev
The Decorator:
type DecoratorType = {
typeDefs: string | Array<string>,
mocks: Object,
reducers?: Object,
reduxMiddlewares?: Array<Function> | ({ apolloClient }: MiddlewaresType) => Array<Function>,
apolloClientOptions?: Object,
typeResolvers?: Object,
context?: Object,
rootValue?: Object,
}
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-decorator';
import typeDefs from '../wherever/your/typeDefs/live';
import mocks from '../wherever/your/mocks/live';
addDecorator(
apolloStorybookDecorator({
typeDefs,
mocks
})
);
function loadStories() {
}
configure(loadStories, module);
Quick Example
import React from 'react';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import { storiesOf } from '@storybook/react';
import apolloStorybookDecorator from 'apollo-storybook-decorator';
const typeDefs = `
type Query {
helloWorld: String
}
schema {
query: Query
}
`;
const mocks = {
Query: () => {
return {
helloWorld: () => {
return 'Hello from Apollo!!';
},
};
},
};
let HelloWorld = function HelloWorld({ data }) {
const hello = data && data.helloWorld;
if (data && data.loading) {
return <h1>Loading one second please!</h1>
}
return <h1>{hello}</h1>
};
HelloWorld = graphql(
gql`
query hello {
helloWorld
}
`,
)(HelloWorld);
storiesOf('Apollo Client', module)
.addDecorator(
apolloStorybookDecorator({
typeDefs,
mocks,
}),
)
.add('Hello World Test', () => {
return <HelloWorld />;
});
Development
To get started, clone this repo and run the following command:
$ yarn
To run the project's examples, run:
$ yarn storybook