Krzysztof Kaczyński
Krzysztof Kaczyński

Reputation: 5071

Why I am getting the error "cannot determine GraphQL output type"?

I am trying to create simple appliaction with Nest.js, GraphQL and MongoDB. I wnated to use TypeORM and TypeGraphql to generate my schema and make a connection with localhost databasebut but i can not run my server with nest start becouse I am getting this error:

UnhandledPromiseRejectionWarning: Error: Cannot determine GraphQL output type for getArticles

I have no idea why i am getting this error. My class ArticleEntity does't has any not primary types, so there should not be any problem. I tried to remove () => ID from @Field() decorator of filed _id of ArticleEntity class but it didn't helped

ArticleResolver

@Resolver(() => ArticleEntity)
export class ArticlesResolver {
  constructor(
    private readonly articlesService: ArticlesService) {}

  @Query(() => String)
  async hello(): Promise<string> {
    return 'Hello world';
  }

  @Query(() => [ArticleEntity])
  async getArticles(): Promise<ArticleEntity[]> {
    return await this.articlesService.findAll();
  }

}

ArticleService

@Injectable()
export class ArticlesService {
  constructor(
    @InjectRepository(ArticleEntity)
    private readonly articleRepository: MongoRepository<ArticleEntity>,
  ) {}

  async findAll(): Promise<ArticleEntity[]> {
    return await this.articleRepository.find();
  }
}

ArticleEntity

@Entity()
export class ArticleEntity {
  @Field(() => ID)
  @ObjectIdColumn()
  _id: string;

  @Field()
  @Column()
  title: string;

  @Field()
  @Column()
  description: string;
}

ArticleDTO

@InputType()
export class CreateArticleDTO {
  @Field()
  readonly title: string;

  @Field()
  readonly description: string;
}

If you need anything else comment

Upvotes: 25

Views: 30359

Answers (6)

Adam
Adam

Reputation: 1699

I encountered this error as a result of missing the @ symbol before ObjectType. I Hope this helps anyone else with the same problem.

I had

ObjectType()
export class User {

Rather than

@ObjectType()
export class User {

Upvotes: 2

Adeel Shekhani
Adeel Shekhani

Reputation: 1105

Output model class should be decorated with @ObjectType() and then all properties of that class will decorated with @Field(). For any one who is using a custom output model class and NOT an entity(sequelize, typeorm, prisma etc). Because I was using database entity first, everything was working fine till I moved to a more customized output model.

One more case would be someone using class A as output and class B is used within A

export class A{
id: number;
name:string;
childProperty: B
. . . . .
}


export class B{
 prop1:string;
 prop2:string;
}

In that case class B should also be decorated with @ObjectType and fields (prop1 , prop2 ) should be also be decorated with @Field as well.

Upvotes: 0

jkeys
jkeys

Reputation: 3955

For anyone who gets this error and uses enums, you may be missing a call to registerEnumType.

Upvotes: 20

Daniel Rearden
Daniel Rearden

Reputation: 84657

ArticleEntity should be decorated with the @ObjectType decorator as shown in the docs https://typegraphql.com/docs/types-and-fields.html.

@Entity()
@ObjectType()
export class ArticleEntity {
  ...
}

Upvotes: 33

srk
srk

Reputation: 1901

In my case, I was using the @ObjectType decorator, but I was importing from type-graphql. I imported from @nestjs/graphql instead, and the problem was resolved.

import { ObjectType } from '@nestjs/graphql';

See here for a related discussion on GitHub.

Upvotes: 8

Clement
Clement

Reputation: 4811

I was using MongoDB and I had my Query return the schema instead of the model class.

Changing @Query((returns) => UserSchema) to @Query((returns) => User) fixed the issue for me.

user.schema.ts

@ObjectType()
@Schema({ versionKey: `version` })
export class User {
    @Field()
    _id: string

    @Prop({ required: true })
    @Field()
    email: string

    @Prop({ required: true })
    password: string
}

export const UserSchema = SchemaFactory.createForClass(User)

user.resolver.ts

@Query((returns) => User)
async user(): Promise<UserDocument> {
    const newUser = new this.userModel({
        id: ``,
        email: `[email protected]`,
        password: `abcdefg`,
    })
    return await newUser.save()
}

Upvotes: 0

Related Questions