Skip to content

Commit c263931

Browse files
committed
docs: More query examples in README
1 parent 909594a commit c263931

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

.changeset/brown-grapes-fry.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@data-client/react': patch
3+
'@data-client/rest': patch
4+
---
5+
6+
Update README

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,24 @@ ctrl.fetch(ArticleResource.update, { id }, articleData);
146146

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

149-
```tsx
149+
```typescript
150150
const queryTotalVotes = new schema.Query(
151-
new schema.All(Post),
152-
(posts, { userId } = {}) => {
153-
if (userId !== undefined)
154-
posts = posts.filter(post => post.userId === userId);
155-
return posts.reduce((total, post) => total + post.votes, 0);
156-
},
151+
new schema.Collection([BlogPost]),
152+
posts => posts.reduce((total, post) => total + post.votes, 0),
157153
);
158154

159155
const totalVotes = useQuery(queryTotalVotes);
160156
const totalVotesForUser = useQuery(queryTotalVotes, { userId });
161157
```
162158

159+
```typescript
160+
const groupTodoByUser = new schema.Query(
161+
TodoResource.getList.schema,
162+
todos => Object.groupBy(todos, todo => todo.userId),
163+
);
164+
const todosByUser = useQuery(groupTodoByUser);
165+
```
166+
163167
### [Powerful Middlewares](https://dataclient.io/docs/concepts/managers)
164168

165169
```ts
@@ -179,16 +183,16 @@ class TickerStream implements Manager {
179183
middleware: Middleware = controller => {
180184
this.handleMsg = msg => {
181185
controller.set(Ticker, { id: msg.id }, msg);
182-
}
186+
};
183187
return next => action => next(action);
184-
}
188+
};
185189

186190
init() {
187191
this.websocket = new WebSocket('wss://ws-feed.myexchange.com');
188192
this.websocket.onmessage = event => {
189193
const msg = JSON.parse(event.data);
190194
this.handleMsg(msg);
191-
}
195+
};
192196
}
193197
cleanup() {
194198
this.websocket.close();

packages/react/README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,24 @@ ctrl.fetch(ArticleResource.update, { id }, articleData);
141141

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

144-
```tsx
144+
```typescript
145145
const queryTotalVotes = new schema.Query(
146-
new schema.All(Post),
147-
(posts, { userId } = {}) => {
148-
if (userId !== undefined)
149-
posts = posts.filter(post => post.userId === userId);
150-
return posts.reduce((total, post) => total + post.votes, 0);
151-
},
146+
new schema.Collection([BlogPost]),
147+
posts => posts.reduce((total, post) => total + post.votes, 0),
152148
);
153149

154150
const totalVotes = useQuery(queryTotalVotes);
155151
const totalVotesForUser = useQuery(queryTotalVotes, { userId });
156152
```
157153

154+
```typescript
155+
const groupTodoByUser = new schema.Query(
156+
TodoResource.getList.schema,
157+
todos => Object.groupBy(todos, todo => todo.userId),
158+
);
159+
const todosByUser = useQuery(groupTodoByUser);
160+
```
161+
158162
### [Powerful Middlewares](https://dataclient.io/docs/concepts/managers)
159163

160164
```ts
@@ -174,16 +178,16 @@ class TickerStream implements Manager {
174178
middleware: Middleware = controller => {
175179
this.handleMsg = msg => {
176180
controller.set(Ticker, { id: msg.id }, msg);
177-
}
181+
};
178182
return next => action => next(action);
179-
}
183+
};
180184

181185
init() {
182186
this.websocket = new WebSocket('wss://ws-feed.myexchange.com');
183187
this.websocket.onmessage = event => {
184188
const msg = JSON.parse(event.data);
185189
this.handleMsg(msg);
186-
}
190+
};
187191
}
188192
cleanup() {
189193
this.websocket.close();

packages/rest/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ const allRemainingTodos = useQuery(queryRemainingTodos);
130130
const firstUserRemainingTodos = useQuery(queryRemainingTodos, { userId: 1 });
131131
```
132132

133+
```typescript
134+
const groupTodoByUser = new schema.Query(
135+
TodoResource.getList.schema,
136+
todos => Object.groupBy(todos, todo => todo.userId),
137+
);
138+
const todosByUser = useQuery(groupTodoByUser);
139+
```
140+
133141
### TypeScript requirements
134142

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

0 commit comments

Comments
 (0)