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

support optional model api identifier in react hooks #201

Merged
merged 2 commits into from
Jun 2, 2023

Conversation

jasong689
Copy link
Contributor

@jasong689 jasong689 commented May 17, 2023

We need to update the react hooks to also allow optional model api identifiers. This follows the same logic that the internal and public model managers.

closes GGT-4093

PR Checklist

  • Important or complicated code is tested
  • Any user facing changes are documented in the Gadget-side changelog
  • Any immediate changes are slated for release in Gadget via a generated package dependency bump
  • Versions within this monorepo are matching and there's a valid upgrade path

@jasong689 jasong689 force-pushed the react-optional-api branch 2 times, most recently from 895dc40 to 842d75d Compare May 17, 2023 19:26
@@ -79,6 +79,37 @@ export const useAction = <
transformedResult,
useCallback(
async (variables, context) => {
if (action.hasAmbiguousIdentifier) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same logic that we generate in the API client

if (action.paramOnlyVariables?.includes(key)) {
newVariables[key] = value;
} else {
if (key === "id") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id is kind of special in that if it's an update effect then we have to include it as a separate variable

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Should we be checking if it's an update? Or can we look at action.variables to see if it should be nested under the model API identifier or not? I'm just wondering if we could future-proof against creates where the end user can supply an id, but it won't be "lifted" to a variable like we do for updates.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this up to check action.variables

@jasong689 jasong689 marked this pull request as ready for review May 23, 2023 22:46
Copy link
Contributor

@airhorns airhorns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM, can you update the readme and docstrings of the hooks to make sure the docs reflect the new reality too?

Copy link
Contributor

@thegedge thegedge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

} else {
newVariables = {
[action.modelApiIdentifier]: {},
} as Exclude<F["variablesType"], null | undefined>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to be casted? :(

Is it TypeScript not figuring it out, or do we have something typed incorrectly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get Type '{ [x: string]: {}; }' is not assignable to type 'Exclude<F["variablesType"], null | undefined>'.

if (action.paramOnlyVariables?.includes(key)) {
newVariables[key] = value;
} else {
if (key === "id") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Should we be checking if it's an update? Or can we look at action.variables to see if it should be nested under the model API identifier or not? I'm just wondering if we could future-proof against creates where the end user can supply an id, but it won't be "lifted" to a variable like we do for updates.

Base automatically changed from optional-api-identifier to main June 1, 2023 16:29
Copy link
Contributor

@airhorns airhorns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for me!

@@ -574,7 +570,7 @@ const [{ data, fetching, error }, _refetch] = useFindBy(api.blogPost.findBySlug,

### `useGlobalAction(actionFunction: GlobalActionFunction, options: UseGlobalActionOptions = {}): [{data, fetching, error}, refetch]`

`useGlobalAction` is a hook for running a backend Global Action. `useGlobalAction(api.widget.create)` is the React equivalent of `await api.someGlobalAction({...})`. `useGlobalAction` doesn't immediately dispatch a request to run an action server side, but instead returns a result object and a function which runs the action, similar to [`urql`'s `useMutation` hook](https://formidable.com/open-source/urql/docs/api/urql/#usemutation). `useGlobalAction` must be passed one of the global action functions from an instance of your application's generated API client. Options:
`useGlobalAction` is a hook for running a backend Global Action. `useGlobalAction(api.someGlobalAction)` is the React equivalent of `await api.someGlobalAction({...})`. `useGlobalAction` doesn't immediately dispatch a request to run an action server side, but instead returns a result object and a function which runs the action, similar to [`urql`'s `useMutation` hook](https://formidable.com/open-source/urql/docs/api/urql/#usemutation). `useGlobalAction` must be passed one of the global action functions from an instance of your application's generated API client. Options:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

@jasong689 jasong689 merged commit fb77ff2 into main Jun 2, 2023
@jasong689 jasong689 deleted the react-optional-api branch June 2, 2023 15:28
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.

3 participants