【发布时间】:2019-06-26 03:46:37
【问题描述】:
首先,我在 Stack Overflow 上发帖时还比较陌生,但我当然会尽我所能在此处获取所有相关信息,并在找到解决方案后分享解决方案,因为我可以想象更多人可能会遇到此问题。
所以我们从一个有多个小型微服务作为后端的系统开始,我们发现 Apollo 服务器能够从 graphql 端点检索模式并将它们拼接在一起,这样我们就可以有一个很好的入口点。 我们已经完成了这项工作,但 apollo 服务器对整体架构没有任何帮助。那是我们发现 NestJS 的时候,因为我们在前端使用了 angular,而 NestJS 与它非常相似,看起来非常合适。
我们遇到的问题是我们似乎无法使以下功能正常工作: - 我想要一个包含一个服务的模块,该服务可以被赋予多个端点(uri's to microservices) - 使用给定的端点,服务应该从这些端点检索 graphQL 模式,并将它们变成 RemoteExecutableSchemas,然后合并它们。 - 在合并它们并使用(远程)链接信息制作 1 个大模式之后,以便 graphQL 知道从哪里获取数据。 - 发生这种情况后,我们想添加一些拼接,以便所有关系都存在(但这不是我的问题所在)
我一直在通过官方文档 (https://docs.nestjs.com/graphql/quick-start) 浏览它们的示例 (https://github.com/nestjs/nest/tree/master/sample/12-graphql-apollo),当然还检查了 github 项目 (https://github.com/nestjs/graphql) 并一直在这个 repo 中寻找什么代码在后台运行。
我们尝试了几种方法来即时获取它们,但在实例化之前无法将模式导入 GraphQLModule。然后我们认为让服务从端点检索 graphqlSchema 并使用实际有效的 printSchema(schema) 将其写入文件可能是可以接受的,但随后我丢失了链接信息,使其成为本地模式而不是远程模式. 现在我们提出了以下问题,但再次陷入困境。
让我们从我的 package.json 中的一点 sn-p 开始,这样人们就知道版本了 :)
"dependencies": {
"@nestjs/common": "^5.4.0",
"@nestjs/core": "^5.4.0",
"@nestjs/graphql": "^5.5.1",
"apollo-link-http": "^1.5.9",
"apollo-server-express": "^2.3.2",
"graphql": "^14.1.1",
"reflect-metadata": "^0.1.12",
"rimraf": "^2.6.2",
"rxjs": "^6.2.2",
"typescript": "^3.0.1"
},
"devDependencies": {
"@nestjs/testing": "^5.1.0",
"@types/express": "^4.16.0",
"@types/jest": "^23.3.1",
"@types/node": "^10.7.1",
"@types/supertest": "^2.0.5",
"jest": "^23.5.0",
"nodemon": "^1.18.3",
"prettier": "^1.14.2",
"supertest": "^3.1.0",
"ts-jest": "^23.1.3",
"ts-loader": "^4.4.2",
"ts-node": "^7.0.1",
"tsconfig-paths": "^3.5.0",
"tslint": "5.11.0"
},
所以,目前我有一个模式处理程序模块,如下所示:
@Module({
imports: [GraphQLModule.forRootAsync({
useClass: GqlConfigService
})],
controllers: [SchemaHandlerController],
providers: [SchemaFetcherService, SchemaSticherService, GqlConfigService]
})
export class SchemaHandlerModule {
}
所以在这里我们导入 GraphQLModule 并让它使用 gql-config 服务来处理给它 GraphQLModuleOptions。
gql-config 服务如下所示:
@Injectable()
export class GqlConfigService implements GqlOptionsFactory {
async createGqlOptions(): Promise<GqlModuleOptions> {
try{
const countrySchema = this.createCountrySchema();
return {
typeDefs: [countrySchema]
};
} catch(err) {
console.log(err);
return {};
}
}
所以我正在异步创建 GqlModuleOptions 并等待结果。 createCountrySchema 函数如下所示:
public async createCountrySchema() : GraphQLSchema{
const uri = 'https://countries.trevorblades.com/Graphql';
try {
const link = new HttpLink({
uri: uri,
fetch
});
const remoteSchema = await introspectSchema(link);
return makeRemoteExecutableSchema({
schema: remoteSchema,
link
});
} catch (err) {
console.log('ERROR: exception when trying to connect to ' + uri + ' Error Message: ' + err);
}
};
为了 POC,我刚刚获得了一个简单的公共 graphQL API 作为端点。此函数返回一个 GraphQLSchema 对象,然后我想(以某种方式)将其添加到 GqlOptions 并使其在操场上可见。 我们还尝试让 createCountrySchema 返回一个 Promise 并在调用 createGqlOptions 中的函数时等待它,但这似乎没有什么不同。
我们得到的实际错误如下所示:
[Nest] 83 - 2/1/2019, 2:10:57 PM [RoutesResolver] SchemaHandlerController {/schema-handler}: +1ms
apollo_1 | (node:83) UnhandledPromiseRejectionWarning: Syntax Error: Unexpected [
apollo_1 |
apollo_1 | GraphQL request (2:9)
apollo_1 | 1:
apollo_1 | 2: [object Promise]
apollo_1 | ^
apollo_1 | 3:
apollo_1 |
apollo_1 | at syntaxError (/opt/node_modules/graphql/error/syntaxError.js:24:10)
apollo_1 | at unexpected (/opt/node_modules/graphql/language/parser.js:1483:33)
apollo_1 | at parseDefinition (/opt/node_modules/graphql/language/parser.js:155:9)
apollo_1 | at many (/opt/node_modules/graphql/language/parser.js:1513:16)
apollo_1 | at parseDocument (/opt/node_modules/graphql/language/parser.js:115:18)
apollo_1 | at parse (/opt/node_modules/graphql/language/parser.js:50:10)
apollo_1 | at parseDocument (/opt/node_modules/graphql-tag/src/index.js:129:16)
apollo_1 | at Object.gql (/opt/node_modules/graphql-tag/src/index.js:170:10)
apollo_1 | at GraphQLFactory.<anonymous> (/opt/node_modules/@nestjs/graphql/dist/graphql.factory.js:48:55)
apollo_1 | at Generator.next (<anonymous>)
apollo_1 | (node:83) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
apollo_1 | (node:83) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我认为我很接近这种方法,但我不太确定。我得到的错误指出,所有的 Promise 都必须用 try/catch 处理,这样我们就不会得到未处理的 Promise,我相信我到处都这样做,所以我不明白这个错误来自哪里......
如果有人有任何指示、解决方案或建议,我将非常高兴。一个多星期以来,我一直在努力获得我们想要在 nestjs 中安装的功能,并且已经看到了大量的示例、sn-ps 和关于此的讨论,但我找不到缝合远程模式并将它们交还给 nestjs 的示例.
我将非常感谢任何关于此的 cmets,并致以诚挚的问候, 杰德
【问题讨论】:
-
你是怎么调用createGqlOptions函数的?
-
那将在 SqlConfigService 中。当 GraphQLModule 被导入 schema-handler 模块时,服务被注入。
标签: typescript graphql apollo-server nestjs