[ad_1]
I’m fairly new to those stacks as I’m trying to use them to build an app (GraphQL, NestJS, and implementing with MongoDB).
I have a file for my Model, in which I export a custom Class and Type to use later on defined as such:
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. They use a simple “cat” example, but I want to use my own class I defined.
I’ve been trying to define the input and dto as such:
create-address.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:
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);
^
Error: Cannot determine a GraphQL output type for the “address”. Make sure your class is decorated with an appropriate decorator.
at 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? Or GraphQL can only handle primitive types? (string, Int…).
To extend my situation, I would like to create some custom class from classes from other packages. 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:
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
[ad_2]