错误:使用 Nestjs + Graphql + Typeorm 时无法确定 GraphQL 输入类型
Posted
技术标签:
【中文标题】错误:使用 Nestjs + Graphql + Typeorm 时无法确定 GraphQL 输入类型【英文标题】:Error: Cannot determine a GraphQL input type, while using Nestjs + Graphql + Typeorm 【发布时间】:2021-02-20 12:02:32 【问题描述】:我正在使用 Nestjs、Graphql 和 Typeorm,而且我是新手。 我目前在这个问题上很糟糕。我正在使用@ManyToOne 关系将食物与用户联系起来。 但我不断收到错误消息,说我的课程不正确?但我导入它并查看https://docs.nestjs.com/techniques/database#relations 似乎所有语法都是正确的。
看一下ManyToOne下的food.entity,我叫usertest(用于测试目的)
在 food.entity.ts 中
import ObjectType, Field, ID from '@nestjs/graphql';
import Entity, DeepPartial, Column, PrimaryGeneratedColumn, ManyToOne from 'typeorm';
import AbstractEntity from '@shared/entities/abstract.entity';
import User from '../user/user.entity';
@ObjectType('Food')
@Entity('foods')
export class Food extends AbstractEntity
constructor(input?: DeepPartial<Food>)
super(input);
@Field((type) => ID)
@PrimaryGeneratedColumn()
id: number;
@Field( nullable: false )
@Column( type: 'varchar', length: 255, nullable: false )
name: string;
@ManyToOne(() => User, (user) => user.id)
usertest: User;
在 user.entity.ts 中
@ObjectType('User')
@Entity('users')
export class User extends AbstractEntity
constructor(input?: DeepPartial<User>)
super(input);
@Field((type) => ID)
@PrimaryGeneratedColumn('uuid')
id: string;
@Field( nullable: true )
@Column( unique: true )
email: string;
@Field( nullable: true )
@Column( unique: true )
phoneNumber: string;
@Field( nullable: true )
@Column( default: false )
verified: boolean;
@Field( nullable: true )
@Column( default: false )
suspended: boolean;
@Field( nullable: true )
@Column( length: 255, nullable: true )
name?: string;
@Field( nullable: true )
@Column( length: 120, nullable: true )
firstName?: string;
@Field( nullable: true )
@Column( length: 120, nullable: true )
lastName?: string;
@Field((type) => UserRole, nullable: true )
@Column( type: 'enum', enum: UserRole )
role: UserRole;
@Column()
password: string;
我收到的错误:
(node:58449) UnhandledPromiseRejectionWarning: Error: Cannot determine a GraphQL input type for the "usertest". Make sure your class is decorated with an appropriate decorator.
at InputTypeFactory.create (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/@nestjs/graphql/dist/schema-builder/factories/input-type.factory.js:19:23)
at /Users/ivan/Documents/Programming/Baker/api-core/node_modules/@nestjs/graphql/dist/schema-builder/factories/input-type-definition.factory.js:44:52
at Array.forEach (<anonymous>)
at /Users/ivan/Documents/Programming/Baker/api-core/node_modules/@nestjs/graphql/dist/schema-builder/factories/input-type-definition.factory.js:42:33
at resolveThunk (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/graphql/type/definition.js:478:40)
at defineInputFieldMap (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/graphql/type/definition.js:1203:18)
at GraphQLInputObjectType.getFields (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/graphql/type/definition.js:1151:27)
at TypeFieldsAccessor.extractFromInputType (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/@nestjs/graphql/dist/schema-builder/services/type-fields.accessor.js:9:35)
at /Users/ivan/Documents/Programming/Baker/api-core/node_modules/@nestjs/graphql/dist/schema-builder/factories/input-type-definition.factory.js:56:66
at resolveThunk (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/graphql/type/definition.js:478:40)
【问题讨论】:
错误是关于 graphql 特定的“输入类型”,阅读文档 @xadm 输入类型是一个名为 User 的类。我仍然看不到问题。如果你能引导我走向正确的道路,可以吗?@InputType()
在哪里?编译器声称这是一个问题
【参考方案1】:
缺少@InputType()
。
像这样
@ObjectType('User')
@InputType('UserInput')
@Entity('users')
我不完全确定这是正确的方法还是正确的做法,但它对我有用。
【讨论】:
【参考方案2】:如果您想将对象作为输入类型属性传递,请使用 GraphQLJSONObject
@Field(() => GraphQLJSONObject, nullable: true )
employee: UserObject;
【讨论】:
以上是关于错误:使用 Nestjs + Graphql + Typeorm 时无法确定 GraphQL 输入类型的主要内容,如果未能解决你的问题,请参考以下文章
从 NestJS + GraphQL 更改 ValidationPipe 错误
Nestjs graphql 上传错误:缺少 POST 正文。你忘记使用 body-parser 中间件了吗?