From c26393176518550a16a2e71aea55a8379f30385e Mon Sep 17 00:00:00 2001 From: Nathaniel Tucker Date: Tue, 6 Aug 2024 20:39:48 +0200 Subject: [PATCH] docs: More query examples in README --- .changeset/brown-grapes-fry.md | 6 ++++++ README.md | 24 ++++++++++++++---------- packages/react/README.md | 24 ++++++++++++++---------- packages/rest/README.md | 8 ++++++++ 4 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 .changeset/brown-grapes-fry.md diff --git a/.changeset/brown-grapes-fry.md b/.changeset/brown-grapes-fry.md new file mode 100644 index 000000000000..3010a027332e --- /dev/null +++ b/.changeset/brown-grapes-fry.md @@ -0,0 +1,6 @@ +--- +'@data-client/react': patch +'@data-client/rest': patch +--- + +Update README diff --git a/README.md b/README.md index 04a709fb7645..74cd8a1f8920 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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(); diff --git a/packages/react/README.md b/packages/react/README.md index 7452ba26677b..e21d8732810d 100644 --- a/packages/react/README.md +++ b/packages/react/README.md @@ -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 @@ -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(); diff --git a/packages/rest/README.md b/packages/rest/README.md index 1d1d3e66d5e4..6799229b6177 100644 --- a/packages/rest/README.md +++ b/packages/rest/README.md @@ -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