aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.81k stars 821 forks source link

GraphQL transformer V2 @index not creating queries #9083

Closed hamedhemmati closed 2 years ago

hamedhemmati commented 2 years ago

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

GraphQL API

Amplify Categories

No response

Environment information

``` # Put output below this line System: OS: macOS 12.0.1 CPU: (8) x64 Apple M1 Memory: 20.71 MB / 16.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 16.5.0 - /usr/local/bin/node Yarn: 1.22.10 - /usr/local/bin/yarn npm: 7.19.1 - /usr/local/bin/npm Watchman: 2021.06.07.00 - /usr/local/bin/watchman Browsers: Chrome: 96.0.4664.55 Edge: 95.0.1020.44 Safari: 15.1 npmPackages: @fontsource/open-sans: ^4.5.2 => 4.5.2 @loadable/component: ^5.15.0 => 5.15.0 aws-amplify: ^4.3.8 => 4.3.8 aws-amplify-react: ^5.1.9 => 5.1.9 bundle-optimisations: 1.0.0 dev-404-page: 1.0.0 functions: 1.0.0 gatsby: ^4.2.0 => 4.2.0 gatsby-plugin-google-analytics: ^4.2.0 => 4.2.0 gatsby-plugin-image: ^2.2.0 => 2.2.0 gatsby-plugin-less: ^6.2.0 => 6.2.0 gatsby-plugin-manifest: ^4.2.0 => 4.2.0 gatsby-plugin-react-helmet: ^5.2.0 => 5.2.0 gatsby-plugin-sharp: ^4.2.0 => 4.2.0 gatsby-plugin-sitemap: ^5.2.0 => 5.2.0 gatsby-source-filesystem: ^4.2.0 => 4.2.0 gatsby-transformer-sharp: ^4.2.0 => 4.2.0 internal-data-bridge: 1.0.0 load-babel-config: 1.0.0 prod-404-500: 1.0.0 react: ^17.0.1 => 17.0.2 react-dom: ^17.0.1 => 17.0.2 react-flow-renderer: ^9.6.11 => 9.6.11 react-helmet: ^6.1.0 => 6.1.0 react-icons: ^4.3.1 => 4.3.1 react-transition-group: ^4.4.2 => 4.4.2 react-transition-group/CSSTransition: undefined () react-transition-group/ReplaceTransition: undefined () react-transition-group/SwitchTransition: undefined () react-transition-group/Transition: undefined () react-transition-group/TransitionGroup: undefined () react-transition-group/TransitionGroupContext: undefined () react-transition-group/config: undefined () rsuite: ^5.2.2 => 5.2.2 rsuite/Affix: undefined () rsuite/Animation: undefined () rsuite/AutoComplete: undefined () rsuite/Avatar: undefined () rsuite/Badge: undefined () rsuite/Breadcrumb: undefined () rsuite/Button: undefined () rsuite/ButtonGroup: undefined () rsuite/ButtonToolbar: undefined () rsuite/Calendar: undefined () rsuite/Carousel: undefined () rsuite/Cascader: undefined () rsuite/CheckPicker: undefined () rsuite/CheckTree: undefined () rsuite/CheckTreePicker: undefined () rsuite/Checkbox: undefined () rsuite/CheckboxGroup: undefined () rsuite/CloseButton: undefined () rsuite/Col: undefined () rsuite/Container: undefined () rsuite/Content: undefined () rsuite/CustomProvider: undefined () rsuite/DOMHelper: undefined () rsuite/DatePicker: undefined () rsuite/DateRangePicker: undefined () rsuite/Disclosure: undefined () rsuite/Divider: undefined () rsuite/Drawer: undefined () rsuite/Dropdown: undefined () rsuite/FlexboxGrid: undefined () rsuite/Footer: undefined () rsuite/Form: undefined () rsuite/FormControl: undefined () rsuite/FormControlLabel: undefined () rsuite/FormErrorMessage: undefined () rsuite/FormGroup: undefined () rsuite/FormHelpText: undefined () rsuite/Grid: undefined () rsuite/Header: undefined () rsuite/IconButton: undefined () rsuite/Input: undefined () rsuite/InputGroup: undefined () rsuite/InputNumber: undefined () rsuite/InputPicker: undefined () rsuite/List: undefined () rsuite/Loader: undefined () rsuite/MaskedInput: undefined () rsuite/Menu: undefined () rsuite/Message: undefined () rsuite/Modal: undefined () rsuite/MultiCascader: undefined () rsuite/Nav: undefined () rsuite/Navbar: undefined () rsuite/Notification: undefined () rsuite/Overlay: undefined () rsuite/Pagination: undefined () rsuite/Panel: undefined () rsuite/PanelGroup: undefined () rsuite/Picker: undefined () rsuite/Placeholder: undefined () rsuite/Plaintext: undefined () rsuite/Popover: undefined () rsuite/Progress: undefined () rsuite/Radio: undefined () rsuite/RadioGroup: undefined () rsuite/RangeSlider: undefined () rsuite/Rate: undefined () rsuite/Ripple: undefined () rsuite/Row: undefined () rsuite/SafeAnchor: undefined () rsuite/Schema: undefined () rsuite/SelectPicker: undefined () rsuite/Sidebar: undefined () rsuite/Sidenav: undefined () rsuite/Slider: undefined () rsuite/Steps: undefined () rsuite/Table: undefined () rsuite/Tag: undefined () rsuite/TagGroup: undefined () rsuite/TagInput: undefined () rsuite/TagPicker: undefined () rsuite/Timeline: undefined () rsuite/Toggle: undefined () rsuite/Tooltip: undefined () rsuite/Tree: undefined () rsuite/TreePicker: undefined () rsuite/Uploader: undefined () rsuite/Whisper: undefined () rsuite/locales: undefined () rsuite/locales/ar_EG: undefined () rsuite/locales/da_DK: undefined () rsuite/locales/de_DE: undefined () rsuite/locales/default: undefined () rsuite/locales/en_GB: undefined () rsuite/locales/en_US: undefined () rsuite/locales/es_AR: undefined () rsuite/locales/es_ES: undefined () rsuite/locales/fa_IR: undefined () rsuite/locales/fi_FI: undefined () rsuite/locales/it_IT: undefined () rsuite/locales/ko_KR: undefined () rsuite/locales/pt_BR: undefined () rsuite/locales/ru_RU: undefined () rsuite/locales/sv_SE: undefined () rsuite/locales/zh_CN: undefined () rsuite/locales/zh_TW: undefined () rsuite/toaster: undefined () rsuite/utils: undefined () webpack-theme-component-shadowing: 1.0.0 npmGlobalPackages: @aws-amplify/cli: 7.5.2 create-next-app: 10.1.3 firebase-tools: 9.4.0 gatsby-cli: 4.2.0 npm: 7.19.1 ```

Describe the bug

I have built a test environment to play with the new transformer V2. I am trying to get play with @index but I am not seeing any queries generated for it.

In the previous version, I use @key to get a list of users by email as follows. type User @model @auth(rules: [{ allow: owner, operations: [create, update, read, delete] }]) @key( name: "UsersByEmail" fields: ["email"] queryField: "UsersByEmail" ) { id: ID! email: String! identityId: String! }

in the new version I am using the new @index with the following schema

type User @model @auth(rules: [{ allow: owner, operations: [create, update, read, delete] }]) { id: ID! email: String! @index(name: "UsersByEmail") identityId: String! }

It seems to be taking it just fine but it's not generating a query for me to use. Nothing in the graphql folder or AppSync console.

Expected behavior

Expecting a new query added to the list of GraphQL queries

Reproduction steps

Create a new type User with @index on a required field amplify push --y

Code Snippet

// Put your code below this line.

Log output

``` // Put your logs below this line ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

cjihrig commented 2 years ago

@key( name: "UsersByEmail" fields: ["email"] queryField: "UsersByEmail" ) @index(name: "UsersByEmail")

Can you try adding the queryField argument to your @index directive as you did with @key in v1.

hamedhemmati commented 2 years ago

@key( name: "UsersByEmail" fields: ["email"] queryField: "UsersByEmail" ) @index(name: "UsersByEmail")

Can you try adding the queryField argument to your @index directive as you did with @key in v1.

This worked thanks. I think this should be added to the docs. I was under impression that the new @index only needs the name not both the name and the queryField.

hamedhemmati commented 2 years ago

Related to this the new @index cannot be removed once it has been created, unlike the old @key. When trying to remove the index you get an unhelpful "An error occurred when pushing the resources to the cloud". I had to delete the whole table and create it again to remove the index or move the index from one field to another. This used to be fine with @key in the V1.

cjihrig commented 2 years ago

I think this should be added to the docs.

Agreed. Thanks for pointing this out.

the new @Index cannot be removed once it has been created

Can you please provide more detailed steps to reproduce this. Based on your original post, I pushed the following schema:

type User
  @model
  @auth(
    rules: [{ allow: owner, operations: [create, update, read, delete] }]
  )
{
  id: ID!
  email: String! @index(name: "UsersByEmail", queryField: "UsersByEmail")
  identityId: String!
}

I then removed the @index and pushed again and everything succeeded.

alharris-at commented 2 years ago

A fix has been released in the latest appsync modelgen plugin, and should go out in the next CLI release.

hamedhemmati commented 2 years ago

Here's a quick video of removing the index

https://user-images.githubusercontent.com/2079826/144135900-57c88766-3146-4341-b546-eb8a0760596e.mov

.

cjihrig commented 2 years ago

@hamedhemmati there should be a better error message displayed, but based on your video, it looks like you removed the projectsByUser index from the Project model, but are still referencing that index from on line 7.

hamedhemmati commented 2 years ago

@hamedhemmati there should be a better error message displayed, but based on your video, it looks like you removed the projectsByUser index from the Project model, but are still referencing that index from on line 7.

Thanks for catching that. It's working now. I see that the documentation has been updated to include the queryField now so I am closing this.

burakkbilginn commented 2 years ago

@hamedhemmati Can you give the link of documentation where you look? I cannot find anything about that inside migration to v2 documentations in the whole web. I mean this: https://docs.amplify.aws/cli/migration/transformer-migration/#changes-that-amplify-cli-will-auto-migrate-for-you and this: https://docs.amplify.aws/cli-legacy/graphql-transformer/connection/

hamedhemmati commented 2 years ago

@hamedhemmati Can you give the link of documentation where you look? I cannot find anything about that inside migration to v2 documentations in the whole web. I mean this: https://docs.amplify.aws/cli/migration/transformer-migration/#changes-that-amplify-cli-will-auto-migrate-for-you and this: https://docs.amplify.aws/cli-legacy/graphql-transformer/connection/

I know the documentation is really confusing for sure. The last bullet point says you need to type the queryField if you want a query. The screenshot doesn't include it. So basically if you want to generate a query you have to pass something like.

@index(name: "byOderByAmount", queryField: "byOderByAmount, sortKeyFields: ["amount"]

image