question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Storybook does not resolve .graphql file imports

See original GitHub issue

Describe the bug Storybook cannot import .graphql files. I’m trying to use graphql-tag/loader, which I’m using in a react create project that uses typescript. It works fine in the app itself but when running storybook it does not resolve correctly.

To Reproduce Steps to reproduce the behavior:

  1. Create a storybook project
  2. Add graphql/tag to webpack config (info below)
  3. Include an import of a graphql file within your project/stories
  4. Start storybook
  5. Observe it does not import correctly

Expected behavior The import should resolve to a graphql object.

Actual behaviour The import resolves to the filename e.g. “static/media/create.d41dd6c7.graphql”

System:

System:
    OS: macOS 10.15.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 12.13.1 - /usr/local/bin/node
    Yarn: 1.19.2 - /usr/local/bin/yarn
    npm: 6.12.1 - /usr/local/bin/npm
  Browsers:
    Chrome: 79.0.3945.117
    Safari: 13.0.3
  npmPackages:
    @storybook/addon-actions: 5.3.7 => 5.3.7 
    @storybook/addon-docs: 5.3.7 => 5.3.7 
    @storybook/addon-links: 5.3.7 => 5.3.7 
    @storybook/addon-storyshots: 5.3.7 => 5.3.7 
    @storybook/addon-viewport: ^5.3.1 => 5.3.7 
    @storybook/addons: 5.3.7 => 5.3.7 
    @storybook/preset-create-react-app: ^1.5.0 => 1.5.2 
    @storybook/react: 5.3.7 => 5.3.7 

Additional context The following is added to the webpack.config.js rules of storybook:

{
    test: /\.(graphql|gql)$/,
    exclude: /node_modules/,
    loader: "graphql-tag/loader",
}

the following added to the extensions:

config.resolve.extensions.push(".graphql")

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

4reactions
afrankel-sfdocommented, Sep 30, 2020

@ian-callaghan ran into the same problem. solved by adding to main.js:

  addons: [
    {
      name: '@storybook/preset-create-react-app',
      options: {
        craOverrides: {
          fileLoaderExcludes: ['graphql'], // THIS
        },
      },
    },
  ],
  webpackFinal: async (config, { configType }) => {

    // and THIS
    config.module.rules.push({ 
      test: /\.graphql$/,
      exclude: /node_modules/,
      loader: 'graphql-tag/loader',
    });

    return config;
  },

0reactions
cybervaldezcommented, Oct 27, 2020

This works for me. From the Apollo graphQL docs:

create-react-app can’t use the Webpack loaders unless ejected. To make the same transformation work in create-react-app without ejecting, use graphql.macro.

import { loader } from 'graphql.macro';
const currentUserQuery = loader('./currentUser.graphql');
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to import .graphql files with webpack - Stack Overflow
Easier to do: Instead of creating a graphql file you can just create a js file to save your queries and create them...
Read more >
Modularizing your GraphQL schema code
js'; import { typeDef as Book, resolvers as bookResolvers, } from './book.js'; // If you had Query fields not associated with a //...
Read more >
GraphQL Code Libraries, Tools and Services
A powerful JavaScript GraphQL client, designed to work well with React, ... No more render props hell; ⏳ Handle loading and error states...
Read more >
typescript-resolvers - GraphQL Code Generator
Without loading your GraphQL operations ( query , mutation , subscription and fragment ), you won't see any change in the generated output....
Read more >
GraphQL Modules tutorial: How to modularize GraphQL schema
GraphQL Modules is a set of libraries and guidelines designed to help you ... book.type.graphql import { gql } from 'graphql-modules'; ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found