Skip to content

Commit

Permalink
update docs to reflect optional model api identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
jasong689 committed Jun 1, 2023
1 parent 2c72e5b commit 715664a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
10 changes: 3 additions & 7 deletions packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,7 @@ export const CreatePost = () => {
// run the action when the button is clicked
// the action runner function accepts action inputs in the same format as api.blogPost.create, and the GraphQL API
createBlogPost({
blogPost: {
title: "New post created from React",
},
title: "New post created from React",
});
}}
>
Expand Down Expand Up @@ -549,9 +547,7 @@ export const UpdatePost = (props: { id: string }) => {
// pass the id of the blog post we're updating as one parameter, and the new post attributes as another
updateBlogPost({
id: props.id,
post: {
title,
},
title,
});
}}
>
Expand All @@ -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:
- `globalActionFunction`: The action function from your application's API client. Gadget generates these global action functions for each global action defined in your Gadget backend. Required. Example: `api.runSync`, or `api.purgeData` (corresponding to Global Actions named `Run Sync` or `Purge Data`).
- `options`: Options for making the call to the backend. Not required and all keys are optional.
Expand Down
10 changes: 5 additions & 5 deletions packages/react/src/useAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ import { ErrorWrapper, noProviderErrorMessage } from "./utils";
* });
*
* const onClick = () => createUser({
* user: {
* name: props.name,
* email: props.email,
* }
* name: props.name,
* email: props.email,
* });
*
* return (
Expand Down Expand Up @@ -86,6 +84,8 @@ export const useAction = <
}

let newVariables: Exclude<F["variablesType"], null | undefined>;
const idVariable = Object.entries(action.variables).find(([key, value]) => key === "id" && value.type === "GadgetID");

if (action.hasCreateOrUpdateEffect) {
if (
action.modelApiIdentifier in variables &&
Expand All @@ -101,7 +101,7 @@ export const useAction = <
if (action.paramOnlyVariables?.includes(key)) {
newVariables[key] = value;
} else {
if (key === "id") {
if (idVariable && key === idVariable[0]) {
newVariables.id = value;
} else {
newVariables[action.modelApiIdentifier][key] = value;
Expand Down

0 comments on commit 715664a

Please sign in to comment.