[英]NestJS: Can we use custom Type and Classes when defining InputType and ObjectType Field with GraphQL & MongoDB
I'm fairly new to those stacks as I'm trying to use them to build an app (GraphQL, NestJS, and implementing with MongoDB).我对这些堆栈相当陌生,因为我正在尝试使用它们来构建应用程序(GraphQL、NestJS 以及使用 MongoDB 实现)。
I have a file for my Model, in which I export a custom Class and Type to use later on defined as such:我有一个 Model 的文件,我在其中导出自定义 Class 和类型以供稍后使用,定义如下:
address.model.ts
: address.model.ts
:
@ObjectType()
export class myAddress {
@Field(type => Int, { nullable: true })
id?: number;
@Field()
name: string;
@Field()
company: string;
@Field()
street1: string;
@Field()
street2: string;
@Field()
city: string;
@Field()
state: string;
@Field()
zip: string;
@Field()
country: string;
@Field()
email: string;
@Field()
phone: string;
@Field()
verify: string[];
@Field()
notes: string;
Following the docs in NestJS for GraphQL and Mongo integration, I created an input and dto file to, I believe, fill the fields.按照 NestJS 中 GraphQL 和 Mongo 集成的文档,我创建了一个输入和 dto 文件,我相信它可以填充字段。 They use a simple "cat" example, but I want to use my own class I defined.
他们使用一个简单的“猫”示例,但我想使用我自己定义的 class。
I've been trying to define the input and dto as such:我一直在尝试这样定义输入和 dto:
create-address.dto.ts:创建地址.dto.ts:
import { Field, ObjectType } from "@nestjs/graphql";
import { myAddress } from "../addresses.model";
@ObjectType()
export class CreateAddressDto {
@Field(type => myAddress)
readonly address: myAddress;
}
Then, in然后,在
address.input.ts:地址.input.ts:
import { Field, InputType } from "@nestjs/graphql";
import { myAddress } from "../addresses.model";
@InputType()
export class AddressInput {
@Field(type => myAddress)
readonly address: myAddress;
};
This throws me an error:这给我一个错误:
throw new cannot_determine_output_type_error_1.CannotDetermineOutputTypeError(hostType);
抛出新的 cannot_determine_output_type_error_1.CannotDetermineOutputTypeError(hostType); ^ Error: Cannot determine a GraphQL output type for the "address".
^ 错误:无法确定“地址”的 GraphQL output 类型。 Make sure your class is decorated with an appropriate decorator.
确保您的 class 已使用合适的装饰器进行装饰。 at OutputTypeFactory.create
在 OutputTypeFactory.create
Which I believe I understand, but my question is: Can we use custom classes and types when we want to create such input and dto files?我相信我理解,但我的问题是:当我们想要创建这样的输入和 dto 文件时,我们可以使用自定义类和类型吗? Or GraphQL can only handle primitive types?
或者 GraphQL 只能处理原始类型? (string, Int...).
(字符串,整数...)。
To extend my situation, I would like to create some custom class from classes from other packages.为了扩展我的情况,我想从其他包的类中创建一些自定义 class。 So I would not be writing myself every single field, but will be borrowing models from other packages.
所以我不会自己写每个领域,而是会从其他包中借用模型。 If that makes sense... To illustrate:
如果这是有道理的......为了说明:
in address.model.ts:在 address.model.ts 中:
import { Field, Int, ObjectType } from "@nestjs/graphql";
import { Address } from '@easypost/api';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
@ObjectType()
export class myAddress {
@Field(type => Int, { nullable: true }) //makes it optional
id?: number;
@Field(type => Address, { nullable: true })
address?: Address;
@Field(type => String, { nullable: true })
notes?: string;
};
export type AddressDocument = AddressDB & Document;
@Schema()
export class AddressDB {
@Prop()
address: myAddress;
}
export const AddressSchema = SchemaFactory.createForClass(AddressDB);
Thank you谢谢
I bet you already know the answer you want, because its been a 4 month since then, but just for another guy who found this question.我打赌你已经知道你想要的答案,因为从那以后已经有 4 个月了,但只是为了另一个发现这个问题的人。 You can just add in your
您只需添加您的
@Schema()
@ObjectType()
export class NeededSchema{
@Props()
@Field()
data: string;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.