Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api-connection): tanstack query hooks for connecting react to fastapi #111

Merged
merged 5 commits into from
Mar 24, 2025

Conversation

cogwirrel
Copy link
Contributor

Reason for this change

  • No need for users to manage state manually
  • More consistent with tRPC

Description of changes

This change implements the open-api#ts-hooks generator, which provides factory methods for options to provide to tanstack query hooks, in a similar manner to tRPC.

This saves the need for users to manage state themselves, and provides parity with tRPC.

By default, PUT/PATCH/POST/DELETE http requests are considered "mutations" and all others "queries". This behaviour can be overridden with x-mutation and x-query vendor extensions however.

Similarly to tRPC, if an operation accepts a cursor parameter, we consider this a paginated operation and generate an infiniteQueryOptions method alongside the queryOptions method. If an operation is paginated but has a different pagination parameter name, this can be specified using the x-cursor vendor extension.

We support streaming operations in a similar manner to tRPC, where a streaming query hook will continually have its data updated as chunks are received. Since a single cache entry is shared by all pages for an infinite query hook, we cannot stream multiple pages at once for a "paginated streaming" operation as this risks conflicts/race-conditions. We therefore iterate through the whole stream for each page in this case.
For mutation operations which return a stream, we don't do anything special and just return the iterator for the user to use if required.

Description of how you validated changes

  • unit tests
  • walked through the dungeon adventure tutorial

Issue # (if applicable)

Fixes #96

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

Sorry, something went wrong.

@cogwirrel cogwirrel force-pushed the feat/fastapi-hooks branch 2 times, most recently from b6d0c56 to 38b466d Compare March 21, 2025 02:20
…stapi

This change implements the open-api#ts-hooks generator, which provides factory methods for options
to provide to tanstack query hooks, in a similar manner to tRPC.

This saves the need for users to manage state themselves, and provides parity with tRPC.

By default, PUT/PATCH/POST/DELETE http requests are considered "mutations" and all others "queries".
This behaviour can be overridden with `x-mutation` and `x-query` vendor extensions however.

Similarly to tRPC, if an operation accepts a `cursor` parameter, we consider this a paginated
operation and generate an `infiniteQueryOptions` method alongside the `queryOptions` method. If an
operation is paginated but has a different pagination parameter name, this can be specified using
the `x-cursor` vendor extension.

We support streaming operations in a similar manner to tRPC, where a streaming query hook will
continually have its data updated as chunks are received. Since a single cache entry is shared by
all pages for an infinite query hook, we cannot stream multiple pages at once for a "paginated
streaming" operation as this risks conflicts/race-conditions. We therefore iterate through the whole
stream for each page in this case.
For mutation operations which return a stream, we don't do anything special and just return the
iterator for the user to use if required.

Fixes #96
…pends on client generation

Previously only the compile target for the website had a dependency on the client generation, but
bundling doesn't rely on compilation and so it was just luck that meant the bundle included the
generated clients in the e2e tests
…rator tests

Since loading dependencies into the @ts-morph/bootstrap Project adds overhead, we refactor to allow
the project to be shared by multiple tests. This reduces the ts hooks generator test time from 27s
to 6s.
… instance

The useXXXApiClient hook would construct a new client every time, which meant that credentials
cached by the useSigV4 hook wouldn't be reused across different pages, leading to a slight
performance hit for using the vanilla client instead of the hooks. This change updates the
XXXApiProvider component to also set up a context with the api client.
@cogwirrel cogwirrel force-pushed the feat/fastapi-hooks branch from 20b53fb to 6fe69df Compare March 24, 2025 00:03
@cogwirrel cogwirrel merged commit 4a96d1a into main Mar 24, 2025
6 checks passed
@cogwirrel cogwirrel deleted the feat/fastapi-hooks branch March 24, 2025 00:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(api-connection): use tanstack query hooks for fastapi
2 participants