我们先让 Task 可以跟 gateway 接上
User 跟 Message 可以晚点跟上,因为做法都一样
先在 Task 加上 federation 的 dependency
$ npm install --save @apollo/federation
or
$ yarn add @apollo/federation
将 Task 的 app.module.ts 更改,改为 GraphQLFederationModule
import { Module } from '@nestjs/common';
// 引入 GraphQLModule
import { GraphQLFederationModule } from '@nestjs/graphql';
import { TasksModule } from './tasks/tasks.module';
import { MongooseModule } from '@nestjs/mongoose';
import { join } from 'path';
@Module({
imports: [
MongooseModule.forRoot('mongodb://root:test@localhost:27012/admin'),
GraphQLFederationModule.forRoot({
autoSchemaFile: join(__dirname, 'src/schema.gql'),
}),
TasksModule
],
})
export class AppModule {}
更改 Task 的 port,可以由读者自行决定,我这边更改为 4002 ,原本的 4000 会让 gateway 使用
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
declare const module: any;
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(4002);
if (module.hot) {
module.hot.accept();
module.hot.dispose(() => app.close());
}
}
bootstrap();
接着使用 nestjs 提供的指令,建立一个 gateway
$ nest new gateway
安装需要的套件
$ yarn add @nestjs/graphql @apollo/gateway apollo-server-express
设定 gateway 的 service list
import { Module } from '@nestjs/common';
import { GraphQLGatewayModule } from '@nestjs/graphql';
@Module({
imports: [
GraphQLGatewayModule.forRootAsync({
useFactory: async () => ({
gateway: {
serviceList: [
{ name: 'task', url: 'http://localhost:4002/graphql' }
],
}
})
}),
]
})
export class AppModule {}
以上是基本的 gateway 设定,接下来会讲解如何实际使用
>>: Laravel 实战经验分享 - Day26 Dockerize 你的 Laravel 专案(下)
今天我们终於要开始进入主题了, 但是在我们写程序之前, 我们还需要先搞清楚一个东西, 那就是需求, ...
我们在上一篇的Show.html 已经完成了资料查询呈现 这里要多出操作(比方像是编辑、删除...)...
(这个标题有点耸动跟钓鱼,但不知道为什麽我就是很想用它,在文章开头先讲明。) 即使已经证实了苹果在i...
在 Class Component 的章节中我们提到过,它有别於 Function Compone...
上次已经玩过挖矿龟 Mining Turtle,此外还有其他几个类似的小乌龟 包括 Digging ...