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.

React app won't compile after adding Apollo with GraphQL

See original GitHub issue

So it’s my first experience with GraphQL and Apollo. I’ve created a small NodeJS backend serving data with the Apollo server. This works fine, but implementing the API within my React frontend application runs my into unsolvable issues. I created a repository so you can checkout the issue yourself: https://github.com/rnnyrk/graphql-parser Within this project I try to fetch data from localhost:4000, since that’s where my Apollo server is running. Although I experience the same issues using the example url: https://48p1r2roz4.sse.codesandbox.io

Screenshot 2021-05-09 at 12 07 31

I’ve started following the “Getting Started” docs, but the above errors keep coming up. Googeling the warnings, issues like these keep coming up https://github.com/graphql/graphql-js/issues/1272 suggesting adding .mjs section to your Webpack rules, or extension resolvers. I tried everything recommended, byt unfortunately nothing seems to works. This is my Webpack setup, the complete file can be found here:

import path from 'path';
import * as webpack from 'webpack';
import * as devServer from 'webpack-dev-server';
import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin';

const baseConfig: webpack.Configuration = {
  mode: 'production',
  target: 'browserslist',
  output: {
    filename: 'static/js/[name].[chunkhash].js',
    chunkFilename: 'static/js/[name].[chunkhash].js',
    path: path.resolve('dist'),
    publicPath: '/',
  },
  entry: path.resolve('src'),
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        use: 'babel-loader',
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
      ....
      {
        test: /\.m?js/,
        resolve: {
          fullySpecified: false,
        },
      },
    ],
  },
  ....
  resolve: {
    extensions: ['*', '.mjs', '.js', '.jsx', '.ts', '.tsx'],
    plugins: [
      new TsconfigPathsPlugin(),
    ],
  },
};

export default baseConfig;

export type WebpackConfig = webpack.Configuration & { devServer?: devServer.Configuration };
type WebpackMergeType = (...config: WebpackConfig[]) => WebpackConfig;

export const merge: WebpackMergeType = (...config) => webpackMerge(baseConfig, ...config);

The issues occurs directly after starting my front-end application. The GraphQL/Webpack versions and Apollo setup:

"@apollo/client": "^3.3.16",
"graphql": "^15.5.0",
"webpack": "5.36.2",

  System:
    OS: macOS 11.3
  Binaries:
    Node: 14.16.0 - ~/.nvm/versions/node/v14.16.0/bin/node
    npm: 7.6.3 - ~/.nvm/versions/node/v14.16.0/bin/npm
  Browsers:
    Chrome: 90.0.4430.93
    Firefox: 87.0
    Safari: 14.1
  npmPackages:
    @apollo/client: ^3.3.16 => 3.3.16

Hopefully I supplied enough information. Any idea or recommendations? Kinda stuck now on first implementing it 😓

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
rnnyrkcommented, May 25, 2021

@hwillson Wonderful! This works like charm, thank you! 😄

2reactions
hwillsoncommented, May 25, 2021

@rnnyrk update your webpack config to exclude .mjs files from the file-loader:

...
      {
        exclude: [
          /\.[tj]sx?$/,
          /\.css$/,
          /\.svg$/,
          /\.(jpe?g|png|gif)$/i,
          /\.json$/,
          /\.html$/,
          /\.ejs$/,
          /\.mjs$/,     // ADD THIS
        ],
        use: [{
          loader: 'file-loader',
          options: { name: 'static/[name].[ext]' },
        }],
      },
...
Read more comments on GitHub >

github_iconTop Results From Across the Web

Get started with Apollo Client - Apollo GraphQL Docs
Step 1: Setup. To start this tutorial, do one of the following: Create a new React project locally with Create React App, or;...
Read more >
Building React Apps with GraphQL, Graphcool & Apollo
In this article, you'll learn how to quickly get started with React & GraphQL by using Apollo Client and Graphcool to building a...
Read more >
Migrating to Apollo Client 3.0 - Apollo GraphQL Docs
This article walks you through migrating your application to Apollo Client 3.0 ... The @apollo/client package includes both React hooks and GraphQL request ......
Read more >
React + GraphQL Tutorial — The Server
With the server up and running, we're ready to dive into the GraphQL part of this tutorial! 2. Writing your GraphQL schema. When...
Read more >
Loading queries with Webpack - 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.
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