Baldráni
Baldráni

Reputation: 5640

Cannot return null for non-nullable field Message.sender with GraphQl and TypeORM

So I'm trying to figure out why does my Query end up with:

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Message.sender.",
    }
    ...
}

This is my entity

@Entity()
export class Message extends BaseEntity {
    @PrimaryGeneratedColumn("uuid")
    id: string;

    @Column("text")
    content: string;

    @CreateDateColumn()
    created_at: string;

    // @ts-ignore
    @ManyToOne(type => User, user => user.messages)
    sender: User;
}

And there goes my resolver :

allMessagesOfProject: async (_, { projectId }, __) => {
    const project = await Project.findOne({
        relations: ["messages"],
        where: { id: projectId }
    });
    if (project) {
        const messages = project.messages;
        return messages.reverse();
    }
    return null;
},

Did I miss something ?

Upvotes: 0

Views: 690

Answers (1)

Baldráni
Baldráni

Reputation: 5640

Okay I've just found out that TypeORM add a layer of security by not allowing to access children if not precise so you have to use the eager option.

@ManyToOne(type => User, user => user.messages, {eager: true})
sender: User;

Hopefully this help someone later on.

Upvotes: 8

Related Questions