简体   繁体   English

NestJS:在使用 GraphQL 和 MongoDB 定义 InputType 和 ObjectType 字段时,我们可以使用自定义类型和类吗

[英]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.

相关问题 在MongoDB中,如果索引位于3个字段上,那么在查询2个字段时可以使用该索引吗? (第三个字段上的通配符) - In MongoDB, if an index is on 3 fields, we can use that index when querying on 2 fields? (wildcard on the 3rd field) MongoDB,Mongoose和Apollo GraphQL在文档中定义_id类型 - MongoDB, Mongoose and Apollo GraphQL defining _id type in document GraphQL 如何使用 Nestjs 或 type-graphql 设置查询 MongoDB 引用集合 - GraphQL how to setup query the MongoDB ref collection using Nestjs or type-graphql 如何在 mongoDB 中使用 $group 在其他自定义字段中使用 count/{$sum: 1} ? - How can use count/{$sum: 1} in other custom field with $group in mongoDB? MongoDB、GraphQL 和 ISODate 类型 - MongoDB, GraphQL and ISODate type MongoDB 在使用 $exists 运算符检查字段是否存在时可以使用索引吗? - Can MongoDB use an index when checking for existence of a field with $exists operator? 我们在哪里可以使用ElasticSearch?在哪里可以使用MongoDB? - Where can we use ElasticSearch and Where can we use MongoDB? 我们可以将数据(唯一值)中的主键用作MongoDB的_id字段吗? - Can we use primary key in data (unique values) as _id field of MongoDB? 我可以在我的graphQL服务器中使用MongoDB而不使用棱镜吗? - Can i use MongoDB in my graphQL server without the use of prisma? 什么时候应该使用聚合管道mongodb? - When should we use the aggregation pipeline mongodb?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM