The screenshot API for developers -
Try ScreenshotOne
Skip to content

Pinia Colada Integration

Pinia Colada integration provides utilities for using oRPC clients with Pinia Colada. It includes helper methods for building query and mutation options, as well as query and mutation keys.

WARNING

This guide assumes you are already familiar with Pinia Colada. If you need a refresher, review the official Pinia Colada documentation before continuing.

Installation

sh
npm install @orpc/pinia-colada@beta
sh
yarn add @orpc/pinia-colada@beta
sh
pnpm add @orpc/pinia-colada@beta
sh
bun add @orpc/pinia-colada@beta
sh
deno add npm:@orpc/pinia-colada@beta

Setup

Before you begin, set up either a server-side client or a client-side client.

ts
import { createPiniaColadaUtils } from '@orpc/pinia-colada'

const orpc = createPiniaColadaUtils(client)
Avoiding Query and Mutation Key Conflicts?

To avoid key conflicts when creating multiple sets of utils, pass a unique prefix. It becomes the first element of every entry key, so entries from different utils never overlap.

ts
const userORPC = createPiniaColadaUtils(userClient, {
  prefix: 'user'
})

const postORPC = createPiniaColadaUtils(postClient, {
  prefix: 'post'
})

Query Options Utility

Use .queryOptions to build query options. It works with useQuery, defineQueryOptions, and any other API that accepts query options.

ts
const query = useQuery(orpc.planet.find.queryOptions({
  input: { id: 123 }, // Specify input if needed
  context: { cache: true }, // Provide client context if needed
  // additional options...
}))

INFO

Options accept plain values only. For reactive inputs, pass a callback to useQuery as described in Reactive Options.

Infinite Query Options Utility

Use .infiniteOptions to build infinite query options. It works with useInfiniteQuery, defineInfiniteQueryOptions, and any other API that accepts infinite query options.

INFO

The input option must be a function that receives the page parameter and returns the query input. Define the pageParam type explicitly if it can be null or undefined.

ts
const query = useInfiniteQuery(() => orpc.planet.list.infiniteOptions({
  input: (offset: number) => ({ limit: 10, offset }),
  context: { cache: true }, // Provide client context if needed
  initialPageParam: 0,
  getNextPageParam: lastPage => lastPage.nextOffset,
  // additional options...
}))

Mutation Options

Use .mutationOptions to build mutation options. It works with useMutation and any other API that accepts mutation options.

ts
const mutation = useMutation(orpc.planet.create.mutationOptions({
  context: { cache: true }, // Provide client context if needed
  // additional options...
}))

mutation.mutate({ name: 'Earth' })

Query/Mutation Key

oRPC provides helper methods for generating query and mutation keys:

  • .key: Generates a partial-match key for actions such as invalidating queries or checking mutation status.
  • .queryKey: Generates a full-match key for Query Options.
  • .infiniteKey: Generates a full-match key for Infinite Query Options.
  • .mutationKey: Generates a full-match key for Mutation Options.
ts
const queryCache = useQueryCache()

// Invalidate all planet queries
queryCache.invalidateQueries({
  key: orpc.planet.key(),
})

// Invalidate only regular (non-infinite) planet queries
queryCache.invalidateQueries({
  key: orpc.planet.key({ type: 'query' })
})

// Invalidate the planet find query with id 123
queryCache.invalidateQueries({
  key: orpc.planet.find.key({ input: { id: 123 } })
})

// Update the planet find query with id 123
queryCache.setQueryData(orpc.planet.find.queryKey({ input: { id: 123 } }), (old) => {
  return { ...old, id: 123, name: 'Earth' }
})

INFO

Because Pinia Colada requires entry keys to be serializable, oRPC serializes inputs into JSON-compatible values (including native types like Date, URL, BigInt, etc.) when building keys.

Calling Procedure Clients

The .call method provides direct access to the underlying procedure client when needed.

ts
const planet = await orpc.planet.find.call({ id: 123 })

Reactive Options

Following the defineQueryOptions pattern, option utilities accept plain values only. For reactive inputs, pass a callback to useQuery instead — it re-evaluates whenever its dependencies change.

ts
const id = ref(123)

const query = useQuery(() => orpc.planet.find.queryOptions({
  input: { id: id.value },
}))

This also composes with defineQueryOptions:

ts
const findPlanetQuery = defineQueryOptions(
  (id: number) => orpc.planet.find.queryOptions({ input: { id } }),
)

const query = useQuery(() => findPlanetQuery(id.value))

Default Options

Use scoped to configure default options for scoped query and mutation utilities. Each value can be either a partial options object, which is spread-merged with lower priority than per-call options, or a function that receives the per-call options and returns the merged result.

ts
const orpc = createPiniaColadaUtils(client, {
  scoped: {
    planet: {
      find: {
        queryKey: options => ({
          // Override the auto-generated key for .queryKey and .queryOptions
          key: options.key ?? ['planet', 'find', options.input]
        }),
        queryOptions: {
          staleTime: 60 * 1000, // 1 minute
        },
      },
      create: {
        mutationOptions: {
          onSuccess: () => {
            // runs for every planet.create mutation
          },
        },
      },
    },
  },
})

// These calls automatically use the default options
const query = useQuery(orpc.planet.find.queryOptions({ input: { id: 123 } }))
const mutation = useMutation(orpc.planet.create.mutationOptions())

// User-provided options take precedence
const customQuery = useQuery(orpc.planet.find.queryOptions({
  input: { id: 123 },
  staleTime: 0, // overrides the default staleTime
}))

INFO

When you configure queryKey, it also affects .queryOptions because it is used internally to generate keys. The same applies to infinite and mutation options when you configure their keys.

Interceptors

Interceptors let you wrap query and mutation calls. Unlike default options, which can be overridden by per-call options, interceptors always run for every query and mutation.

ts
import { isInferableError, safe } from '@orpc/client'

const orpc = createPiniaColadaUtils(client, {
  queryInterceptors: [],
  infiniteInterceptors: [],
  mutationInterceptors: [
    async ({ context, path, next }) => {
      const [error, data] = await safe(next())

      if (error) {
        if (isInferableError(error)) {
          // handle typesafe errors
        }

        throw error
      }

      return data
    }
  ],
})

INFO

You can use safe and isInferableError together for typesafe error handling in interceptors.

Plugins

Plugins package reusable defaults and interceptors for queries and mutations.

ts
const orpc = createPiniaColadaUtils(client, {
  plugins: []
})

Client Context

When a client is invoked through the Pinia Colada integration, an operation context is automatically added to the client context. You can use this context to configure request behavior, such as selecting the HTTP method for RPC Link.

ts
import {
  PINIA_COLADA_OPERATION_CONTEXT_SYMBOL,
  PiniaColadaOperationContext,
} from '@orpc/pinia-colada'
import { RPCLink } from '@orpc/client/fetch'

interface ClientContext extends PiniaColadaOperationContext {
}

const GET_OPERATION_TYPE = new Set(['query', 'infinite'])

const link = new RPCLink<ClientContext>({
  method: ({ context }) => {
    const operationType = context[PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]?.type

    if (operationType && GET_OPERATION_TYPE.has(operationType)) {
      return 'GET'
    }

    return 'POST'
  },
})

Typesafe Error Handling

Use the built-in isInferableError helper to handle typesafe errors in queries and mutations.

ts
import { isInferableError } from '@orpc/client'

const mutation = useMutation(orpc.planet.create.mutationOptions({
  onError: (error) => {
    if (isInferableError(error)) {
      // Handle typesafe errors here
    }
  }
}))

mutation.mutate({ name: 'Earth' })

if (mutation.error.value && isInferableError(mutation.error.value)) {
  // Handle the typesafe errors here
}

Released under the MIT License.