Skip to content

Commit

Permalink
Add live query support to the imperative operation runners
Browse files Browse the repository at this point in the history
#239 added support for live queries to the react hooks. React provides a great model for ... react-ing... to changes in the live query where the hooks can trigger a re-render. #239 did not add support for the imperative style of api calls to go live because it isn't immediately clear how they should work. This adds the inner support we need for calling imperative live queries.

I chose to use async iterators for this api. The end result would look like:

```
for await (const result of api.widget.findOne("123")) {
  console.log(result);
}
```

`result` there would be a fully formed `GadgetRecord` instance, and each time it changed on the backend, we'd push a whole new instance out the async iterator.

[no-changelog-required]
  • Loading branch information
airhorns committed Oct 17, 2023
1 parent aec662c commit 20e988a
Show file tree
Hide file tree
Showing 5 changed files with 774 additions and 351 deletions.
1 change: 1 addition & 0 deletions packages/api-client-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"klona": "^2.0.6",
"tiny-graphql-query-compiler": "^0.2.2",
"tslib": "^2.6.2",
"wonka": "^6.3.2",
"ws": "^8.13.0"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions packages/api-client-core/spec/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ export const base64 = (str: string) => Buffer.from(str).toString("base64");
export function expectValidGraphQLQuery(query: string) {
parse(query);
}

export const asyncIterableToIterator = <T>(iterator: AsyncIterable<T>) => {
return iterator[Symbol.asyncIterator]();
};
Loading

0 comments on commit 20e988a

Please sign in to comment.