webpack - 如何让 Vue (vue cli 3) 正确处理 GraphQL 文件?

标签 webpack vue.js graphql vue-cli

我有一个新的基于 vue-cli 3 的项目,它在 src/ 文件夹中有 .graphql 文件,例如:

#import "./track-list-fragment.graphql"

query ListTracks(
  $sortBy: String
  $order: String
  $limit: Int
  $nextToken: String
) {
  listTracks(
    sortBy: $sortBy
    order: $order
    limit: $limit
    nextToken: $nextToken
  ) {
    items {
      ...TrackListDetails
    }
    nextToken
  }
}

当我运行 yarn serve 时,它提示没有 GraphQL 加载器:

Module parse failed: Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
> #import "./track-list-fragment.graphql"
|
| query ListTracks(

但我确实正确设置了我的 vue.config.js(我认为):

const webpack = require('webpack');
const path = require('path');

module.exports = {
  configureWebpack: {
    resolve: {
      alias: {
        $scss: path.resolve('src/assets/styles'),
      },
    },
    plugins: [
      new webpack.LoaderOptionsPlugin({
        test: /\.graphql$/,
        loader: 'graphql-tag/loader',
      }),
    ],
  },
};

我该如何解决这个问题?

最佳答案

这行得通!

const path = require('path');

module.exports = {
  pluginOptions: {
    i18n: {
      locale: 'en',
      fallbackLocale: 'en',
      localeDir: 'locales',
      enableInSFC: false,
    },
  },
  configureWebpack: {
    resolve: {
      alias: {
        $element: path.resolve(
          'node_modules/element-ui/packages/theme-chalk/src/main.scss'
        ),
      },
    },
  },
  chainWebpack: config => {
    config.module
      .rule('graphql')
      .test(/\.graphql$/)
      .use('graphql-tag/loader')
      .loader('graphql-tag/loader')
      .end();
  },
};

关于webpack - 如何让 Vue (vue cli 3) 正确处理 GraphQL 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51469006/

相关文章:

graphql - 子解析器中的Graphql-Access参数

javascript - Vue.js 在单击列表时更改附加到表单的模型

webpack - 无法在 React 应用程序中使用 GLTFLoader 从本地目录加载 GLTF 文件。它从外部 URL 正确加载

webpack css-loader 图像路径需要保持相对于样式表位置

reactjs - 故事书-顺风。我应该如何将顺风添加到故事书中

javascript - 传递到组件中的 Prop 不呈现

vue.js - 使用循环Vuejs更改div的背景颜色

graphql - 在 GraphQL 中,您可以更改别名中的输出结构吗?

GraphQL:嵌套查询与根查询

css - foundation-sites 包含在 webpack 中。不能使用样式