【问题标题】:ApolloServer schema optionApolloServer 模式选项
【发布时间】:2021-10-22 13:06:17
【问题描述】:

我正在观看关于 react、graphQL、typeScript 和 apollo 服务器部分的 youtube 全栈教程,我尝试在 schema 选项中传递 buildSchema(function from type-graphql) 并收到此错误消息

我的代码 - 我正在使用打字稿

import { MikroORM } from "@mikro-orm/core";
import { __port__, __prod__ } from "./constants";
import { Post } from "./entity/Post";
import express from "express";
import { ApolloServer } from "apollo-server-express";
import { buildSchema, Query, Resolver } from "type-graphql";

@Resolver()
class HelloResolver {
  @Query(() => String)
  hello() {
    return "hi";
  }
}

const main = async () => {
  const orm = await MikroORM.init();
  await orm
    .getMigrator()
    .up()
    .catch((err) => {});
  const app = express();

  const apolloServer = new ApolloServer({ // line 24
    schema: await buildSchema({
      resolvers: [HelloResolver],
    }),
  });

  apolloServer.applyMiddleware({ app });

  app.listen(__port__, () => {
    console.log(`server started on localhost:${__port__}/graphql`);
  });
};

main().catch((err) => {
  console.log(err);
});

我的错误信息

PATH/node_modules/ts-node/src/index.ts:307
        throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset))
              ^
TSError: ⨯ Unable to compile TypeScript
src/index.ts (24,41): Argument of type '{ schema: GraphQLSchema; }' is not assignable to parameter of type 'Config<ExpressContext>'.
  Type '{ schema: GraphQLSchema; }' is missing the following properties from type 'Config<ExpressContext>': formatError, debug, rootValue, validationRules, and 6 more. (2345)
    at getOutput (PATH/node_modules/ts-node/src/index.ts:307:15)
    at PATH/node_modules/ts-node/src/index.ts:336:16
    at Object.compile (PATH/node_modules/ts-node/src/index.ts:498:11)
    at Module.m._compile (PATH/node_modules/ts-node/src/index.ts:392:43)
    at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Object.require.extensions.<computed> [as .ts] (PATH/node_modules/ts-node/src/index.ts:395:12)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at Object.<anonymous> (PATH/node_modules/ts-node/src/_bin.ts:182:12)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

【问题讨论】:

    标签: typescript express apollo apollo-server typegraphql


    【解决方案1】:

    我遇到了同样的问题,对我来说,这是因为有一个嵌套的 repo 结构。 ts-node 和 typescript 依赖项安装在与我运行 yarn start 不同的位置。

    我可以通过卸载 ts-node 和 typescript npm uninstall ts-node typescript,进入我的项目根目录(我有我的 start 脚本设置)并重新安装 ts-node 和 ts npm install -D ts-node typescript 来解决这个问题。 /p>

    【讨论】:

    • 这行得通。谢谢!
    【解决方案2】:

    解决方法是在 ApolloServer 对象参数后添加as Config&lt;ExpressContext&gt;

    const apolloServer = new ApolloServer({
        schema: ...
      } as Config<ExpressContext>);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多