aws-amplify / amplify-codegen

Amplify Codegen is a JavaScript toolkit library for frontend and mobile developers building Amplify applications.
Apache License 2.0
60 stars 63 forks source link

Unable to create model with required one to many relation #884

Closed marcoboerner closed 1 month ago

marcoboerner commented 1 month ago

Environment information

System:
  OS: macOS 15.1
  CPU: (12) arm64 Apple M3 Pro
  Memory: 123.80 MB / 36.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 22.9.0 - /opt/homebrew/bin/node
  Yarn: undefined - undefined
  npm: 10.8.3 - /opt/homebrew/bin/npm
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/auth-construct: 1.3.0
  @aws-amplify/backend: 1.2.1
  @aws-amplify/backend-auth: 1.1.4
  @aws-amplify/backend-cli: 1.2.6
  @aws-amplify/backend-data: 1.1.3
  @aws-amplify/backend-deployer: 1.1.2
  @aws-amplify/backend-function: 1.4.0
  @aws-amplify/backend-output-schemas: 1.2.0
  @aws-amplify/backend-output-storage: 1.1.1
  @aws-amplify/backend-secret: 1.1.1
  @aws-amplify/backend-storage: 1.1.2
  @aws-amplify/cli-core: 1.1.2
  @aws-amplify/client-config: 1.3.0
  @aws-amplify/deployed-backend-client: 1.4.0
  @aws-amplify/form-generator: 1.0.1
  @aws-amplify/model-generator: 1.0.6
  @aws-amplify/platform-core: 1.1.0
  @aws-amplify/plugin-types: 1.2.1
  @aws-amplify/sandbox: 1.2.1
  @aws-amplify/schema-generator: 1.2.2
  aws-amplify: 6.6.0
  aws-cdk: 2.158.0
  aws-cdk-lib: 2.158.0
  typescript: 5.6.2
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Describe the bug

I defined a parent and child model in the resource.ts file and created Swift code with the CLI. When I try to create the model in my app, I get the following error:

GraphQLResponseError<TestChild>: GraphQL service returned a partially-successful response containing errors: [Amplify.GraphQLError(message: "Cannot return null for non-nullable type: \'ID\' within parent \'TestParent\' (/createTestChild/parent/id)", locations: nil, path: Optional([Amplify.JSONValue.string("createTestChild"), Amplify.JSONValue.string("parent"), Amplify.JSONValue.string("id")]), extensions: nil)]
Recovery suggestion: The list of `GraphQLError` contains service-specific messages.

Reproduction steps

Define the following model in the resource.ts file:

TestChild: a.model({
  content: a.string().required(),
  parentId: a.id().required(),
  parent: a.belongsTo('TestParent', 'parentId')
})
  .authorization(allow => [allow.owner()]),

TestParent: a.model({
  content: a.string().required(),
  children: a.hasMany('TestChild', 'parentId')
})
  .authorization(allow => [allow.owner()]),

Create the Swift code with: npx ampx generate graphql-client-code --format modelgen --model-target swift

Try to create the model with:

func testModelCreation() {
    Task {
        do {
            let testParent = TestParent(
                content: "Parent"
            )

            let createdParent = try await Amplify.API
                .mutate(request: .create(testParent))
                .get()

            let testChild = TestChild(
                content: "Child",
                parent: createdParent
            )

            _ = try await Amplify.API
                .mutate(request: .create(testChild))
                .get()
        } catch {
            debugPrint(error)
        }
    }
}
ykethan commented 1 month ago

Hey, thanks for raising this! I'm going to transfer this over to our codegen repository for better assistance.

marcoboerner commented 1 month ago

I learned that it seems both models and the relations are actually created correctly. When I query for both, after the failed mutation, I get the correct results. It seems the issue is somewhere in the try await Amplify.API.mutate(request:.create(myModel)) method, or maybe one of the generated initializers.

dpilch commented 1 month ago

The error message is expected here. You can remove this error message by lowering the statement max depth to 1 with --statement-max-depth 1 or by disabling subscriptions with a.model(...).disableOperations(['subscriptions']).

Please see the message at the top of this page for more details. https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/

github-actions[bot] commented 1 month ago

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.