Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CRUD operation types match regular operations #2541

Open
Genyus opened this issue Mar 11, 2025 · 0 comments · May be fixed by #2542
Open

Make CRUD operation types match regular operations #2541

Genyus opened this issue Mar 11, 2025 · 0 comments · May be fixed by #2542

Comments

@Genyus
Copy link
Contributor

Genyus commented Mar 11, 2025

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

@Genyus Genyus linked a pull request Mar 11, 2025 that will close this issue
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant