Reputation: 667
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
Upvotes: 0
Views: 2844
Reputation: 21
Answering for future people who are looking for an answer.
In below code, you are using the myAddress class in an InputType() class, graphql will expect that myAddress is also decorated with InputType() decorator:
@InputType()
export class AddressInput {
@Field(type => myAddress)
readonly address: myAddress;
};
So to fix this, you would need to add @InputType() decorator in the myAddress class as well, like below:
@ObjectType()
@InputType('myAddressInput')
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;
Upvotes: 1
Reputation: 96
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. You can just add in your
@Schema()
@ObjectType()
export class NeededSchema{
@Props()
@Field()
data: string;
}
Upvotes: 0