-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Storybook] useStaticQuery fetching fails when used inside stories #26099
Comments
@r3sMetz just skimming through your repository, the issue could lie with how you're handling the queries themselves. For both Storybook and Gatsby to play nice in terms of data, you'll need to mock out the data you need to provide to the components/pages, as technically all the graphql data is being stripped down from execution in the Storybook webpack configuration. Take a look at this to see how you can use the queries with Storybook and Gatsby. Feel free to provide feedback |
Hey, thanks for reviewing my repo! Your repo seems like a cool way too. My approach worked before, it's just broken atm. I need to query data in the gatsby way because i want to use and test things like gatsby-image. |
@r3sMetz no problem and storybook in this case will rely on a build to be made beforehand. as it's expecting a output folder to exist, as it's the case with for instance CRA as when you create a new project based on it a Regarding your question about the images. I'm not sure, but you might want to do the following:
See if what i mentioned helps. Or if your're ok waiting a bit longer. I can update the repo towards the end of the week and report back to you. Sounds good? |
Exactly, in my package.json the storybook script looks something like this Sounds pretty good, but don't hurry :) I filed this issue based on a chat inside another useStaticQuery Issue, i got told that the issue with useStaticQuery inside stories will soon be working again. |
As a sidenote to the actual issue, @muescha please do not edit other people's comments. If you have new context to add, please add it in a new comment rather than modifying another person's words, out of respect to them. Thank you! |
@marcysutton ok. I removed my change (add link to issue comment)
The comment is here: #26077 (comment) |
Any news on this or some temporary solution? I am using the same method with query results pre-build and all stories for components with |
I use explicit versions for now and a resolutions object inside my package.json. {
"name": "gatsby-starter-default",
"private": true,
"description": "A simple starter to get up and developing quickly with Gatsby",
"version": "0.1.0",
"author": "Kyle Mathews <[email protected]>",
"dependencies": {
"@types/faker": "4.1.12",
"babel-plugin-module-resolver": "4.0.0",
"dotenv": "^8.2.0",
"faker": "4.1.0",
"gatsby": "2.24.3",
"gatsby-background-image": "1.1.1",
"gatsby-image": "2.4.13",
"gatsby-plugin-manifest": "2.4.18",
"gatsby-plugin-offline": "3.2.18",
"gatsby-plugin-postcss": "2.3.11",
"gatsby-plugin-react-helmet": "3.3.10",
"gatsby-plugin-sharp": "2.6.19",
"gatsby-plugin-typescript": "2.4.15",
"gatsby-source-filesystem": "2.3.19",
"gatsby-transformer-sharp": "2.5.11",
"npm-run-all": "^4.1.5",
"prop-types": "15.7.2",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-helmet": "6.1.0",
"tailwindcss": "1.5.1"
},
"devDependencies": {
"@babel/core": "7.10.5",
"@storybook/addon-a11y": "5.3.19",
"@storybook/addon-actions": "5.3.19",
"@storybook/addon-backgrounds": "5.3.19",
"@storybook/addon-links": "5.3.19",
"@storybook/addon-viewport": "5.3.19",
"@storybook/addons": "5.3.19",
"@storybook/react": "5.3.19",
"autoprefixer": "9.8.5",
"babel-loader": "8.1.0",
"babel-preset-gatsby": "0.5.2",
"babel-preset-react-app": "9.1.2",
"eslint-import-resolver-babel-module": "5.1.2",
"eslint-plugin-import": "2.22.0",
"postcss-cli": "7.1.1",
"postcss-import": "12.0.1",
"prettier": "1.19.1"
},
"keywords": [
"gatsby"
],
"license": "MIT",
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write \"**/*.{js,jsx,json,md}\"",
"start": "npm run develop",
"serve": "gatsby serve",
"clean": "gatsby clean",
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1",
"storybook": "NO_PURGE=true run-s clean build start-storybook",
"start-storybook": "NODE_ENV=production start-storybook -p 6006 -s ./public",
"build-storybook": "NODE_ENV=production build-storybook -s ./public"
},
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby-starter-default"
},
"bugs": {
"url": "https://github.com/gatsbyjs/gatsby/issues"
},
"resolutions": {
"babel-plugin-remove-graphql-queries": "2.9.9"
}
} |
Thanks, @smetzdev! This is really working as temp fix. Solution in short for other Storybook users: Static query results loading in Gatsby runtime was added in "dependencies": {
"gatsby": "2.24.6"
},
"resolutions": {
"babel-plugin-remove-graphql-queries": "2.9.14"
} Important note: But packages install through |
Thats cool, i even learned a bit from your answer (tbh, my solution came from some trail and error 😅). May i ask a little question out of interest: If you build it before you have to restart your storybook everytime you have to update a query inside storybook, but running |
Yeah, if i change query, then i need to run |
@blainekasten @sidharthachatterjee I have the same problem with the latest version of Gatsby. 1. Considering this line:
Recent versions of 2. This pull request edits - [`production`, `test`].includes(process.env.NODE_ENV) &&
+ (process.env.NODE_ENV === `test` ||state.opts.stage === `build-html`) && Now as most Storybook + Gatsby workspaces are advised by Gatsby docs to run Storybook with Here is what I did to confirm that these two changes are the cause of the problem:
Viola. The components containing the queries are loaded with the needed queries. |
Cool, works for me atm. "scripts": {
"copy-static-queries": "cp -r ./public/page-data/sq/d ./public/static",
"prestorybook": "npm run copy-static-queries",
} Edit: |
@smetzdev It doesn't work for me with It might be that your |
You're right, i still had it left over in my dependencies without me noticing it. I still might want to keep it that way, it feels more right to me running Do you see any problems there? |
@smetzdev I don't think I don't think Update: As |
FWIW, @mohsenkhanpour and @smetzdev 's fixes of |
* fix: add storybook build files to gitignore * fix: update storybook scripts to handle staticQueries REF: gatsbyjs/gatsby#26099 (comment) * chore: update components that use StaticQueries - remove unneeded components whose job was to perform staticQuery and just do staticQuery in component * Revert "chore: update components that use StaticQueries" This reverts commit ebb44ac. * chore: update Header * chore: update Side Nav Content
#28922 - there is an undocumented option on the babel plugin for overwriting the static query directory rather than copying. Detailed in this PR I have opened. |
My workaround for gatsby 2.31.0 and storybook 6.1.15. module.exports = {
webpackFinal(config) {
config.module.rules[0].use[0].options.plugins = [
...,
[require.resolve("babel-plugin-remove-graphql-queries"), {
stage: config.mode === `development` ? "develop-html" : "build-html",
staticQueryDir: "page-data/sq/d",
}]
]
} |
@DevSide that didn't quite work for me. Are you doing anything special in your |
Thanks @DevSide, that's working for me. For everyone else, here's a full example of what's working for me on the latest version of Gatsby as of this comment ( First, make a Gatsby addon to keep your (sidenote: this really should just be an addon available on NPM - #28304) // .storybook/addon-gatsby.js
module.exports = {
webpack: async (config, options) => {
const babelPlugins = [
// use @babel/plugin-proposal-class-properties for class arrow functions
require.resolve('@babel/plugin-proposal-class-properties'),
// use @babel/plugin-proposal-optional-chaining since Webpack 4 doesn't understand optional chaining
require.resolve('@babel/plugin-proposal-optional-chaining'),
// use babel-plugin-remove-graphql-queries to remove static queries from components when rendering in storybook
[
require.resolve('babel-plugin-remove-graphql-queries'),
{
stage: config.mode === `development` ? 'develop-html' : 'build-html',
staticQueryDir: 'page-data/sq/d',
},
],
]
// Transpile Gatsby module because Gatsby includes un-transpiled ES6 code.
config.module.rules[0].exclude = [/node_modules\/(?!(gatsby)\/)/]
// use installed babel-loader which is v8.0-beta (which is meant to work with @babel/core@7)
config.module.rules[0].use[0].loader = require.resolve('babel-loader')
// use @babel/preset-react for JSX and env (instead of staged presets)
config.module.rules[0].use[0].options.presets = [
require.resolve('@babel/preset-react'),
require.resolve('@babel/preset-env'),
]
config.module.rules[0].use[0].options.plugins = babelPlugins
// Prefer Gatsby ES6 entrypoint (module) over commonjs (main) entrypoint
config.resolve.mainFields = ['browser', 'module', 'main']
// Add TypeScript support
config.module.rules.push({
test: /\.(ts|tsx)$/,
loader: require.resolve('babel-loader'),
options: {
presets: [['react-app', { flow: false, typescript: true }]],
plugins: babelPlugins,
},
})
config.resolve.extensions.push('.ts', '.tsx')
return config
},
} If you aren't using TypeScript, feel free to remove that portion. Then, in your const path = require('path')
module.exports = {
// ...your other settings, if any
addons: [
// ...your other addons, if any
path.resolve('./.storybook/addon-gatsby.js'),
],
} Lastly, install |
@angeloashmore This part helped me. Thanks a lot for this and for your amazing work on gatsby-plugin-local-search. // use babel-plugin-remove-graphql-queries to remove static queries from components when rendering in storybook
[
require.resolve('babel-plugin-remove-graphql-queries'),
{
stage: config.mode === `development` ? 'develop-html' : 'build-html',
staticQueryDir: 'page-data/sq/d',
},
], |
thank you very much for the answers above. i was able to run and see images via gql using this topic and recipe start-storybook mode
build-storybook /serve mode
in both cases babel plugin goes onto build mode
Destination folder storybook-static static folder has folders with images and page-date/sq/d/ has gql cache json files as expected my package.json https://pastebin.com/TRj35qmN |
I've also not been able to get lazy images to work. With the right Storybook webpack config, Any clues as to how to get the images transformed when an image is used from a static query in Storybook? |
Solution with Gatsby 3.3.0 |
I have the same @p1t1ch problem. I'm able to make it work with |
I'll close this issue as it's not about Gatsby but the correct configuration for Storybook. We've since then updated our Storybook guide (thanks to various contributors): https://www.gatsbyjs.com/docs/how-to/testing/visual-testing-with-storybook/ It also includes a section on a new Storybook Addon for Gatsby: https://storybook.js.org/addons/storybook-addon-gatsby If you have further questions for configuring Storybook with Gatsby please go to our GitHub Discussions (https://github.com/gatsbyjs/gatsby/discussions/categories/help) or to the addon's page. Thanks! |
Description
Fetching data via
useStaticQuery
within a story causes Storybook to break.Steps to reproduce
I setup a reproduction repo, based on gatsby-default starter, following the description how to use Storybook with Gatsby.
Reproduction Repo
Expected result
The Story in the repo is supposed to show the queried title "Gatsby Starter Default".
Actual result
Error message:
Environment
System:
OS: macOS 10.15.5
CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 13.12.0 - /usr/local/bin/node
Yarn: 1.22.4 - /usr/local/bin/yarn
npm: 6.14.4 - /usr/local/bin/npm
Languages:
Python: 2.7.16 - /usr/bin/python
Browsers:
Chrome: 84.0.4147.89
Firefox: 78.0.2
Safari: 13.1.1
npmPackages:
gatsby: ^2.23.12 => 2.24.13
gatsby-image: ^2.4.9 => 2.4.14
gatsby-plugin-manifest: ^2.4.14 => 2.4.21
gatsby-plugin-offline: ^3.2.13 => 3.2.21
gatsby-plugin-react-helmet: ^3.3.6 => 3.3.10
gatsby-plugin-sharp: ^2.6.14 => 2.6.23
gatsby-source-filesystem: ^2.3.14 => 2.3.23
gatsby-transformer-sharp: ^2.5.7 => 2.5.11
The text was updated successfully, but these errors were encountered: