Search code examples
typescriptapolloprismatypegraphql

Object literal may only specify known properties, and 'clientId' does not exist in type 'RatesWhereUniqueInput'


I'm using typescript with prisma and typegraphql and getting this type error. RatesWhereUniqeInput is generated by prisma and is defining itself as a "CompoundUniqueInput" because the database I am referencing has 2 keys (clientId: string, employeeId: number).

In my repository I want to use both of these to reference a specific database line using "update()" but because the type is generated within the prisma client as clientId_employeeId? I am getting a type error.

repository function

    async update(model: Rates): Promise<Rates> {
        const { employeeId, clientId, ...data } = this.mapper.from(model);

        const entity = await this.db.rates.update({
            where: { employeeId, clientId },
            data: {
                ...data,
            },
        });

        return this.mapper.to(entity);
    }

prisma index.d.ts

  export type RatesWhereUniqueInput = {
    clientId_employeeId?: RatesClientIdEmployeeIdCompoundUniqueInput
  }

ratesmapper.ts

@injectable()
export class RatesMapper {
    public from(model: Rates): RatesEntity {
        return {
            employeeId: model.employeeId,
            clientId: model.clientId,
            rateFull: model.rateFull,
            rateQuarter: model.rateQuarter,
            rateLine: model.rateLine,
            rateWord: model.rateWord,
            createdAt: model.createdAt,
            updatedAt: model.updatedAt,
        };
    }

    public to(entity: RatesEntity): Rates {
        return new Rates({
            ...entity,
        });
    }
}

Solution

  • Just to clarify, you're trying to run a update query using prisma on single record from the Rates table/model. To do this you're getting an error in your where condition.

    Solution

    Try changing the update query as follows:

    const entity = await this.db.rates.update({
            where: {
                clientId_employeeId: {
                    employeeId,
                    clientId,
                },
            },
            data: {
                ...data,
            },
        });
    
    

    This is the syntax Prisma uses when specifying the where condition for models with Compound ID or unique identifier (an ID or unique identifier that is made up of multiple fields of the model). You can read more in the get record by compound ID or compound unique identifier subsection of the CRUD Reference Guide in the Prisma docs.