Jakub
Jakub

Reputation: 2709

GraphQL error Cannot perform update query

I'm working on a ToDo list application in NodeJS, Koa, and GraphQL. I wrote an update card mutation but when I run the query to update I get the following error:

Cannot perform update query because update values are not defined. Call "qb.set(...)" method to specify updated values.

The mutation:

import { getRepository } from 'typeorm';
import { Card } from '../../entities/card';

export const updateCardMutation = {
    async updateCard(_, { id, patch }): Promise<Card> {
        const repository = getRepository(Card);
        const card = await repository.findOne({ id });
        const result = await repository.update(id, patch);
        return {
            ...card,
            ...patch,
        };
    },
};

I would like to know what I'm doing wrong and if something more is needed it please notify me so I will edit the question accordingly

card entity:

import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn } from 'typeorm';

@Entity('cards')
export class Card {
    @PrimaryGeneratedColumn('uuid')
    id: string;

    @CreateDateColumn()
    created_at: Date;

    @UpdateDateColumn()
    updated_at: Date;

    @Column('text')
    title: string;

    @Column('text', {
        nullable: true,
    })
    description: string;

    @Column('boolean', {
        default: 'false',
    })
    done: boolean;
}

Upvotes: 0

Views: 2251

Answers (4)

Vinci
Vinci

Reputation: 1286

I have had this error before with my update query is nestjs & graqhql

Cannot perform update query because update values are not defined

I have fixed it by using the save() function from the repository on the same id, so I have changed from this

async update(
    id: number,
    updateEmployeeInput: UpdateEmployeeInput,
  ): Promise<Employee> {
    await this.employeeRepository.update(id, updateEmployeeInput);
    return this.employeeRepository.findOneOrFail(id);
  }

to this

async update(
    id: number,
    updateEmployeeInput: UpdateEmployeeInput,
  ): Promise<Employee> {
    await this.employeeRepository.save(updateEmployeeInput)
    return this.employeeRepository.findOneOrFail(id);
  }

Upvotes: 1

Jakub
Jakub

Reputation: 2709

The issue was when I was calling the updateMutation, it was creating the patch object of anonymous type. So it just needed to be clean before going to the DB engine

I resolved my issues by adding the following code:

{ ...patch }

Inside the next script:

export const updateCardMutation = {
    async updateCard(_, { id, patch }): Promise<Card> {
        const repository = getRepository(Card);
        const card = await repository.findOne({ id });
        const result = await repository.update(id, { ...patch }); // Added here
        return {
            ...card,
            ...patch,
        };
    },
};

In this way, I was able to update my card.

Upvotes: 1

Muhammad
Muhammad

Reputation: 730

You need to spread the update Object.

export const updateCardMutation = {
async updateCard(_, { id, patch }): Promise<Card> {
    const repository = getRepository(Card);
    const card = await repository.findOne({ id });
    const result = await repository.update(id, {...patch}); // here
    return {
        ...card,
        ...patch,
    };
},

};

Upvotes: 2

Muhammad Ali
Muhammad Ali

Reputation: 2014

https://github.com/typeorm/typeorm/blob/master/docs/update-query-builder.md

As an error Call qb.set() , typeorm query builder are different with other orm

await getRepository().createQueryBuilder().update(Card).set(patch)
.where("id = :id", { id })
.execute();

some how patch object may stringify [object], so you can spread it like set({...patch})

Upvotes: 0

Related Questions