在 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