Ivan Sim
Ivan Sim

Reputation: 375

Error: Cannot determine a GraphQL input type, while using Nestjs + Graphql + Typeorm

I am using Nestjs, Graphql and Typeorm, and i am new to it. I am currently suck on this issue. I am using @ManyToOne relationship to link food with user. but i keep on getting an error saying my class is not correct? but I imported it and looking at https://docs.nestjs.com/techniques/database#relations seems like all the syntax is correct.

Take a look at food.entity under ManyToOne, i name is usertest (for testing purpose)

In food.entity.ts

import { ObjectType, Field, ID } from '@nestjs/graphql';
import { Entity, DeepPartial, Column, PrimaryGeneratedColumn, ManyToOne } from 'typeorm';
import { AbstractEntity } from '@shared/entities/abstract.entity';
import { User } from '../user/user.entity';

@ObjectType('Food')
@Entity('foods')
export class Food extends AbstractEntity {
    constructor(input?: DeepPartial<Food>) {
        super(input);
    }

    @Field((type) => ID)
    @PrimaryGeneratedColumn()
    id: number;

    @Field({ nullable: false })
    @Column({ type: 'varchar', length: 255, nullable: false })
    name: string;

    @ManyToOne(() => User, (user) => user.id)
    usertest: User;

}

In user.entity.ts

@ObjectType('User')
@Entity('users')
export class User extends AbstractEntity {
    constructor(input?: DeepPartial<User>) {
        super(input);
    }

    @Field((type) => ID)
    @PrimaryGeneratedColumn('uuid')
    id: string;

    @Field({ nullable: true })
    @Column({ unique: true })
    email: string;

    @Field({ nullable: true })
    @Column({ unique: true })
    phoneNumber: string;

    @Field({ nullable: true })
    @Column({ default: false })
    verified: boolean;

    @Field({ nullable: true })
    @Column({ default: false })
    suspended: boolean;

    @Field({ nullable: true })
    @Column({ length: 255, nullable: true })
    name?: string;

    @Field({ nullable: true })
    @Column({ length: 120, nullable: true })
    firstName?: string;

    @Field({ nullable: true })
    @Column({ length: 120, nullable: true })
    lastName?: string;

    @Field((type) => UserRole, { nullable: true })
    @Column({ type: 'enum', enum: UserRole })
    role: UserRole;

    @Column()
    password: string;
}

Error that I receive:

(node:58449) UnhandledPromiseRejectionWarning: Error: Cannot determine a GraphQL input type for the "usertest". Make sure your class is decorated with an appropriate decorator.
    at InputTypeFactory.create (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/@nestjs/graphql/dist/schema-builder/factories/input-type.factory.js:19:23)
    at /Users/ivan/Documents/Programming/Baker/api-core/node_modules/@nestjs/graphql/dist/schema-builder/factories/input-type-definition.factory.js:44:52
    at Array.forEach (<anonymous>)
    at /Users/ivan/Documents/Programming/Baker/api-core/node_modules/@nestjs/graphql/dist/schema-builder/factories/input-type-definition.factory.js:42:33
    at resolveThunk (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/graphql/type/definition.js:478:40)
    at defineInputFieldMap (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/graphql/type/definition.js:1203:18)
    at GraphQLInputObjectType.getFields (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/graphql/type/definition.js:1151:27)
    at TypeFieldsAccessor.extractFromInputType (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/@nestjs/graphql/dist/schema-builder/services/type-fields.accessor.js:9:35)
    at /Users/ivan/Documents/Programming/Baker/api-core/node_modules/@nestjs/graphql/dist/schema-builder/factories/input-type-definition.factory.js:56:66
    at resolveThunk (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/graphql/type/definition.js:478:40)

Upvotes: 4

Views: 13598

Answers (4)

FooBar
FooBar

Reputation: 6138

I found adding @HideField() on my TypeORM relationship for my GraphQL entity would prevent me from adding an unused InputType, and thus prevent creating an unused entity in my GraphQL schema.

I then added a @ResolveField instead, which handled the logic of retrieving the relationship (e.g. through a data loader) if that was needed.

Upvotes: 0

aaronmbmorse
aaronmbmorse

Reputation: 459

You should be able to use PartialType(Entity, InputType)

https://docs.nestjs.com/graphql/mapped-types#partial

Upvotes: 0

Safi Habhab
Safi Habhab

Reputation: 1077

use GraphQLJSONObject if you want to pass an object as an input type property

 @Field(() => GraphQLJSONObject, { nullable: true })
 employee: UserObject;

Upvotes: 1

Ivan Sim
Ivan Sim

Reputation: 375

It's missing @InputType().

Like this

@ObjectType('User')
@InputType('UserInput')
@Entity('users')

I am not entirely sure if this is the right way or the right practices, but it works for me.

Upvotes: 18

Related Questions