繁体   English   中英

如何在nestJS graphQLModule 中导入指令?

[英]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 应用程序中实现这一点。

  1. 将这些包安装到当前的nestjs repo
npm i @graphql-codegen/cli @graphql-codegen/typescript-mongodb
  1. 添加 codegen 配置文件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文件中生成类型

  1. 在你的 package.json 脚本中添加生成脚本
    "generate": "graphql-codegen --config codegen.yml"
  1. 运行生成脚本
npm run generate

注意:graphQL 服务器 ( start:dev ) 应该在使用 generate 命令时运行

工作回购链接 - https://github.com/vinodsai-a/nestjs-graphql-codegen-example

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM