Nextjs-Graphql webpack loader: How to integrate Nextjs with Graphql loader


I am trying to integrate Nextjs with graphql-tag/loader, This is my next.config.js file:

const withSass = require('@zeit/next-sass')
const graphqlLoader = require('graphql-tag/loader')

module.exports = withSass({
  webpack: (config, { buildId, dev, isServer, defaultLoaders }) => {
      test: /.(graphql|gql)$/,
      loader: graphqlLoader,
      exclude: /node_modules/

    return config

I am unable to build, I get the error below:

TypeError: Cannot read property 'cacheable' of undefined

Please help.


i made it working in my setup as follows. Not sure what is wrong in your code, but you can try it and see if it is working 🙂 You can use next js plugin for it. Maybe the order of plugins matter. Here is my config. There are some additional code, but i am sure, that you will get it what you need from it. As for the libraries version “next”: “6.1.1”, “next-optimized-images”: “1.4.1”, “next-plugin-graphql”: “^0.0.1”,

const withSass = require("@zeit/next-sass");
const webpack = require("webpack");
const withGraphQL = require("next-plugin-graphql");
const withOptimizedImages = require("next-optimized-images");

module.exports = withOptimizedImages(
      cssModules: true,
      cssLoaderOptions: {
        importLoaders: 1,
        localIdentName: "[local]___[hash:base64:5]"
      webpack: config => {

          new webpack.ContextReplacementPlugin(
            new RegExp(`^\./.*\.js$`)

        return config;

If you would prefer just to modify your code and do not install plugins you can inspire yourself from this next-graphql-plugin. The plugin is working for me, the difference from your setup is that they have rule configured as follows

       test: /.(graphql|gql)$/,
       include: [dir],
       exclude: /node_modules/,
       use: [
             loader: 'graphql-tag/loader'
Leave a Reply

Your email address will not be published. Required fields are marked *