【问题标题】:Response not successful: Received status code 500 when using `datasources`响应不成功:使用 `datasources` 时收到状态码 500
【发布时间】:2019-04-27 03:20:45
【问题描述】:

这是我的服务器设置:

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

【问题讨论】:

标签: apollo apollo-server


【解决方案1】:

添加introspection: true 解决了这个问题

【讨论】:

  • 您能否说得更具体些。在哪里添加内省?
  • 虽然该解决方案对我不起作用,但 introspection: true 是 ApolloServer 配置中的一个选项。 See Here
【解决方案2】:

在操场设置中将request.credentials: "omit"更改为"request.credentials": "include"。

【讨论】:

    【解决方案3】:

    在 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 solution

    这个问题应该在 Node.js 中通过将 TypeScript 目标选项设置为 es6 来解决。现代 Node.js 版本支持 ES6 类,无需转译。

    所以解决方案:在tsconfig.json 中将 target 设置为 es6

    【讨论】:

      【解决方案4】:

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

      确保您的tsconfig.json 将compilerOptions.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"
        }
      }
      

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-12-20
          • 2021-04-02
          • 2019-02-16
          • 2020-07-09
          • 2020-12-27
          • 1970-01-01
          • 2019-07-28
          • 2016-04-22
          相关资源
          最近更新 更多