[英]How to import directives in a nestJS graphQLModule?
我需要使用graphql-code-generator
和 typescript typescript-mongodb
插件為 mongoDB 生成 typeScript 類型,但我不明白如何在 nestJS 應用程序中導入該插件的指令。
在我的后端應用程序(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 {}
在插件的文檔中,我看到我必須使用指令:
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
})
但我不知道如何在我的使用GraphQLModule
的 nestJS 應用程序中實現這一點。
npm i @graphql-codegen/cli @graphql-codegen/typescript-mongodb
codegen.yml
overwrite: true
schema: "http://localhost:3000/graphql"
documents: null
generates:
src/generated/graphql.ts:
plugins:
- "typescript"
- "typescript-mongodb"
上面的代碼模式應該指向本地 GraphQl 端點。 上面的代碼將在src/generated/graphql.ts
文件中生成類型
"generate": "graphql-codegen --config codegen.yml"
npm run generate
注意:graphQL 服務器 ( start:dev
) 應該在使用 generate 命令時運行
工作回購鏈接 - https://github.com/vinodsai-a/nestjs-graphql-codegen-example
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.