0

我尝试将 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;

index.js 用于makeExecutableSchema

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",
  },
  target: "node",
  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 ")".
4

0 回答 0