Describe the bug
On my schema.graphql I added models with @key and @connection directives, after that I followed the instructions from the docs to update my api: amplify api gql-compile and then amplify push. Everything works fine, I have all the queries I need and I can call/use them correctly.
But when I go to the AWS Console and connect my branch and my backend, the CD runs and all my queries generated by @key and @connection are erased, then if try to call then I got an error telling me they are invalid:
"Validation error of type FieldUndefined: Field 'querySessionsByCreatedAt' in type 'Query' is undefined @ 'querySessionsByCreatedAt'"
I don't know if my schema has something wrong and why only works using amplify push but here are all my files:
schema.graphql, I have a @key to sort Sessions by createdAt and @connection to handle a many to many relation between Sessions and SessionsGroups.
type Todo @model
@auth(rules: [
{ allow: owner },
{ allow: groups, groups: ["admin"], operations: [read] }
])
{
id: ID!
title: String!
status: String
createdAt: String!
type: String!
}
type Session @model
@key(name: "byCreatedAt", fields: ["type", "createdAt"])
{
id: ID!
name: String!
link: String!
type: String!
createdAt: String!
sessionGroups: [SessionSessionGroup] @connection(keyName: "bySession", fields: ["id"])
}
type SessionGroup @model @auth(rules: [
{ allow: groups, groups: ["admin"] },
{ allow: groups, groupsField: "userGroups", operations: [read, update] }
])
{
id: ID!
name: String!
userGroups: [String]
sessions: [SessionSessionGroup] @connection(keyName: "bySessionGroup", fields: ["id"])
}
type SessionSessionGroup
@model(queries: null)
@key(name: "bySession", fields: ["sessionID", "sessionGroupID"])
@key(name: "bySessionGroup", fields: ["sessionGroupID", "sessionID"])
{
id: ID!
sessionID: ID!
sessionGroupID: ID!
session: Session! @connection(fields: ["sessionID"])
sessionGroup: SessionGroup! @connection(fields: ["sessionGroupID"])
}
Here is my amplify.yml
version: 0.1
backend:
phases:
build:
commands:
- '# Execute Amplify CLI with the helper script'
- amplifyPush --simple
frontend:
phases:
preBuild:
commands:
- npm install
- npm run test
build:
commands:
- npm run build
artifacts:
baseDirectory: dist
files:
- '**/*'
cache:
paths:
- node_modules/**/*
test:
phases:
preTest:
commands:
- npm ci
- npm install
- npm install wait-on
- 'npm start & npx wait-on http://localhost:8080'
test:
commands:
- 'npx cypress run'
artifacts:
baseDirectory: cypress
configFilePath: 'cypress.json'
files:
- '**/*.csv'
- '**/*.mp3'
Amplify CLI Version
4.21.2
To Reproduce
Create a project, add the user auth (default options), then I add API with the schema above, run amplify push, then connect the branch and the new backend
Expected behavior
Expected the deploy from CD has the same behavior from amplify push, so all the necessary queries should be there.
Desktop (please complete the following information):
Additional context
Things that I already tried:
querySessionSessionGroupsBySessionGroup, querySessionSessionGroupsBySession and querySessionsByCreatedAtSteps when I tried the create the API again:
⯠amplify add api
? Please select from one of the below mentioned services: GraphQL
? Provide API name: apiname
? Choose the default authorization type for the API Amazon Cognito User Pool
Use a Cognito user pool configured as a part of this project.
? Do you want to configure advanced settings for the GraphQL API No, I am done.
? Do you have an annotated GraphQL schema? No
? Do you want a guided schema creation? Yes
? What best describes your project: Single object with fields (e.g., âTodoâ with ID, name, description)
? Do you want to edit the schema now? Yes
Please edit the file in your editor: /path/to/my/projectamplify/backend/api/apiname/schema.graphql
? Press enter to continue
? Do you want to generate code for your newly created GraphQL API Yes
? Choose the code generation language target javascript
? Enter the file name pattern of graphql queries, mutations and subscriptions src/graphql/*/.js
? Do you want to generate/update all possible GraphQL operations - queries, mutations and subscriptions Yes
? Enter maximum statement depth [increase from default if your schema is deeply nested] 5
I realized that on my build the last version is installed, 4.22, then I updated my locally version and I got the same results locally, my queries were not generated. Is this a bug? Is there any config missing?
I received all these warnings while the last version was being installed:
$ npm install -g @aws-amplify/cli@latest
npm WARN deprecated [email protected]: Merge GraphQL Schemas has been deprecated and merged into GraphQL Tools, so it will no longer get updates. Use GraphQL Tools instead to stay up-to-date! Check out https://www.graphql-tools.com/docs/migration-from-merge-graphql-schemas for migration and https://the-guild.dev/blog/graphql-tools-v6 for new changes.
npm WARN deprecated @graphql-toolkit/[email protected]: GraphQL Toolkit is deprecated and merged into GraphQL Tools, so it will no longer get updates. Use GraphQL Tools instead to stay up-to-date! Check out https://www.graphql-tools.com/docs/migration-from-toolkit for migration and https://the-guild.dev/blog/graphql-tools-v6 for new changes.
npm WARN deprecated @graphql-toolkit/[email protected]: GraphQL Toolkit is deprecated and merged into GraphQL Tools, so it will no longer get updates. Use GraphQL Tools instead to stay up-to-date! Check out https://www.graphql-tools.com/docs/migration-from-toolkit for migration and https://the-guild.dev/blog/graphql-tools-v6 for new changes.
npm WARN deprecated @graphql-toolkit/[email protected]: GraphQL Toolkit is deprecated and merged into GraphQL Tools, so it will no longer get updates. Use GraphQL Tools instead to stay up-to-date! Check out https://www.graphql-tools.com/docs/migration-from-toolkit for migration and https://the-guild.dev/blog/graphql-tools-v6 for new changes.
npm WARN deprecated [email protected]: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated [email protected]: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated [email protected]: core-js@<3 is no longer maintained and not recommended for usage due to the number of issues. Please, upgrade your dependencies to the actual version of core-js@3.
npm WARN deprecated [email protected]: GraphQL Import has been deprecated and merged into GraphQL Tools, so it will no longer get updates. Use GraphQL Tools instead to stay up-to-date! Check out https://www.graphql-tools.com/docs/migration-from-import for migration and https://the-guild.dev/blog/graphql-tools-v6 for new changes.
npm WARN deprecated [email protected]: request has been deprecated, see https://github.com/request/request/issues/3142
/usr/local/bin/amplify -> /usr/local/lib/node_modules/@aws-amplify/cli/bin/amplify
Hi @LucasAndrad. For the querySessionSessionGroupsBySessionGroup and querySessionSessionGroupsBySession queries, I would expect them to not be generated since the SessionSessionGroup model has queries turned off with @model(queries: null). I'm not sure yet why querySessionsByCreatedAt isn't being generated though.
Hi @jhockett thanks for the reply, but querySessionSessionGroupsBySessionGroup and querySessionSessionGroupsBySession should be generated because of the @connection (the docs said that here) and they are generated correctly when I use version 4.21.2, but when I update to version 4.22 they are not generated anymore.
Looking at the example in the docs, the queries listed are not on the PostEditor type (which sets queries to null), rather they query the User or Post directly.
It's possible I'm misunderstanding something here, but if you set the queries to null on the @model then I think those queries should not have been generated in the previous version either.
I tested my self @model(queries: null) only blocks the generation of getItemName and listItemName
I can confirm the behavior @LucasAndrad is describing here. I also noticed that my auth directives like @aws_iam @aws_cognito_user_pools were not being applied to my generated schema until I removed queries: null from the @model.
For my use case, my schema makes heavy use of queries: null for limiting users access to many-to-many relationship data.
Consider the following association model:
type UserOrganizationAssociation
@model(queries: null, subscriptions: null)
@auth(
rules: [
{
allow: private
provider: iam
operations: [create, update, delete, read]
}
{ allow: private, operations: [read] }
]
)
@key(name: "byUser", fields: ["userID"])
@key(name: "byOrganization", fields: ["organizationID"]) {
id: ID!
userID: ID!
user: User @connection(fields: ["userID"])
organizationID: ID!
organization: Organization @connection(fields: ["organizationID"])
}
Before 4.22, I'd use this pattern in my schemas to create data that can only be queried through other models. From 4.22 on it just doesn't work, and I get an 401 Unauthorized trying to query into this model from connected models. I can get it to work again by removing the queries: null but then users can run ListUserOrganization query and get a dump of every org chart. That's okay in some scenarios, but not for multi-tenant applications.
In practice, putting queries: null on the many-to-many models is really convenient. I like users to be able to query through them, but not get at them directly, considering that's the whole idea behind association models.
If this need to be triaged, I'd be happy to join in. I have pushed a lot of amplify @auth mechanisms to their limits so I might have some weird use cases to showcase for or against certain functionality.
Thanks
Thanks for sharing your case @r0zar but as you can see, we need to wait a lot of months before get any feedback from amplify team. When I opened this issue this repository already had 620 issues (a lot) now they have 764.
Here is what I did to get my queries to work, I added this backend pre-build step on my amplify.yml to force the build to use version 4.21, the bad thing is that I can't update amplify anymore, so I hope they fix this bug before major changes in future versions.
backend:
phases:
preBuild:
commands:
- npm install -g @aws-amplify/[email protected]
@LucasAndrad @r0zar
I reproduced the issues you posted here, those extra queries were indeed generated before version 4.22, the @key transformer used to auto generate query operations, we have since disabled it.
If you still want to generate the queries, you can specify the "queryField" in the @key definition.
https://docs.amplify.aws/cli/graphql-transformer/directives#key
Okay, good to know! I'm going to test queryField in a key and queries: null in the model to make sure it works as expected. If that checks out, this issue is resolved for me.
Closing the issue for now, please open a new issue if you are still blocked.