将文件“.gql”与 webpack 一起使用时会出现什么样的 graphql 错误?

问题描述

我尝试将 typeDefs 分离到文件中 在users.typeDefs.ts

type User {
    id: ID!
    email: string
}

extend type Query {
    getAllUsers(): User
}

用户解析器:

const resolvers = () => ({
  Query: {
    getAllUsers() {
      return [{ id: "1",email: "email" }];
    },},});

export default resolvers;

makeExecutableSchema 的 index.js

import { makeExecutableSchema } from "graphql-tools";
import userTypeDefs from "./users/users.typeDefs.gql";
import userResolvers from "./users/users.resolver";

const typeDefs = [userTypeDefs];

const resolvers = [userResolvers];

export default makeExecutableSchema({
  typeDefs,resolvers,});

这是我的服务器代码

import * as Hapi from "@hapi/hapi";
import { Server,Request,Responsetoolkit } from "hapi";
import { ApolloServer } from "apollo-server-hapi";
import schema from "@graphql";
import { config } from "@configs/enviroment";

async function StartServer() {
  const server = new ApolloServer({
    context: (request: Request,h: Responsetoolkit) => ({ request,h }),introspection: process.env.NODE_ENV === "development",schema,});
  await server.start();

  const app: Server = new Hapi.server({
    port: config.get("port" as any),host: config.get("host" as any),});

  await server.applyMiddleware({
    app,path: "/graphql",});

  await app.start();
}

StartServer()
  .then(() => {
    console.log(
      "Server start at" +
        ` ${config.get("host" as any)}:${config.get("port" as any)}`
    );
  })
  .catch((error) => console.log(error));

export default StartServer;

webpack 配置在这里

const path = require("path");
const nodeExternals = require("webpack-node-externals");
const tsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
const mode =
  process.env.NODE_ENV === "production" || process.env.NODE_ENV === "prod"
    ? "production"
    : "development";
const isProd = mode === "production";

module.exports = {
  entry: "./src/App.ts",devtool: isProd ? "source-map" : "eval-source-map",mode: mode,target: "node",externals: [nodeExternals()],resolve: {
    extensions: [".js",".json",".ts",".tsx",".gql"],plugins: [new tsconfigPathsPlugin()],output: {
    libraryTarget: "commonjs",path: path.join(__dirname,"dist"),filename: "[name].bundle.js",module: {
    rules: [
      {
        test: /\.ts(x?)$/,include: [path.resolve(__dirname,"src")],use: [
          {
            loader: "ts-loader",],{
        test: /\.(graphql|gql)$/,exclude: /node_modules/,loader: "graphql-tag/loader",};

当我开始项目时遇到此错误,我尝试搜索但未找到解决方案。任何人都可以帮忙吗?

ERROR in ./src/graphql/users/users.typeDefs.gql
Module build Failed (from ./node_modules/graphql-tag/loader.js):
GraphQLError: Syntax Error: Expected Name,found ")".

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)