[英]How to import directives in a nestJS graphQLModule?
I need to generate typeScript types for mongoDB using graphql-code-generator
and the typescript-mongodb
plugin, but I don't understand how to import the directives of that plugin in a nestJS application.我需要使用
graphql-code-generator
和 typescript typescript-mongodb
插件为 mongoDB 生成 typeScript 类型,但我不明白如何在 nestJS 应用程序中导入该插件的指令。
In my backend app (nestJS) the graphql module is defined like this:在我的后端应用程序(nestJS)中,graphql 模块的定义如下:
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
@Module({
imports: [
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
typePaths: ['./**/*.graphql']
}),
],
})
export class AppModule {}
In the docs of the plugin I see I have to use the directives:在插件的文档中,我看到我必须使用指令:
import { makeExecutableSchema } from '@graphql-tools/schema'
import { DIRECTIVES } from '@graphql-codegen/typescript-mongodb'
const schema = makeExecutableSchema({
typeDefs: [
DIRECTIVES
// the rest of your GraphQL types
],
resolvers
})
But I don't get it how to implement this in my nestJS app, which is using GraphQLModule
.但我不知道如何在我的使用
GraphQLModule
的 nestJS 应用程序中实现这一点。
npm i @graphql-codegen/cli @graphql-codegen/typescript-mongodb
codegen.yml
codegen.yml
overwrite: true
schema: "http://localhost:3000/graphql"
documents: null
generates:
src/generated/graphql.ts:
plugins:
- "typescript"
- "typescript-mongodb"
The above code schema should point to the local GraphQl endpoint.上面的代码模式应该指向本地 GraphQl 端点。 The above code will generate types in
src/generated/graphql.ts
file上面的代码将在
src/generated/graphql.ts
文件中生成类型
"generate": "graphql-codegen --config codegen.yml"
npm run generate
Note: graphQL server ( start:dev
) should be running while using generate command注意:graphQL 服务器 (
start:dev
) 应该在使用 generate 命令时运行
Working repo link - https://github.com/vinodsai-a/nestjs-graphql-codegen-example工作回购链接 - https://github.com/vinodsai-a/nestjs-graphql-codegen-example
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.