Skip to content

Commit

Permalink
docs: Use Collection schema for Query example
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Sep 9, 2024
1 parent 49ac1c4 commit 20eb933
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
12 changes: 4 additions & 8 deletions docs/core/shared/_VoteDemo.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,14 @@ interface Props {
}
```

```tsx title="TotalVotes" collapsed {15}
```tsx title="TotalVotes" collapsed {11}
import { schema } from '@data-client/rest';
import { useQuery } from '@data-client/react';
import { Post } from './PostResource';
import { PostResource } from './PostResource';

const queryTotalVotes = new schema.Query(
new schema.All(Post),
(posts, { userId } = {}) => {
if (userId !== undefined)
posts = posts.filter(post => post.author.id === userId);
return posts.reduce((total, post) => total + post.votes, 0);
},
PostResource.getList.schema,
posts => posts.reduce((total, post) => total + post.votes, 0),
);

export default function TotalVotes({ userId }: Props) {
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs/resources/TodoResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ export const TodoResource = placeholderResource({

export const queryRemainingTodos = new schema.Query(
TodoResource.getList.schema,
(entries) => entries.filter((todo) => !todo.completed).length,
entries => entries.filter(todo => !todo.completed).length,
);
1 change: 1 addition & 0 deletions examples/todo-app/typetest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-expressions */
import { useQuery, useController, useSuspense } from '@data-client/react';

import {
Expand Down
25 changes: 16 additions & 9 deletions packages/rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,16 @@ const userOneTodos = await TodoResource.getList({ userId: 1 });
const newTodo = await TodoResource.getList.push({ title: 'my todo' });

// POST https://jsonplaceholder.typicode.com/todos?userId=1
const newUserOneTodo = await TodoResource.getList.push({ userId: 1 }, { title: 'my todo' });
const newUserOneTodo = await TodoResource.getList.push(
{ userId: 1 },
{ title: 'my todo' },
);

// GET https://jsonplaceholder.typicode.com/todos?userId=1&page=2
const nextPageOfTodos = await TodoResource.getList.getPage({ userId: 1, page: 2 });
const nextPageOfTodos = await TodoResource.getList.getPage({
userId: 1,
page: 2,
});

// PUT https://jsonplaceholder.typicode.com/todos/5
todo5 = await TodoResource.update({ id: 5 }, { title: 'my todo' });
Expand Down Expand Up @@ -108,10 +114,11 @@ const todoList = useSuspense(TodoResource.getList);
```typescript
const ctrl = useController();
const updateTodo = data => ctrl.fetch(TodoResource.update, { id }, data);
const partialUpdateTodo= data =>
const partialUpdateTodo = data =>
ctrl.fetch(TodoResource.partialUpdate, { id }, data);
const addTodoToEnd = data => ctrl.fetch(TodoResource.getList.push, data);
const addTodoToBeginning = data => ctrl.fetch(TodoResource.getList.unshift, data);
const addTodoToBeginning = data =>
ctrl.fetch(TodoResource.getList.unshift, data);
const deleteTodo = data => ctrl.fetch(TodoResource.delete, { id });
```

Expand All @@ -120,7 +127,7 @@ const deleteTodo = data => ctrl.fetch(TodoResource.delete, { id });
```tsx
const queryRemainingTodos = new schema.Query(
TodoResource.getList.schema,
(entries) => entries.filter((todo) => !todo.completed).length,
entries => entries.filter(todo => !todo.completed).length,
);

const allRemainingTodos = useQuery(queryRemainingTodos);
Expand All @@ -145,12 +152,12 @@ supports inferring argument types from the path templates.
- [Backbone Model](https://backbonejs.org/#Model)
- [ImmutableJS Record](https://immutable-js.github.io/immutable-js/docs/#/Record)


## API

#### Networking definition
- [Endpoints](https://dataclient.io/rest/api/Endpoint): [RestEndpoint](https://dataclient.io/rest/api/RestEndpoint)
- [Resources](https://dataclient.io/docs/getting-started/resource): [resource()](https://dataclient.io/rest/api/resource), [hookifyResource()](https://dataclient.io/rest/api/hookifyResource)

- [Endpoints](https://dataclient.io/rest/api/Endpoint): [RestEndpoint](https://dataclient.io/rest/api/RestEndpoint)
- [Resources](https://dataclient.io/docs/getting-started/resource): [resource()](https://dataclient.io/rest/api/resource), [hookifyResource()](https://dataclient.io/rest/api/hookifyResource)

<table>
<caption>
Expand Down Expand Up @@ -229,4 +236,4 @@ supports inferring argument types from the path templates.
<td>memoized custom transforms</td>
<td align="center">✅</td>
</tr>
</tbody></table>
</tbody></table>

0 comments on commit 20eb933

Please sign in to comment.