Norbert de Langen a039c96426
Merge branch 'master' into babel-7
# Conflicts:
#	MIGRATION.md
#	addons/backgrounds/package.json
#	addons/events/package.json
#	addons/info/package.json
#	addons/info/src/__snapshots__/index.test.js.snap
#	addons/jest/package.json
#	addons/knobs/package.json
#	addons/links/package.json
#	addons/notes/package.json
#	addons/options/package.json
#	addons/storyshots/storyshots-core/package.json
#	addons/storyshots/storyshots-puppeteer/package.json
#	addons/storysource/package.json
#	addons/viewport/package.json
#	app/angular/package.json
#	app/html/package.json
#	app/marko/package.json
#	app/mithril/package.json
#	app/polymer/package.json
#	app/react/package.json
#	app/vue/package.json
#	examples/angular-cli/package.json
#	examples/cra-kitchen-sink/package.json
#	examples/html-kitchen-sink/package.json
#	examples/marko-cli/package.json
#	examples/mithril-kitchen-sink/package.json
#	examples/official-storybook/package.json
#	examples/official-storybook/stories/__snapshots__/addon-info.stories.storyshot
#	examples/official-storybook/stories/__snapshots__/other-dirname.stories.storyshot
#	examples/polymer-cli/package.json
#	examples/vue-kitchen-sink/package.json
#	jest.config.js
#	lib/cli/package.json
#	lib/cli/test/snapshots/angular-cli/package.json
#	lib/cli/test/snapshots/marko/package.json
#	lib/cli/test/snapshots/meteor/package.json
#	lib/cli/test/snapshots/mithril/package.json
#	lib/cli/test/snapshots/polymer/package.json
#	lib/cli/test/snapshots/react/package.json
#	lib/cli/test/snapshots/react_project/package.json
#	lib/cli/test/snapshots/react_scripts/package.json
#	lib/cli/test/snapshots/sfc_vue/package.json
#	lib/cli/test/snapshots/update_package_organisations/package.json
#	lib/cli/test/snapshots/vue/package.json
#	lib/cli/test/snapshots/webpack_react/package.json
#	lib/codemod/package.json
#	lib/components/src/layout/__snapshots__/index.stories.storyshot
#	lib/core/package.json
#	lib/core/src/server/config/webpack.config.prod.js
#	package.json
#	yarn.lock
2018-08-15 00:01:36 +02:00
..
2018-07-31 19:34:05 +03:00

Storybook GraphiQL Addon

Storybook GraphQL Addon can be used to display the GraphiQL IDE with example queries in Storybook.

Framework Support

Screenshot

Getting Started

First, install the addon

npm install -D @storybook/addon-graphql

Import the setupGraphiQL function and use it to create the graphiql helper with a base url.

import { storiesOf } from '@storybook/react'
import { setupGraphiQL } from '@storybook/addon-graphql'

// setup the graphiql helper which can be used with the add method later
const graphiql = setupGraphiQL({ url: 'http://localhost:3000/graphql' });

storiesOf('GraphQL Demo', module)
  .add('get user info', graphiql(`{
    user(id: "1") {
      name
    }
  }`));

Tip: try creating the helper in another file and import the configured graphiql helper from it

Advanced Setup

The setupGraphiQL function also accepts a fetcher parameter which can be used to change how graphiql gets data. If the fetcher parameter is not given, it'll create a fetcher which uses the fetch api to make requests. The above example can also be written using a custom fetcher.

import { storiesOf } from '@storybook/react'
import { setupGraphiQL } from '@storybook/addon-graphql'

import { url } from './settings';

const fetcher = params => {
  const options = {
    method: 'post',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(params),
  };
  return fetch(url, options).then(res => res.json());
};

// create the helper with a custom fetcher
const graphiql = setupGraphiQL({ fetcher });

storiesOf('GraphQL Demo', module)
  .add('get user info', graphiql(`{
    user(id: "1") {
      name
    }
  }`));