【发布时间】:2022-01-06 10:25:05
【问题描述】:
我在使用 prisma + graphQL + nestjs 时遇到问题。它不断告诉我它无法确定 graphQL 输入类型“错误:无法确定“父级”的 GraphQL 输入类型(“Org”)。确保您的类使用适当的装饰器进行装饰。”
这是我的棱镜架构,
model Org {
id String @id @default(uuid())
name String
parentId String? //an org can have a single parent org
parent Org? @relation("ParentChildren", fields: [parentId], references: [id])
children Org[] @relation("ParentChildren") // This is a self relation
}
这是用于创建的 dto
import { Field, InputType } from '@nestjs/graphql';
import { Prisma } from '@prisma/client';
import { Org } from '../entities/org.entity';
@InputType()
export class OrgCreateInput implements Prisma.OrgCreateInput {
name: string;
@Field((type) => Org)
parent?: Prisma.OrgCreateNestedOneWithoutChildrenInput;
@Field((type) => Org)
children?: Prisma.OrgCreateNestedManyWithoutParentInput;
id?: string;
}
实体
import { ObjectType } from '@nestjs/graphql';
import { Org as PrismaOrg } from '@prisma/client';
@ObjectType()
export class Org implements PrismaOrg {
parentId: string;
id: string;
name: string;
}
解析器的一部分
import { Resolver, Query, Mutation, Args } from '@nestjs/graphql';
import { OrgService } from './org.service';
import { Org } from './entities/org.entity';
import { OrgCreateInput } from './dto/create-org.input';
import { UpdateOrgInput } from './dto/update-org.input';
@Resolver(() => Org)
export class OrgResolver {
constructor(private readonly orgService: OrgService) {}
// create a org
@Mutation(() => Org, { name: 'createOrg' })
async createOrg(@Args('orgCreateInput') orgCreateInput: OrgCreateInput) {
return this.orgService.create(orgCreateInput);
}
服务者的一部分
import { Injectable } from '@nestjs/common';
import { Prisma, Org } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
@Injectable()
export class OrgService {
constructor(private prisma: PrismaService) {}
async create(data: Prisma.OrgCreateInput): Promise<Org> {
return this.prisma.org.create({
data,
});
}
我们如何处理 graphQL 中的自我关系?我们是否只需要在 graphQL 输入中使用基本字符串,然后在 prisma 中进行自我关联?还是有更好的办法?
谢谢!
【问题讨论】:
-
只是一个猜测,但
Org实体定义中的每个字段不应该也需要添加@Field装饰器吗? -
我正在使用用于 nestjs 的 graphql cli 插件,所以默认情况下不需要这些 docs.nestjs.com/graphql/cli-plugin
标签: typescript graphql nestjs prisma