响应不成功:使用“数据源”时收到状态代码 500

Response not successful: Received status code 500 when using `datasources`

这是我的服务器设置:

const server = new ApolloServer({
  typeDefs,
  resolvers,
  dataSources: () => ({
    movieAPI: new MovieAPI()
  }),
  context: {
    hello: `world`
  }
});

server
  .listen({ port: 4000 })
  .then(({ url }) => console.log(` app running at ${url}`));

如果我删除 datasources 一切正常,否则我会在 graphql playground "error": "Response not successful: Received status code 500"

中出错

我正在这样做 Apollo Server tutorial 但我有一些 babel 配置所以我可以使用 es6 语法。

这是我的 repo

添加 introspection: true 解决了问题

在 playground 的设置中将 request.credentials: "omit" 更改为 "request.credentials": "include"

在 playground GUI 中有一个按钮:COPY CURL 如果您在终端中尝试,您可以获得有关错误的更多详细信息。 我有同样的问题,我从 curl 得到了这个回复:

{
  "errors": [
    {
      "message": "Class constructor DataSource cannot be invoked without 'new'",
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "TypeError: Class constructor DataSource cannot be invoked without 'new'",
            "    at new UserAPI (/var/www/html/blog-project/server/src/datasources/user.ts:12:5)",
            "    at Object.dataSources (/var/www/html/blog-project/server/src/index.ts:30:14)",
            "    at /var/www/html/blog-project/server/node_modules/apollo-server-core/src/requestPipeline.ts:618:34",
            "    at Generator.next (<anonymous>)",
            "    at /var/www/html/blog-project/server/node_modules/apollo-server-core/dist/requestPipeline.js:8:71",
            "    at new Promise (<anonymous>)",
            "    at __awaiter (/var/www/html/blog-project/server/node_modules/apollo-server-core/dist/requestPipeline.js:4:12)",
            "    at initializeDataSources (/var/www/html/blog-project/server/node_modules/apollo-server-core/dist/requestPipeline.js:323:20)",
            "    at Object.<anonymous> (/var/www/html/blog-project/server/node_modules/apollo-server-core/src/requestPipeline.ts:117:9)",
            "    at Generator.next (<anonymous>)",
            "    at /var/www/html/blog-project/server/node_modules/apollo-server-core/dist/requestPipeline.js:8:71",
            "    at new Promise (<anonymous>)",
            "    at __awaiter (/var/www/html/blog-project/server/node_modules/apollo-server-core/dist/requestPipeline.js:4:12)",
            "    at Object.processGraphQLRequest (/var/www/html/blog-project/server/node_modules/apollo-server-core/dist/requestPipeline.js:41:12)",
            "    at /var/www/html/blog-project/server/node_modules/apollo-server-core/src/runHttpQuery.ts:310:32",
            "    at Generator.next (<anonymous>)"
          ]
        }
      }
    }
  ]
}

搜索 Class constructor DataSource cannot be invoked without 'new' 后,我找到了

This problem should be solved in Node.js by setting TypeScript target option to es6. Modern Node.js versions support ES6 classes, there is no need to transpile them.

所以解决方案:在tsconfig.json设置targetes6

对于那些在 AWS 上使用 TypeScript 和无服务器框架的人:

确保您的 tsconfig.jsoncompilerOptions.target 设置为 "es6" 并将 compilerOptions.module 设置为 "CommonJS"

看起来这可能是过度转译代码的问题 (ref)。所以我们需要设置 "target": "es6" 以包含 class 关键字。这会在使用 es6 模块时出现问题,因此需要 tsconfig.json

中的 "module": "CommonJS"

完整 tsconfig.json

{
  "compilerOptions": {
    "preserveConstEnums": true,
    "strictNullChecks": true,
    "sourceMap": true,
    "allowJs": true,
    "target": "es6",
    "outDir": ".build",
    "moduleResolution": "node",
    "lib": ["es2015"],
    "rootDir": "./",
    "module": "CommonJS"
  }
}

如果您可以提供带有问题的错误堆栈跟踪,那就太好了!

可能有很多原因 你从 apollo 服务器得到 500 响应的原因,但我只想谈谈一般原因。对于仍然面临这个问题的其他人。大多数情况下,这应该是您创建和连接数据源 到 apollo 服务器的方式的问题。请查看 错误的堆栈跟踪 并解决问题。