Skip to content

Make CRUD operation types match regular operations #2541

Closed
@Genyus

Description

@Genyus

There is currently a discrepancy between the type signatures of regular operations (actions/queries) and CRUD operations.

Operation Type Definition:

export type GetTemplate<Input extends Payload = never, Output extends Payload = Payload> = 
  AuthenticatedQueryDefinition<[_Template], Input, Output>;

CRUD Operation Type Definition:

type GetQuery<Input extends Payload, Output extends Payload> = 
  AuthenticatedQueryDefinition<[_WaspEntityTagged], Input, Output>;

The critical difference is in the default type parameters:

  1. Regular operation definition has default values:
  • <Input extends Payload = never, Output extends Payload = Payload>
  • The = never and = Payload parts provide default values
  1. CRUD operation definition has no default values:
  • <Input extends Payload, Output extends Payload>

This means that for regular operations, the output type can be set using the satisfies keyword, e.g.

export const getAllTasks = async (_args, context) => {
  return context.entities.Task.findMany({
   ...
  }) satisfies GetAllTasks<void>;
};

and on the client-side, the query type can be inferred:

Awaited<ReturnType<typeof getAllTasks>>[0];

But this technique fails for CRUD operations due to the lack of default values in the type definitions.

To Reproduce
Steps to reproduce the behavior:

  1. Define a CRUD:
crud Tasks {
  entity: Task,
  operations: {
    getAll: {
      isPublic: true, // by default only logged in users can perform operations
      overrideFn: import { getAllTasks } from "@src/tasks.ts",
    },
    get: {},
    create: {},
    update: {},
  },
}
  1. Create an override function:
import { Tasks } from 'wasp/server/crud/Tasks';

export const getAllTasks = (async (_args, context) => {
  return await context.entities.Task.findMany();
}) satisfies Tasks.GetAllQuery<void>;
  1. See compiler error shown in IDE: Generic type 'GetAllQuery' requires 2 type argument(s).

Expected behavior
The CRUD function should work in the same way as a regular operation, allowing for client-side query type inference.

Additional context
Present in Wasp 0.16.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions