如何在 nestjs 中只公开部分 prima 生成的模式

How to only expose part of prima generated schema in nestjs

我正在尝试将 nestjs 与 prisma 集成。我按照 nestjs 文档中的 tutorial

一切正常,直到我想进行一些更改,以便 nestjs 不会公开所有 query/mutations 架构。

我做的是 1. 更新应用程序模块不让 typePath 包含生成的模式

import { Module } from '@nestjs/common';
import { AppController } from 'app/app.controller';
import { AppService } from 'app/app.service';
import { GraphQLModule } from '@nestjs/graphql';
import { UserModule } from './user/user.module';

@Module({
  imports: [
    GraphQLModule.forRoot({
      typePaths: ['./src/**/*.schema.graphql'],
      debug: true,
      playground: true,
    }),
    UserModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

2。然后我在 user 文件夹中创建了一个 user.schema.graphql 文件:

#import User from "../prisma/generated/prisma.graphql"

type Query {
  user(id: ID!): User
}

但是,我遇到一些错误,提示我无法导入用户:

(node:36277) UnhandledPromiseRejectionWarning: Error: Type "User" not found in document.
    at ASTDefinitionBuilder._resolveType (/Users/anx/Projects/axesk/gallery-api/node_modules/graphql/utilities/buildASTSchema.js:100:11)
    at ASTDefinitionBuilder.buildType (/Users/anx/Projects/axesk/gallery-api/node_modules/graphql/utilities/buildASTSchema.js:210:79)
    at ASTDefinitionBuilder._buildWrappedType (/Users/anx/Projects/axesk/gallery-api/node_modules/graphql/utilities/buildASTSchema.js:229:17)
    at ASTDefinitionBuilder.buildField (/Users/anx/Projects/axesk/gallery-api/node_modules/graphql/utilities/buildASTSchema.js:249:18)
    at /Users/anx/Projects/axesk/gallery-api/node_modules/graphql/utilities/buildASTSchema.js:332:21
    at /Users/anx/Projects/axesk/gallery-api/node_modules/graphql/jsutils/keyValMap.js:36:31
    at Array.reduce (<anonymous>)
    at keyValMap (/Users/anx/Projects/axesk/gallery-api/node_modules/graphql/jsutils/keyValMap.js:35:15)
    at ASTDefinitionBuilder._makeFieldDefMap (/Users/anx/Projects/axesk/gallery-api/node_modules/graphql/utilities/buildASTSchema.js:329:48)
    at fields (/Users/anx/Projects/axesk/gallery-api/node_modules/graphql/utilities/buildASTSchema.js:312:22)
(node:36277) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:36277) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我是 nestjs 和 graphql 的新手,请帮忙!

我让它以一种 hack 的方式工作..我会把我的解决方案贴在这里,以供遇到同样问题的任何人使用。

例如,我有一个 user.schema.graphqlprisma.graphql 导入某种类型:

#import User from "../prisma/generated/prisma.graphql"

type Query {
  user(id: ID!): User
}

然后我创建一个 main.schema.graphql 文件,它将手动从项目中导入每个模式。

#import * from "./user/user.schema.graphql"

最后只需将此 maim 模式导入 app.module.ts:

import { Module } from '@nestjs/common';
import { AppController } from 'app/app.controller';
import { AppService } from 'app/app.service';
import { GraphQLModule } from '@nestjs/graphql';
import { UserModule } from './user/user.module';
import { importSchema } from 'graphql-import';

@Module({
  imports: [
    GraphQLModule.forRoot({
      resolverValidationOptions: {
        requireResolversForResolveType: false,
      },
      typeDefs: importSchema('./src/main.schema.graphql'),
      debug: true,
      playground: true,
    }),
    UserModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

这样做不太理想。如果 graphql-import 库可以从文件夹中导入,它会让生活更轻松。

或者@nest/graphql 库可以在后台使用 graphql-import 而不是 merge-graphql-schemas 来构建 typeDefs 也很棒。