SoftwareBrothers / adminjs-prisma

MIT License
44 stars 27 forks source link

Unknown argument `id`. Available options are marked with ?. #54

Open Mirasaki opened 1 month ago

Mirasaki commented 1 month ago

id is being sent in the update requests, which is not an accepted property. Full stack trace below. I tried looking for an option to exclude this field from the payload through properties, but wasn't able to find any relevant options.

User Resource

export const createUserResource = async (componentLoader: ComponentLoader) => {
  const { prisma } = await import('@repo/database');
  const { getModelByName } = await import('@adminjs/prisma');
  const { owningRelationSettingsFeature, RelationType } = await import('@adminjs/relations');
  return {
    resource: { model: getModelByName('User'), client: prisma },
    options: {
      navigation: {
        name: 'Users',
        icon: 'Users',
      },
      actions: clearUserCacheActions,
    },
    features: [
      owningRelationSettingsFeature({
        componentLoader,
        licenseKey: ADMINJS_RELATIONS_LICENSE_KEY,
        relations: {
          roles: {
            type: RelationType.ManyToMany,
            junction: {
              joinKey: 'user',
              inverseJoinKey: 'role',
              throughResourceId: 'RolesOnUser',
            },
            target: {
              resourceId: 'Role',
            },
          },
        },
      }),
    ],
  } as {
    options: ResourceOptions;
  }
};

User Model

model User {
  id           Int   @id @default(autoincrement())
  discordId    String  @unique

  email        String?  @unique
  emailVerified Boolean @default(false)
  accessToken  String? @db.Text
  refreshToken String? @db.Text

  centsSpent        Int
  claimedSpentTiers Int[]

  ApiKey             ApiKey?
  SteamLink          SteamLink?
  RolesOnUser              RolesOnUser[]
  Orders             Order[]
  PurchasedGiftCards GiftCard[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

Packages

    "@adminjs/express": "^6.1.0",
    "@adminjs/prisma": "^5.0.3",
    "@adminjs/relations": "^1.1.2",
    "@adminjs/themes": "^1.0.1",
    "adminjs": "^7.8.1",

Stack Trace

@repo/api:dev: 500 - Internal Server Error encountered:
@repo/api:dev: PrismaClientValidationError: 
@repo/api:dev: Invalid `prisma.user.update()` invocation:
@repo/api:dev: 
@repo/api:dev: {
@repo/api:dev:   where: {
@repo/api:dev:     id: 3
@repo/api:dev:   },
@repo/api:dev:   data: {
@repo/api:dev:     id: 3,
@repo/api:dev:     ~~
@repo/api:dev:     discordId: "1148597817498140774",
@repo/api:dev:     email: "me@mirasaki.devghf h fgh",
@repo/api:dev:     emailVerified: true,
@repo/api:dev:     accessToken: "tAsvS2RZeqw5sfNShcy6RPeqU2Z2LW",
@repo/api:dev:     refreshToken: "cJhMotVjGKdZwJzekA8HjfUyorz1UU",
@repo/api:dev:     centsSpent: 0,
@repo/api:dev:     claimedSpentTiers: NaN,
@repo/api:dev:     createdAt: new Date("2024-05-20T00:34:04.749Z"),
@repo/api:dev:     updatedAt: new Date("2024-05-20T13:15:39.372Z"),
@repo/api:dev: ?   ApiKey?: ApiKeyUpdateOneWithoutUserNestedInput,
@repo/api:dev: ?   SteamLink?: SteamLinkUpdateOneWithoutUserNestedInput,
@repo/api:dev: ?   RolesOnUser?: RolesOnUserUpdateManyWithoutUserNestedInput,
@repo/api:dev: ?   Orders?: OrderUpdateManyWithoutUserNestedInput,
@repo/api:dev: ?   PurchasedGiftCards?: GiftCardUpdateManyWithoutPurchaserNestedInput
@repo/api:dev:   }
@repo/api:dev: }
@repo/api:dev: 
@repo/api:dev: Unknown argument `id`. Available options are marked with ?.
@repo/api:dev:     at Tn (/home/mirasaki/projects/node/templates/full-stack-template/node_modules/.pnpm/@prisma+client@5.14.0_prisma@5.14.0/node_modules/@prisma/client/runtime/library.js:115:6855)
@repo/api:dev:     at In.handleRequestError (/home/mirasaki/projects/node/templates/full-stack-template/node_modules/.pnpm/@prisma+client@5.14.0_prisma@5.14.0/node_modules/@prisma/client/runtime/library.js:122:6533)
@repo/api:dev:     at In.handleAndLogRequestError (/home/mirasaki/projects/node/templates/full-stack-template/node_modules/.pnpm/@prisma+client@5.14.0_prisma@5.14.0/node_modules/@prisma/client/runtime/library.js:122:6211)
@repo/api:dev:     at In.request (/home/mirasaki/projects/node/templates/full-stack-template/node_modules/.pnpm/@prisma+client@5.14.0_prisma@5.14.0/node_modules/@prisma/client/runtime/library.js:122:5919)
@repo/api:dev:     at async l (/home/mirasaki/projects/node/templates/full-stack-template/node_modules/.pnpm/@prisma+client@5.14.0_prisma@5.14.0/node_modules/@prisma/client/runtime/library.js:127:11167)
@repo/api:dev:     at async Resource.update (file:///home/mirasaki/projects/node/templates/full-stack-template/node_modules/.pnpm/@adminjs+prisma@5.0.3_@prisma+client@5.14.0_prisma@5.14.0__adminjs@7.8.1_@types+babel__core@7_7wpdhlyuc4llqxwvl4tcgfbqo4/node_modules/@adminjs/prisma/lib/Resource.js:116:24)
@repo/api:dev:     at async BaseRecord.update (file:///home/mirasaki/projects/node/templates/full-stack-template/node_modules/.pnpm/adminjs@7.8.1_@types+babel__core@7.20.5_@types+react-dom@18.3.0_@types+react@18.3.2/node_modules/adminjs/lib/backend/adapters/record/base-record.js:129:30)
@repo/api:dev:     at async Object.handler (file:///home/mirasaki/projects/node/templates/full-stack-template/node_modules/.pnpm/adminjs@7.8.1_@types+babel__core@7.20.5_@types+react-dom@18.3.0_@types+react@18.3.2/node_modules/adminjs/lib/backend/actions/edit/edit-action.js:47:23)
@repo/api:dev:     at async ActionDecorator.handler (file:///home/mirasaki/projects/node/templates/full-stack-template/node_modules/.pnpm/adminjs@7.8.1_@types+babel__core@7.20.5_@types+react-dom@18.3.0_@types+react@18.3.2/node_modules/adminjs/lib/backend/decorators/action/action-decorator.js:56:19)
@repo/api:dev:     at async ApiController.recordAction (file:///home/mirasaki/projects/node/templates/full-stack-template/node_modules/.pnpm/adminjs@7.8.1_@types+babel__core@7.20.5_@types+react-dom@18.3.0_@types+react@18.3.2/node_modules/adminjs/lib/backend/controllers/api-controller.js:149:28) {
@repo/api:dev:   clientVersion: '5.14.0'
@repo/api:dev: }
@repo/api:dev: POST /admin/api/resources/User/records/3/edit 500 16.695 ms - -
dziraf commented 1 month ago

Can you try this before hook in edit in the resource that's edited?

import { Before } from 'adminjs';

const excludeIdPropertyFromPayload: Before = async (request, context) => {
  if (request.method === 'get') {
    return request;
  }

  const resource = context.resource.decorate();

  const { payload = {} } = request;
  const updatedPayload = {};

  for (const key of Object.keys(payload)) {
    const property = resource.getPropertyByKey(key);

    if (!property || !property.isId()) {
      updatedPayload[key] = payload[key];
    }
  }

  request.payload = updatedPayload;

  return request;
};
Mirasaki commented 1 month ago

That addresses the issue, thanks. I'm not sure if I'm supposed to close this, though - as I reckon it's an internal bug. It only seems to happen when using @adminjs/relations.