reactive/data-client / .agents/skills/data-client-vue
data-client-vue
UnverifiedRead, change and render async and shared state in Vue with @data-client/vue: any data from an API or other async source, and global state shared across components (it has to persist somewhere, which makes it async). Use it for loading and error states, mutations and forms, filtered or derived views, and debugging data that looks wrong or stale on screen. Use it in place of fetch in onMounted or watch, or app-wide refs and provide/inject; its main composables are useSuspense, useQuery and useController.
references/useQuery.md
View on GitHub<!-- Generated by `yarn build:skills` from docs/core/api/useQuery.md (vue). Edit the source doc, not this file. -->
# useQuery()
Data rendering without the fetch.
Access any [Queryable Schema](https://dataclient.io/rest/api/schema#queryable)'s store value; like [Entity](https://dataclient.io/rest/api/Entity), [Object](https://dataclient.io/rest/api/Object), [All](https://dataclient.io/rest/api/All), [Collection](https://dataclient.io/rest/api/Collection), [Query](https://dataclient.io/rest/api/Query),
[Union](https://dataclient.io/rest/api/Union), and [Scalar](https://dataclient.io/rest/api/Scalar). [Lazy](https://dataclient.io/rest/api/Lazy) fields also work via their [`.query`](https://dataclient.io/rest/api/Lazy#query) accessor.
If the value does not exist, returns `undefined`.
`useQuery()` is reactive to data [mutations](./mutations.md); rerendering only when necessary. Returns `undefined`
when data is [Invalid](https://dataclient.io/vue/concepts/expiry-policy#invalid), or when `null` is the second argument.
> **Tip**
>
> [Queries](https://dataclient.io/rest/api/Query) are a great companion to efficiently render aggregate computations like those that use [groupBy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy#browser_compatibility),
> [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map), [reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce), and [filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter).
## Usage
```ts title="Post"
import { Entity, EntityMixin } from '@data-client/rest';
export class Post extends Entity {
id = 0;
author = { id: 0 };
title = '';
body = '';
votes = 0;
static key = 'Post';
static schema = {
author: EntityMixin(
class User {
id = 0;
},
),
};
}
```
```ts title="PostResource" {15-22}
import { resource } from '@data-client/rest';
import { Post } from './Post';
export { Post };
export const PostResource = resource({
path: '/posts/:id',
searchParams: {} as { userId?: string | number } | undefined,
schema: Post,
}).extend('vote', {
path: '/posts/:id/vote',
method: 'POST',
body: undefined,
schema: Post,
getOptimisticResponse(snapshot, { id }) {
const post = snapshot.get(Post, { id });
if (!post) throw snapshot.abort;
return {
id,
votes: post.votes + 1,
};
},
});
```
```html title="PostItem.vue" {10}
<script setup lang="ts">
import { useController } from '@data-client/vue';
import NumberFlow from '@number-flow/vue';
import { PostResource, type Post } from './PostResource';
const props = defineProps<{ post: Post }>();
const ctrl = useController();
const handleVote = () => {
ctrl.fetch(PostResource.vote, { id: props.post.id });
};
</script>
<template>
<article class="listItem">
<button
class="upvote"
@click="handleVote"
:aria-label="`Upvote, ${post.votes} votes`"
>
<NumberFlow :value="post.votes" />
</button>
<div>
<h4>{{ post.title }}</h4>
<p class="meta">{{ post.body }}</p>
</div>
</article>
</template>
```
```html title="TotalVotes.vue" {13}
<script setup lang="ts">
import { Query } from '@data-client/rest';
import { useQuery } from '@data-client/vue';
import NumberFlow from '@number-flow/vue';
import { PostResource } from './PostResource';
const queryTotalVotes = new Query(
PostResource.getList.schema,
posts => posts.reduce((total, post) => total + post.votes, 0),
);
const props = defineProps<{ userId: number }>();
const totalVotes = useQuery(queryTotalVotes, () => ({ userId: props.userId }));
</script>
<template>
<span v-if="totalVotes !== undefined" class="meta">
<NumberFlow :value="totalVotes" /> votes
</span>
</template>
```
```html title="PostList.vue"
<script setup lang="ts">
import { useSuspense } from '@data-client/vue';
import { PostResource } from './PostResource';
import PostItem from './PostItem.vue';
import TotalVotes from './TotalVotes.vue';
const userId = 2;
const posts = await useSuspense(PostResource.getList, { userId });
</script>
<template>
<header class="appHeader">
<h1 class="appTitle">Posts</h1>
<TotalVotes :userId="userId" />
</header>
<div class="list">
<PostItem v-for="post in posts" :key="post.pk()" :post="post" />
</div>
</template>
```
See [truthiness narrowing](https://www.typescriptlang.org/docs/handbook/2/narrowing.html#truthiness-narrowing) for
more information about type handling
> **Tip: Conditional Dependencies**
>
> Use `null` as the second argument to any Data Client hook means "do nothing."
> A [RestEndpoint](https://dataclient.io/rest/api/RestEndpoint) without path or search params takes `{}` in its place, like `enabled ? {} : null`.
>
> ```typescript
> // todo could be undefined if id is undefined
> const todo = useQuery(
> Todo,
> computed(() => (id.value ? { id: id.value } : null)),
> );
> ```
## Types
```typescript
function useQuery<S extends Queryable>(
schema: S,
...args: MaybeRefsOrGetters<SchemaArgs<S>> | [null]
): ComputedRef<DenormalizeNullable<S> | undefined>;
```
Arguments can be plain values, [refs](https://vuejs.org/api/reactivity-core.html#ref) (including [computed](https://vuejs.org/api/reactivity-core.html#computed)), or getter
functions like `() => ({ id: props.id })`. A plain object like `{ id: props.id }` is read once and won't
follow prop or route changes, so use a getter or `computed` when an argument can change.
The result updates when the arguments change.
### Queryable
[Queryable](https://dataclient.io/rest/api/schema#queryable) schemas require an `queryKey()` method that returns something. These include
[Entity](https://dataclient.io/rest/api/Entity), [Object](https://dataclient.io/rest/api/Object), [All](https://dataclient.io/rest/api/All), [Collection](https://dataclient.io/rest/api/Collection), [Query](https://dataclient.io/rest/api/Query),
[Union](https://dataclient.io/rest/api/Union), and [Scalar](https://dataclient.io/rest/api/Scalar). [Lazy](https://dataclient.io/rest/api/Lazy) fields produce a Queryable via their [`.query`](https://dataclient.io/rest/api/Lazy#query) accessor.
```ts
interface Queryable {
queryKey(
args: readonly any[],
unvisit: (schema: any, args: readonly any[]) => any,
delegate: IQueryDelegate,
// Must be non-void
): {};
}
```
## Examples
### Sorting & Filtering
[Query](https://dataclient.io/rest/api/Query) sorts and filters what is already in the store, so picking another column
re-sorts the table without fetching again.
```ts title="resources/User"
import { Entity, resource } from '@data-client/rest';
export class User extends Entity {
id = '';
name = '';
isAdmin = false;
posts = 0;
joined = 0;
static key = 'User';
}
export const UserResource = resource({
path: '/users/:id',
schema: User,
});
/** A users list's order: by one column, reversed when `desc` */
export type SortBy = 'name' | 'posts' | 'joined';
export interface Sort {
sortBy: SortBy;
desc: boolean;
}
```
```html title="RoleFilter.vue"
<script setup lang="ts">
const roles = { All: undefined, Admins: true, Members: false };
defineProps<{ isAdmin: boolean | undefined }>();
const emit = defineEmits<{ change: [isAdmin: boolean | undefined] }>();
</script>
<template>
<div class="segmented" role="group" aria-label="Role">
<button
v-for="(value, label) in roles"
:key="label"
:aria-pressed="isAdmin === value"
@click="emit('change', value)"
>
{{ label }}
</button>
</div>
</template>
```
```html title="UsersTableView.vue"
<script setup lang="ts">
import { type Sort, type SortBy, type User } from './resources/User';
const props = defineProps<{ users: readonly User[]; sort: Sort }>();
const emit = defineEmits<{ sort: [sort: Sort] }>();
function ariaSort(column: SortBy) {
if (props.sort.sortBy !== column) return undefined;
return props.sort.desc ? 'descending' : 'ascending';
}
// a second click on the sorted column reverses it
function toggleSort(column: SortBy) {
const desc = props.sort.sortBy === column && !props.sort.desc;
emit('sort', { sortBy: column, desc });
}
</script>
<template>
<table>
<thead>
<tr>
<th :aria-sort="ariaSort('name')">
<button class="sort" @click="toggleSort('name')">Name</button>
</th>
<th class="value" :aria-sort="ariaSort('posts')">
<button class="sort" @click="toggleSort('posts')">Posts</button>
</th>
<th class="value" :aria-sort="ariaSort('joined')">
<button class="sort" @click="toggleSort('joined')">Joined</button>
</th>
</tr>
</thead>
<tbody>
<tr v-for="user in users" :key="user.pk()">
<td>
{{ user.name }}
<span v-if="user.isAdmin" class="pill">Admin</span>
</td>
<td class="value">{{ user.posts }}</td>
<td class="value">{{ user.joined }}</td>
</tr>
</tbody>
</table>
</template>
```
```html title="UsersTable.vue" {34-37}
<script lang="ts">
import { All, Query } from '@data-client/rest';
import { type Sort, User } from './resources/User';
interface Args extends Sort {
/** admins only (true), members only (false), or everyone */
isAdmin: boolean | undefined;
}
const sortedUsers = new Query(
new All(User),
(entries, { sortBy, desc, isAdmin }: Args) => {
const users = entries
.filter(user => isAdmin === undefined || user.isAdmin === isAdmin)
.sort((a, b) =>
sortBy === 'name' ?
a.name.localeCompare(b.name)
: a[sortBy] - b[sortBy],
);
return desc ? users.reverse() : users;
},
);
</script>
<script setup lang="ts">
import { useQuery, useSuspense } from '@data-client/vue';
import { UserResource } from './resources/User';
import UsersTableView from './UsersTableView.vue';
const props = defineProps<{ sort: Sort; isAdmin: boolean | undefined }>();
defineEmits<{ sort: [sort: Sort] }>();
await useSuspense(UserResource.getList);
const users = useQuery(sortedUsers, () => ({
...props.sort,
isAdmin: props.isAdmin,
}));
</script>
<template>
<UsersTableView :users="users ?? []" :sort="sort" @sort="$emit('sort', $event)" />
</template>
```
```html title="UsersPage.vue"
<script setup lang="ts">
import { ref } from 'vue';
import RoleFilter from './RoleFilter.vue';
import { type Sort } from './resources/User';
import UsersTable from './UsersTable.vue';
const sort = ref<Sort>({ sortBy: 'name', desc: false });
const isAdmin = ref<boolean>();
</script>
<template>
<RoleFilter :is-admin="isAdmin" @change="isAdmin = $event" />
<UsersTable :sort="sort" :is-admin="isAdmin" @sort="sort = $event" />
</template>
```
### Lazy relationships
[Lazy](https://dataclient.io/rest/api/Lazy) fields keep raw IDs during parent denormalization. Use [`.query`](https://dataclient.io/rest/api/Lazy#query) with `useQuery` to resolve them on demand,
isolating re-renders to only the components that need the related data.
```ts title="Resources"
import { Entity, Lazy, resource } from '@data-client/rest';
export class Building extends Entity {
id = '';
name = '';
static key = 'Building';
}
export class Department extends Entity {
id = '';
name = '';
buildings: string[] = [];
static schema = {
buildings: new Lazy([Building]),
};
static key = 'Department';
}
export const DepartmentResource = resource({
path: '/departments/:id',
schema: Department,
});
```
```html title="BuildingList.vue" {8-11}
<script setup lang="ts">
import { computed } from 'vue';
import { useQuery } from '@data-client/vue';
import { Department } from './Resources';
const props = defineProps<{ dept: Department }>();
const buildings = useQuery(
Department.schema.buildings.query,
computed(() => props.dept.buildings),
);
</script>
<template>
<span v-if="buildings">{{ buildings.map(b => b.name).join(', ') }}</span>
</template>
```
```html title="DepartmentsPage.vue"
<script setup lang="ts">
import { All } from '@data-client/rest';
import { useQuery, useFetch } from '@data-client/vue';
import { DepartmentResource, Department } from './Resources';
import BuildingList from './BuildingList.vue';
useFetch(DepartmentResource.getList);
const departments = useQuery(new All(Department));
</script>
<template>
<Loading v-if="!departments" />
<div v-else>
<div v-for="dept in departments" :key="dept.pk()">
<strong>{{ dept.name }}</strong>: <BuildingList :dept="dept" />
</div>
</div>
</template>
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation). | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK). | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |