feat(api-connection): tanstack query hooks for connecting react to fastapi #111
+4,971
−96
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reason for this change
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
andx-query
vendor extensions however.Similarly to tRPC, if an operation accepts a
cursor
parameter, we consider this a paginated operation and generate aninfiniteQueryOptions
method alongside thequeryOptions
method. If an operation is paginated but has a different pagination parameter name, this can be specified using thex-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
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