首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Graphql加载器:如何将Nextjs与Graphql加载器集成

Graphql加载器:如何将Nextjs与Graphql加载器集成
EN

Stack Overflow用户
提问于 2018-09-18 00:33:47
回答 1查看 2.6K关注 0票数 5

我正在尝试将Nextjs与graphql-tag/loader集成,这是我的next.config.js文件:

代码语言:javascript
复制
const withSass = require('@zeit/next-sass')
const graphqlLoader = require('graphql-tag/loader')

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

    return config
  }
})

我无法构建,我得到以下错误:

代码语言:javascript
复制
/HOME/node_modules/graphql-tag/loader.js:43
  this.cacheable();
       ^
TypeError: Cannot read property 'cacheable' of undefined

请帮帮忙。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-18 03:20:06

我让它在我的设置中工作,如下所示。我不确定你的代码出了什么问题,但你可以试一试,看看它是否正常工作:)你可以使用next js插件。也许插件的顺序很重要。这是我的配置。还有一些额外的代码,但我相信你会从中得到你需要的东西。至于库版本"next":"6.1.1","next-optimized-images":"1.4.1","next-plugin-graphql":"^0.0.1",

代码语言:javascript
复制
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(
  withGraphQL(
    withSass({
      cssModules: true,
      cssLoaderOptions: {
        importLoaders: 1,
        localIdentName: "[local]___[hash:base64:5]"
      },
      webpack: config => {

        config.plugins.push(
          new webpack.ContextReplacementPlugin(
            /graphql-language-service-interface[\\/]dist$/,
            new RegExp(`^\\./.*\\.js$`)
          )
        );

        return config;
      }
    })
  )
);

如果你只想修改你的代码,不想安装插件,你可以从这个next-graphql-plugin得到启发。这个插件对我来说是有效的,与你的设置的不同之处在于他们的规则配置如下

代码语言:javascript
复制
  config.module.rules.push({
       test: /\.(graphql|gql)$/,
       include: [dir],
       exclude: /node_modules/,
       use: [
           {
             loader: 'graphql-tag/loader'
           }
       ]
    })
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52372233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档