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

How to get Vue (vue cli 3) to properly handle GraphQL files?

我有一个新的基于 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();
  },
};

我很确定 LoaderOptionsPlugin 不是您想要的。 webpack 文档提到这用于从 webpack 1 迁移到 webpack 2。这不是我们在这里做的。

这是在 "normal" webpack config 中配置加载器的样子:

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          { loader: 'style-loader' },
          {
            loader: 'css-loader',
            options: {
              modules: true
            }
          }
        ]
      }
    ]
  }
};

按照这种方法并假设我正确理解 Vue 3 docs,下面是我如何使用原始示例的数据配置 Vue 3 应用程序:

module.exports = {
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.css$/,
          use: [
            { loader: 'style-loader' },
            {
              loader: 'css-loader',
              options: {
                modules: true
              }
            }
          ]
        }
      ]
    }
  }
}

现在,我们需要配置 graphql 加载器而不是 css 加载器:

module.exports = {
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.graphql$/,
          use: 'graphql-tag/loader'
        }
      ]
    }
  }
}

这是未经测试的,我只是脱离了我对 webpack 和 Vue 文档的理解。我没有一个项目来测试这个,但如果你 post 一个 link 到你的项目,我会非常乐意测试。