在tsconfig.json中开启experimentalDecorators和emitDecoratorMetadata配置
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
安装@notadd/ast.ts-graphql(graphql配置文件自动生成库,仅开发时使用),所有需要代码扫描的源文件所在目录需要配置到tsconfig.json中的"include": ["src"]
yarn add @notadd/graphql
yarn add @notadd/apollo-fastify
yarn add @notadd/ast.ts-graphql --dev
import { ApolloFastifyModule } from '@notadd/apollo-fastify';
import { GraphqlModule } from '@notadd/graphql';
@Module({
imports: [ApolloFastifyModule, GraphqlModule.forDefault((i) => i)],
})
class AppModule implements OnModuleInit {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
ngOnModuleInit() {
console.log(`模块初始化完成`);
}
}
yarn add @notadd/core
yarn add @notadd/platform-node
MAIN_PATH,需要在platformNode中被设置为启动文件的路径__filename,notadd框架将在此文件目录搜索.evn文件,并初始化环境变量,使用graphql时,notadd框架自动生成的graphql配置文件将从此文件进行搜索
import { MAIN_PATH,PLATFORM_NAME } from '@notadd/core';
platformNode([
{
provide: MAIN_PATH,
useValue: __filename,
},
{
provide: PLATFORM_NAME,
useValue: 'Hello World',
},
])
.bootstrapModule(AppModule)
.then(
async (res): Promise<void> => {
console.log(res);
console.log(`启动初始化完成`);
},
);
/* HelloWorld.ts */
import { Controller } from '@notadd/core';
import { Query, Args } from '@notadd/graphql';
interface Output {
/**
* HelloWorld 返回信息
*/
message: string;
}
interface Input {
/**
* Hello World 请求信息
*/
name: string;
}
@Controller()
export class HelloWorld {
@Query()
async helloworld(@Args('input') input: Input): Promise<Output> {
return { message: `${input.name} Hello World!` };
}
}
import { HelloWorld } from 'HelloWorld';
@Module({
imports: [ApolloFastifyModule, GraphqlModule.forDefault((i) => i)],
controllers: [HelloWorld],
})
class AppModule implements OnModuleInit {
async ngOnModuleInit(): Promise<void> {
console.log(`模块初始化完成`);
}
}
├── package.json
├── README.md
├── src
│ ├── HelloWorld.ts
│ ├── index.graphql
│ └── index.ts
└── tsconfig.json