Skip to content

Commit

Permalink
docs: More query examples in README
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Aug 6, 2024
1 parent 909594a commit c263931
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
6 changes: 6 additions & 0 deletions .changeset/brown-grapes-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@data-client/react': patch
'@data-client/rest': patch
---

Update README
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,24 @@ ctrl.fetch(ArticleResource.update, { id }, articleData);

### [Programmatic queries](https://dataclient.io/rest/api/Query)

```tsx
```typescript
const queryTotalVotes = new schema.Query(
new schema.All(Post),
(posts, { userId } = {}) => {
if (userId !== undefined)
posts = posts.filter(post => post.userId === userId);
return posts.reduce((total, post) => total + post.votes, 0);
},
new schema.Collection([BlogPost]),
posts => posts.reduce((total, post) => total + post.votes, 0),
);

const totalVotes = useQuery(queryTotalVotes);
const totalVotesForUser = useQuery(queryTotalVotes, { userId });
```

```typescript
const groupTodoByUser = new schema.Query(
TodoResource.getList.schema,
todos => Object.groupBy(todos, todo => todo.userId),
);
const todosByUser = useQuery(groupTodoByUser);
```

### [Powerful Middlewares](https://dataclient.io/docs/concepts/managers)

```ts
Expand All @@ -179,16 +183,16 @@ class TickerStream implements Manager {
middleware: Middleware = controller => {
this.handleMsg = msg => {
controller.set(Ticker, { id: msg.id }, msg);
}
};
return next => action => next(action);
}
};

init() {
this.websocket = new WebSocket('wss://ws-feed.myexchange.com');
this.websocket.onmessage = event => {
const msg = JSON.parse(event.data);
this.handleMsg(msg);
}
};
}
cleanup() {
this.websocket.close();
Expand Down
24 changes: 14 additions & 10 deletions packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,24 @@ ctrl.fetch(ArticleResource.update, { id }, articleData);

### [Programmatic queries](https://dataclient.io/rest/api/Query)

```tsx
```typescript
const queryTotalVotes = new schema.Query(
new schema.All(Post),
(posts, { userId } = {}) => {
if (userId !== undefined)
posts = posts.filter(post => post.userId === userId);
return posts.reduce((total, post) => total + post.votes, 0);
},
new schema.Collection([BlogPost]),
posts => posts.reduce((total, post) => total + post.votes, 0),
);

const totalVotes = useQuery(queryTotalVotes);
const totalVotesForUser = useQuery(queryTotalVotes, { userId });
```

```typescript
const groupTodoByUser = new schema.Query(
TodoResource.getList.schema,
todos => Object.groupBy(todos, todo => todo.userId),
);
const todosByUser = useQuery(groupTodoByUser);
```

### [Powerful Middlewares](https://dataclient.io/docs/concepts/managers)

```ts
Expand All @@ -174,16 +178,16 @@ class TickerStream implements Manager {
middleware: Middleware = controller => {
this.handleMsg = msg => {
controller.set(Ticker, { id: msg.id }, msg);
}
};
return next => action => next(action);
}
};

init() {
this.websocket = new WebSocket('wss://ws-feed.myexchange.com');
this.websocket.onmessage = event => {
const msg = JSON.parse(event.data);
this.handleMsg(msg);
}
};
}
cleanup() {
this.websocket.close();
Expand Down
8 changes: 8 additions & 0 deletions packages/rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ const allRemainingTodos = useQuery(queryRemainingTodos);
const firstUserRemainingTodos = useQuery(queryRemainingTodos, { userId: 1 });
```

```typescript
const groupTodoByUser = new schema.Query(
TodoResource.getList.schema,
todos => Object.groupBy(todos, todo => todo.userId),
);
const todosByUser = useQuery(groupTodoByUser);
```

### TypeScript requirements

TypeScript is optional, but will only work with 4.0 or above. 4.1 is needed for stronger types as it
Expand Down

0 comments on commit c263931

Please sign in to comment.