簡體   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