{"id":21818586,"url":"https://github.com/focusreactive/storybook-graphql-kit","last_synced_at":"2025-04-14T02:28:14.186Z","repository":{"id":53671792,"uuid":"213474829","full_name":"focusreactive/storybook-graphql-kit","owner":"focusreactive","description":"Write GraphQL queries and pass response data to your components","archived":false,"fork":false,"pushed_at":"2021-03-19T21:04:39.000Z","size":4010,"stargazers_count":29,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-14T02:28:04.900Z","etag":null,"topics":["development","documentation","focusreactive","graphcms","graphql","graphql-variables","query","react","storybook","storybook-addon","tool","ui"],"latest_commit_sha":null,"homepage":"https://focusreactive.github.io/storybook-addon-graphcms/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/focusreactive.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-07T19:59:51.000Z","updated_at":"2024-10-16T11:45:20.000Z","dependencies_parsed_at":"2022-09-04T10:13:07.359Z","dependency_job_id":null,"html_url":"https://github.com/focusreactive/storybook-graphql-kit","commit_stats":null,"previous_names":["focusreactive/storybook-addon-graphcms"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/focusreactive%2Fstorybook-graphql-kit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/focusreactive%2Fstorybook-graphql-kit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/focusreactive%2Fstorybook-graphql-kit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/focusreactive%2Fstorybook-graphql-kit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/focusreactive","download_url":"https://codeload.github.com/focusreactive/storybook-graphql-kit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248810748,"owners_count":21165175,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["development","documentation","focusreactive","graphcms","graphql","graphql-variables","query","react","storybook","storybook-addon","tool","ui"],"created_at":"2024-11-27T16:13:36.276Z","updated_at":"2025-04-14T02:28:14.166Z","avatar_url":"https://github.com/focusreactive.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Storybook GraphQL Kit [![npm version](https://badge.fury.io/js/%40focus-reactive%2Fstorybook-graphql-kit.svg)](https://badge.fury.io/js/%40focus-reactive%2Fstorybook-graphql-kit) [![Storybook](https://raw.githubusercontent.com/storybookjs/storybook-addon-console/master/docs/storybook.svg?sanitize=true)](https://focusreactive.github.io/storybook-graphql-kit)\n\n[![Storybook](https://raw.githubusercontent.com/focusreactive/storybook-graphql-kit/master/docs/storybook-logo.png)](https://storybook.js.org/)\n[![GraphQL](https://raw.githubusercontent.com/focusreactive/storybook-graphql-kit/master/docs/graphql.png)](https://graphql.org/)\n\nWrite GraphQL queries and pass response data to your components **or just display them**\n\n## Why you want to use it\n\n### 1. Develop components\n\nDevelop your components in isolation with actual backend stored data. With this addon you can add query to your stories and use fetched data as props for your components. You are flexible to use it with a single component or return an array of components - everything you need is just to write stories.\n\n![Screenshot1](https://raw.githubusercontent.com/focusreactive/storybook-graphql-kit/master/docs/screenshot-example1.png)\n\n### 2. Browse database\n\nYou can simply use Storybook and this addon for rapid development of tools to browse your GraphQL endpoint. Just write the queries for data you want to show and it will be outputted as a table. If you have arrays, nested objects or images inside your query it will be turned to friendly visible form. Good for documenting things withing your team.\n\n![Screenshot2](https://raw.githubusercontent.com/focusreactive/storybook-graphql-kit/master/docs/screenshot-example2.png)\n\n\n## Usage\n\nInstall addon\n\n```shell\nyarn add --dev @focus-reactive/storybook-graphql-kit\n```\n\nAdd addon to your Storybook\n\n```js\n// addons.js\n\nimport \"@focus-reactive/storybook-graphql-kit/register\";\n```\n\nConfigure addon\n\n```js\n// config.js or in your stories.js\nimport { addDecorator } from '@storybook/react';\nimport { withGraphQL } from \"@focus-reactive/storybook-graphql-kit\";\n\naddDecorator(\n  withGraphQL({\n    endpoint: YOUR_GRAPHQL_ENDPOINT,\n    token: YOUR_GRAPHQL_TOKEN,\n  })\n);\n\n```\n\nWrite stories\n\n```js\n// stories.js\n\nimport { QueryParams } from '@focus-reactive/storybook-graphql-kit';\n\nexport default {\n  title: 'Browse GraphQL',\n};\n\n// write a query in your preferred way\nconst query = /* GraphQL */ `\n  query($conferenceTitle: ConferenceTitle, $eventYear: EventYear, $name: String) {\n    result: speakers(\n      where: {\n        conferenceEvents_some: { year: $eventYear, conferenceBrand: { title: $conferenceTitle } }\n        name_contains: $name\n      }\n    ) {\n      id\n      bio\n      name\n      talks {\n        title\n      }\n      avatar {\n        mimeType\n        url\n      }\n    }\n  }\n`;\n\n// In Storybook v.5 you can simply export you stories from `stories.js` files. You can use `storiesOf(...).add(...)` syntax if prefer\nexport const speakers = ({ graphQlResponse }) =\u003e {\n  const data = graphQlResponse.result;\n  return null;\n  // or pass data to your components\n};\n\n// You need to pass parameters to your story\nspeakers.story = {\n  name: 'Conference Speakers',\n  parameters: QueryParams({\n    query,\n    vars: { conferenceTitle: 'React_Day_Berlin', eventYear: 'Y2019' },\n    searchVars: { name: '' },\n    isConnected: true,\n  }),\n};\n\n\n```\n\nNote some details here:\n\n1. See how we return `null` from the story function. In this case the default UI will be used to display data in a table form. It's useful if you just want to quickly see the results of your request.\n2. If you return a React Component from your story it will behave as regular Storybook story except the additional field in the `story context` passed to the function that will contain fetched data. You story will be rendered **only after** data will be successfully received, so `({ graphQlResponse: { result } })` always has result of your query.\n3. You can pass variables in `vars` and `searchVars` fields to the query as you usually do for GraphQL requests. The difference of `searchVars` is that there will be input fields in the addon panel and users can use them for searching\n4. Settings passed to `withGraphQL` and to `QueryParams` are merged into one object before using. You can pass them in any order or even use only one of them. Just do it in the most convenient way for you\n\nInstead of passing options to the story how it's shown above you can `import { Query } from '@focus-reactive/storybook-graphql-kit';` and pass all options via this helper:\n\n```js\nimport { Query } from '@focus-reactive/storybook-graphql-kit';\n\nexport const speakers = Query({\n  name: 'Conference Speakers',\n  query,\n  vars: { conferenceTitle: 'React_Day_Berlin', eventYear: 'Y2019' },\n  searchVars: { name: '' },\n  viewId,\n});\n\n```\n\nIt does totally the same but in more compact way, especially if you only want to have the default view.\n\n## Features\n\n1. You don't need to write `stories` or even use `React` if you only want to view the results of queries. It has convenient default UI to display your data.\n2. Expands nested objects to display in flat table\n3. Detects `url` fields with image sources and outputs them\n4. Input fields for GraphQL variables in the addon panel\n5. Handles GraphQL requests and renders story only with successfully received data\n6. Works fine with any GraphQL endpoint\n\n## Related Addons\n\n- [Storybook Addon GraphCMS](https://github.com/focusreactive/storybook-graphql-kit-graphcms) thin extension with GraphCMS features\n\n## API\n\nYou need to pass options to addon. You can pass them directly as story parameters, with `withGraphQL` decorator or with `Query` helper.\n\n```js\n\nimport { Query } from '../../addon/decorator'\n\nexport const pages = Query({ ...options });\n\n```\n\n### Common options for any GraphQL service:\n\n`endpoint` - Your GraphQL project endpoint\n\n`token` - GraphQL bearer token\n\n`query` - your GraphQL query\n\n`vars` - GraphQL variables\n\n`searchVars` - GraphQL variables you will set in the addon panel\n\n### Stories options\n\n`name` - The name of the Story\n\n`story` - Story functions (if you don't use Storybook origin syntax)\n\n### Writing stories\n\nWhen you writing stories with your own components you can access the raw query response alongside with parsed results:\n\n```js\n\nexport const yourStory = ({ graphQlResponse }) =\u003e {\n  // Result of your query. Usually what you want to use for components inside your App\n  const data = graphQlResponse.result;\n  // Array with information about fetched fields. The same that used for displaying default table\n  const columns = graphQlResponse.columns;\n  // Array of receiving objects. Works fine together with `columns` to output the parsed data\n  const rows = graphQlResponse.rows;\n\n  return data.map(props =\u003e \u003cYouComponent {...props} /\u003e)\n};\n```\n\n`graphQlResponse.columns` and `graphQlResponse.rows` are useful if you creating own tool to display your GraphQL data. Otherwise use `graphQlResponse.result`.\n\n\n### Authoring Own Addons based on Storybook-GraphQL-Kit\n\nYou can use API to extend this addon with your own representations of receiving data.\n\n```js\nimport { withGraphQL } from '@focus-reactive/storybook-graphql-kit';\n\nconst customRenderer = ({ key, value, ind, options }) =\u003e {\n  if (key !== 'featured field') return null;\n\n  return {\n    id: key,\n    getLabel: () =\u003e `${key}`,\n    getValue: () =\u003e value,\n    // return your custom React component or null for using `getValue`\n    render: () =\u003e \u003cCustomComponent/\u003e,\n  };\n};\n\nexport const withYourServiceFeatures = withGraphQL\n   .addRender(customRenderer)\n   .addRender(customRenderer1)\n   .addRender(customRenderer2);\n\n```\n\nSee https://github.com/focusreactive/storybook-graphql-kit-graphcms as example of how to extend and create new addon\n\n## Contributing\n\nAny contribution are welcome to this project! Feel free to open an issue or start a PR.\n\nTo develop this project:\n\n1. git clone\n2. yarn\n3. yarn start\n4. open http://localhost:9009\n5. the package code is located inside `addon` folder\n\n## Credits\n\n\u003cdiv align=\"left\" style=\"height: 16px;\"\u003eCreated with ❤︎ to \u003cb\u003eGraphQL\u003c/b\u003e community by \u003ca href=\"https://twitter.com/UsulPro\"\u003eOleg Proskurin\u003c/a\u003e at \u003ca href=\"https://twitter.com/FocusReactive\"\u003eFocusReactive\u003c/a\u003e\n\u003c/div\u003e\n\n2019\n\n[![FocusReactive](https://raw.githubusercontent.com/focusreactive/storybook-graphql-kit/master/docs/focusreactive-logo.svg?sanitize=true)](https://focusreactive.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffocusreactive%2Fstorybook-graphql-kit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffocusreactive%2Fstorybook-graphql-kit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffocusreactive%2Fstorybook-graphql-kit/lists"}