-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
opa-react: introduce batching (#123)
This change introduces **batching** as an option for `@styra/opa-react`. It is enabled by passing `batch={true}` to `<AuthzProvider>` and will cause multiple calls to `useAuthz` or usages of `<Authz>` to be combined into groups of batch requests. It is only applicable when using Enterprise OPA (which has a [Batch API](https://docs.styra.com/enterprise-opa/reference/api-reference/batch-api)), or when implementing a compatible API yourself. Notes: 1. `fromResult` is exempt from caching (same as with the non-batching cache feature): `{path: "foo", input: {user: "alice"}, fromResult: (x) => x.foo}` and `{path: "foo", input: {user: "alice"}, fromResult: (x) => x.bar}` **yield the same result** (which one is undefined), so this should **be avoided**. 2. The batch feature doesn't support query cancellation via `signal` (`AbortController`), the non-batching querier does. 3. Some props fields have been removed from `AuthzContext`, since only a subset of the `AuthzProviderProps` are useful from the hooks etc. It's a general cleanup: you can **set** `retry` and `batch` on `AuthzProvider`, but those will be used to create the `queryClient` that's employed in `useAuthz`, so only `queryClient` (and not `retry` and `batch`) is retrievable from `AuthzContext`.
- Loading branch information
Showing
9 changed files
with
407 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
"@styra/opa-react": minor | ||
--- | ||
|
||
Support batching requests sent to the backend (optional) | ||
|
||
When used with [Enterprise OPA's Batch API](https://docs.styra.com/enterprise-opa/reference/api-reference/batch-api), this mode allows for sending much | ||
fewer requests to the backend. It's enabled by setting `batch={true}` on `<AuthzProvider>`. | ||
|
||
Note that the Batch API has no notion of "default query", so it's not possible | ||
to use batching without having either `defaultPath` (`<AuthzProvider>`) or | ||
`path` (`useAuthz()`, `<Authz>`) set. | ||
|
||
Please note that `fromResult` is exempt from the cache key, so multiple requests | ||
with the same path and input, but different `fromResult` settings will lead to | ||
unforeseen results. | ||
This is on par with the regular (non-batching) caching, and we'll revisit this | ||
if it becomes a problem for users. Please create an issue on Github if it is | ||
problematic for you. | ||
|
||
Furthermore, batching queries are not wired up with `AbortController` like the | ||
non-batching equivalents are. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.