0

我正在尝试将nestjs 与棱镜集成。我按照nestjs文档中的教程进行操作。

一切正常,直到我想进行一些更改,因此nestjs 不会公开所有查询/突变模式。

我所做的是 1. 更新 app 模块不让 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.schema.graphql文件user

#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的新手,请帮忙!

4

1 回答 1

3

我让它以某种黑客方式工作..我会将我的解决方案粘贴到这里,以供遇到同样问题的任何人使用。

例如,我有一个从以下user.schema.graphql位置导入某种类型的prisma.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 也会很棒。

于 2018-11-30T15:47:04.650 回答