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

fix(ai): update useAIGeneration to add graphqlErrors before return #5816

Merged
merged 4 commits into from
Oct 11, 2024

Conversation

thaddmt
Copy link
Contributor

@thaddmt thaddmt commented Sep 19, 2024

Description of changes

  • Danny noticed an error where simple string type returns from the client were returning malformed objects
  • This is because we were assuming all return types from the generate client would be spreadable objects
  • Updated called to useDataState to just return the data and massage the return type to add graphqlErrors after to avoid having to spread the return data

Issue #, if available

Description of how you validated changes

Checklist

  • Have read the Pull Request Guidelines
  • PR description included
  • yarn test passes and tests are updated/added
  • PR title and commit messages follow conventional commit syntax
  • If this change should result in a version bump, changeset added (This can be done after creating the PR.) This does not apply to changes made to docs, e2e, examples, or other private packages.

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

@thaddmt thaddmt requested a review from a team as a code owner September 19, 2024 23:23
Copy link

changeset-bot bot commented Sep 19, 2024

🦋 Changeset detected

Latest commit: dfe72eb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@aws-amplify/ui-react-ai Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

dbanksdesign
dbanksdesign previously approved these changes Sep 23, 2024
Copy link
Member

@calebpollman calebpollman left a comment

Choose a reason for hiding this comment

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

The issue here reads as misuse of useDataState and not necessarily an issue with useDataState itself. Adding graphqlErrors to the DataState output itself can be done by massaging the output of useDataState before returning from useAIGeneration

@thaddmt thaddmt changed the title fix(ai): update useAIGeneration to manage its own date state fix(ai): update useAIGeneration to add graphqlErrors before return Sep 24, 2024
@thaddmt
Copy link
Contributor Author

thaddmt commented Sep 24, 2024

The issue here reads as misuse of useDataState and not necessarily an issue with useDataState itself. Adding graphqlErrors to the DataState output itself can be done by massaging the output of useDataState before returning from useAIGeneration

Ended up going with your solution, great call out, thanks for catching

dbanksdesign
dbanksdesign previously approved these changes Sep 24, 2024
Comment on lines 76 to 88
const { data, errors } = result as SingularReturnValue<
Schema[Key]['returnType']
>;
setGraphqlErrors(errors);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return data;
};

return useDataState(updateAIGenerationStateAction, {});
const [data, handler] = useDataState(
updateAIGenerationStateAction,
undefined
);
return [{ ...data, graphqlErrors }, handler];
Copy link
Member

Choose a reason for hiding this comment

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

Can simplify a bit and remove the graphqlErrors state by returning result directly from updateAIGenerationStateAction and restructuring the hook return output, something like:

    const { data, errors } =
      (result?.data as SingularReturnValue<Schema[Key]['returnType']>) ?? {};

    const output: [
      state: GenerateState<Schema[Key]['returnType']>,
      handleAction: (input: Schema[Key]['args']) => void,
    ] = [{ ...result, data, graphqlErrors: errors }, handler];

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah damn this is much cleaner, let me try that and ping you :)

@dbanksdesign dbanksdesign merged commit 5af986f into main Oct 11, 2024
34 checks passed
@dbanksdesign dbanksdesign deleted the ai-generate-types branch October 11, 2024 17:08
@github-actions github-actions bot mentioned this pull request Oct 9, 2024
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