Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 1773
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 31, 20222022-05-31T02:06:55+00:00 2022-05-31T02:06:55+00:00

NestJS: Can we use custom Type and Classes when defining InputType and ObjectType Field with GraphQL & MongoDB

  • 0

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

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 4798 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1063 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1010 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.