Cannot return null for non-nullable field Apollo GraphQL

Viewed 59

This is my model:

export class Task extends BaseModel {
    @Field()
    @prop({ required: true })
    title!: string

    @Field()
    @prop({ required: true })
    index!: number

    @Field(() => User)
    @prop({ required: true, ref: User })
    createdBy!: User

    @Field(() => DateScalar, { nullable: true })
    @prop()
    completedAt?: Date

    @Field({ nullable: true })
    @prop()
    current?: boolean

    @Field(() => Task, { nullable: true })
    @prop({ ref: Task })
    parent?: string

    @Field(() => Boolean, { nullable: true })
    get completed(): boolean {
        const { completedAt } = ((this as any)._doc as Task) || this
        return Boolean(completedAt)
    }
}

This is my resolver:

@Resolver(() => Task)
export class TaskResolver {

    @Query(() => Task, { nullable: true })
    async task(@Arg('id', () => ID) id: string) {
        return await TaskModel.findById(id).exec()
    }

    @Query(() => Task, { nullable: true})
    async getTaskByTitle(@Arg('title') title: string) {
        return await TaskModel.find({ title: title }).exec()
    }
}

When I do a this query I get the error:

query GetTaskByTitle($title: String!) {
  getTaskByTitle(title: $title) {
    createdAt
    updatedAt
    title
  }
}

When I do this query I get all the correct information:

query GetTaskByTitle($taskId: ID!) {
  task(id: $taskId) {
    id
    createdAt
    updatedAt
    title
    index
  }
}

The error I am getting specifically is:

message": "Cannot return null for non-nullable field Task.title.",

What am I doing wrong in the querying process or the modeling process that is causing this issue?

0 Answers
Related