Skip to main content
Version: Next

wasp/server/operations

Server-side APIs for your Queries and Actions: the types for implementing them, and the functions for calling them from other server code.

Wasp generates two exports for each Query and Action you declare, both named after it. For example, for a Query declared as getFoo:

An Operation uses auth if your app uses auth, unless you set auth: false in its spec.

To call Queries and Actions from the client, use wasp/client/operations instead.

Placeholder Types​

AuthenticatedActionDefinition​

AuthenticatedActionDefinition<Entities, Input, Output> = (args, context) => Output | Promise<Output>

Placeholder

You can't import this name. Wasp generates the real exports from your Wasp Spec, named as the top of this page explains. This entry documents their general type.

The type of an authenticated Action's implementation.

It's the same as AuthenticatedQueryDefinition, but for Actions (see it for the generated type's Input and Output type arguments). Wasp generates one such type for each Action that uses auth, named after it (e.g. CreateFoo for an Action declared as createFoo). Actions that don't use auth get an UnauthenticatedActionDefinition instead.

Type Parameters​

Entities​

Entities

Input​

Input extends Payload

Output​

Output extends Payload

Parameters​

args​

Input

The data passed in when calling the Action (e.g. the data for a new task).

context​

AuthenticatedOperationContext<Entities>

An additional context object passed into the Action by Wasp. It contains the logged-in user, and the Entities the Action uses in entities.

Returns​

Output | Promise<Output>

Example​

With the Action declared as action(createFoo, { entities: ["Foo"] }):

import { type CreateFoo } from "wasp/server/operations";

type Foo = // ...

export const createFoo: CreateFoo<{ bar: string }, Foo> = (args, context) => {
// implementation
};

See​


AuthenticatedOperation​

AuthenticatedOperation<Input, Output> = 0 extends 1 & Input ? (args, context) => Promise<Output> : [Input] extends [never] ? (args, context) => Promise<Output> : [Input] extends [void] ? (context) => Promise<Output> : (args, context) => Promise<Output>

Placeholder

You can't import this name. Wasp generates the real exports from your Wasp Spec, named as the top of this page explains. This entry documents their general type.

The type of the functions for calling authenticated Queries and Actions on the server.

Wasp generates one such function for each Query and Action that uses auth, named after it.

Calling an Operation on the server is similar to calling it on the client. The difference is that you must pass in a context object with the user as the last argument. You don't have to pass the other parts of the context object, like Entities: Wasp injects them automatically.

The function's arguments depend on the Operation's Input type:

  • If it's void, the function only takes the context: getFoo({ user }).
  • Otherwise, it takes the payload and the context: getFoo(args, { user }).

Type Parameters​

Input​

Input

The type of the Operation's args (its input payload).

Output​

Output

The type of the Operation's return value (its output payload).

Example​

import { getAllTasks, getFilteredTasks, createTask } from "wasp/server/operations";

const user = // Get an AuthUser object, e.g. from context.user in an Operation.

// TypeScript automatically infers the return values and type-checks
// the payloads.
const allTasks = await getAllTasks({ user });
const doneTasks = await getFilteredTasks({ isDone: true }, { user });
const newTask = await createTask(
{ description: "Keep learning TypeScript" },
{ user },
);

See​


AuthenticatedQueryDefinition​

AuthenticatedQueryDefinition<Entities, Input, Output> = (args, context) => Output | Promise<Output>

Placeholder

You can't import this name. Wasp generates the real exports from your Wasp Spec, named as the top of this page explains. This entry documents their general type.

The type of an authenticated Query's implementation.

Wasp generates one such type for each Query that uses auth, named after it (e.g. GetFoo for a Query declared as getFoo). Queries that don't use auth get an UnauthenticatedQueryDefinition instead.

The generated type's Input and Output type arguments are optional, and their defaults make the type as permissive as possible. If you don't want your Query to take or return anything, use void as a type argument.

Using the generated type is optional, but it properly types the Query's context: TypeScript will know which Entities are in context.entities, and that context includes the user. Specifying Input and Output gives you type support for the arguments and the return value inside the implementation, and full-stack type safety: the client-side Query will have the same types.

The implementation is a Node.js function, and it can be async. Both arguments are positional, so you can name the parameters however you want.

Type Parameters​

Entities​

Entities

The type of context.entities: an object with the Prisma API for each Entity in the Query's spec (e.g. { Task: ... } for entities: ["Task"]). The generated type fills it in for you.

Input​

Input extends Payload

The type of the Query's args (its input payload). The generated type defaults it to never.

Output​

Output extends Payload

The type of the Query's return value (its output payload). The generated type defaults it to Payload (any serializable value).

Parameters​

args​

Input

The data passed in when calling the Query (e.g. filtering conditions).

context​

AuthenticatedOperationContext<Entities>

An additional context object passed into the Query by Wasp. It contains the logged-in user, and the Entities the Query uses in entities.

Returns​

Output | Promise<Output>

Examples​

With the Query getFoo declared like this:

import { app, query } from "@wasp/spec";
import { getFoo } from "./src/queries" with { type: "ref" };

export default app({
// ...
spec: [
query(getFoo, { entities: ["Foo"] }),
],
});

Wasp expects to find a named export getFoo from the file src/queries.ts. You can use the generated type GetFoo and specify the Query's inputs and outputs using its type arguments:

import { type GetFoo } from "wasp/server/operations";

type Foo = // ...

export const getFoo: GetFoo<{ id: number }, Foo> = (args, context) => {
// implementation
};

In this case, the Query expects to receive an object with an id field of type number (this is the type of args), and return a value of type Foo (this must match the type of the Query's return value).

If you don't want to explicitly type the Query's return value, the satisfies keyword tells TypeScript to infer it automatically:

import { type GetFoo } from "wasp/server/operations";

export const getFoo = (async (_args, context) => {
const foos = await context.entities.Foo.findMany();
return {
foos,
message: "Here are some foos!",
queriedAt: new Date(),
};
}) satisfies GetFoo;

TypeScript knows the correct type for context, and that the Query's return type is { foos: Foo[], message: string, queriedAt: Date }.

See​


UnauthenticatedActionDefinition​

UnauthenticatedActionDefinition<Entities, Input, Output> = (args, context) => Output | Promise<Output>

Placeholder

You can't import this name. Wasp generates the real exports from your Wasp Spec, named as the top of this page explains. This entry documents their general type.

The type of an Action's implementation, when the Action doesn't use auth.

It's the same as AuthenticatedActionDefinition, except that the context doesn't include the user. Wasp uses it for the generated Action types (e.g. CreateFoo) when your app doesn't use auth, or when you set auth: false in the Action's spec.

Type Parameters​

Entities​

Entities

Input​

Input extends Payload

Output​

Output extends Payload

Parameters​

args​

Input

context​

OperationContext<Entities>

Returns​

Output | Promise<Output>

Example​

import { type Task } from "wasp/entities";
import { type CreateTask } from "wasp/server/operations";

export const createTask: CreateTask<Pick<Task, "description">, Task> = (
args,
context,
) => {
return context.entities.Task.create({
data: { description: args.description },
});
};

UnauthenticatedOperation​

UnauthenticatedOperation<Input, Output> = 0 extends 1 & Input ? (args?) => Promise<Output> : [Input] extends [never] ? (args?) => Promise<Output> : [Input] extends [void] ? () => Promise<Output> : (args) => Promise<Output>

Placeholder

You can't import this name. Wasp generates the real exports from your Wasp Spec, named as the top of this page explains. This entry documents their general type.

The type of the functions for calling Queries and Actions that don't use auth on the server.

Wasp generates one such function for each Query and Action that doesn't use auth, named after it.

The function takes the Operation's payload as its only argument (or no arguments, if the Operation's Input type is void). You don't have to pass the context object: Wasp injects the Entities automatically.

Type Parameters​

Input​

Input

The type of the Operation's args (its input payload).

Output​

Output

The type of the Operation's return value (its output payload).

Example​

import { getAllTasks, getFilteredTasks } from "wasp/server/operations";

const allTasks = await getAllTasks();
const doneTasks = await getFilteredTasks({ isDone: true });

See​


UnauthenticatedQueryDefinition​

UnauthenticatedQueryDefinition<Entities, Input, Output> = (args, context) => Output | Promise<Output>

Placeholder

You can't import this name. Wasp generates the real exports from your Wasp Spec, named as the top of this page explains. This entry documents their general type.

The type of a Query's implementation, when the Query doesn't use auth.

It's the same as AuthenticatedQueryDefinition, except that the context doesn't include the user. Wasp uses it for the generated Query types (e.g. GetFoo) when your app doesn't use auth, or when you set auth: false in the Query's spec.

Type Parameters​

Entities​

Entities

Input​

Input extends Payload

Output​

Output extends Payload

Parameters​

args​

Input

context​

OperationContext<Entities>

Returns​

Output | Promise<Output>

Example​

import { type Task } from "wasp/entities";
import { type GetFilteredTasks } from "wasp/server/operations";

export const getFilteredTasks: GetFilteredTasks<
Pick<Task, "isDone">,
Task[]
> = async (args, context) => {
return context.entities.Task.findMany({
where: { isDone: args.isDone },
});
};

Referenced Types​

SuperJSONArray​

Extends​

Indexable​

[n: number]: SuperJSONValue


SuperJSONObject​

Indexable​

[key: string]: SuperJSONValue


WaspInternal_CustomSerializableJSONValue_Register​


AuthenticatedOperationContext​

AuthenticatedOperationContext<Entities> = object

The context Wasp passes to the implementation of a Query or Action that uses auth.

Type Parameters​

Entities​

Entities

Properties​

entities​

entities: Entities

The Prisma API for each Entity in the Operation's spec, keyed by the Entity's name. For example, context.entities.Task exposes prisma.task from Prisma's CRUD API.

user​

user: AuthUser | null

The logged-in user, or null if the request isn't authenticated. Check it with if (!context.user).

See​

Using the context.user object


CustomSerializableJSONValue​

CustomSerializableJSONValue = WaspInternal_CustomSerializableJSONValue_Register[keyof WaspInternal_CustomSerializableJSONValue_Register]


OperationContext​

OperationContext<Entities> = object

The context Wasp passes to the implementation of a Query or Action that doesn't use auth.

Type Parameters​

Entities​

Entities

Properties​

entities​

entities: Entities

The Prisma API for each Entity in the Operation's spec, as in AuthenticatedOperationContext.


Payload​

Payload = void | SuperJSONValue


SerializableJSONValue​

SerializableJSONValue = Set<SuperJSONValue> | Map<SuperJSONValue, SuperJSONValue> | undefined | bigint | Date | RegExp | CustomSerializableJSONValue


SuperJSONValue​

SuperJSONValue = JSONValue | SerializableJSONValue | SuperJSONArray | SuperJSONObject