响应失败:使用`datasources'时收到状态码500

时间:2018-11-25 05:18:52

标签: apollo apollo-server

这是我的服务器设置:

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

5 个答案:

答案 0 :(得分:2)

游乐场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

应通过将TypeScript target选项设置为es6在Node.js中解决此问题。现代的Node.js版本支持ES6类,无需进行编译。

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

答案 1 :(得分:1)

添加introspection: true解决了该问题

答案 2 :(得分:0)

在游乐场更改中将request.credentials: "omit"更改为"request.credentials": "include"

答案 3 :(得分:0)

对于将TypeScript和无服务器框架与AWS结合使用的用户:

请确保您的tsconfig.json的{​​{1}}设置为compilerOptions.target,并且"es6"设置为compilerOptions.module

看起来这可能是过度转码(ref)的问题。因此,我们需要将"CommonJS"设置为包含"target": "es6"关键字。这带来了使用es6模块的问题,因此需要class

中的"module": "CommonJS"

完整tsconfig.json?

tsconfig.json

答案 4 :(得分:0)

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

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