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

@belongsTo referencing composite key does not work properly #91

Closed
5 tasks done
cjihrig opened this issue Mar 8, 2022 · 0 comments · Fixed by #462
Closed
5 tasks done

@belongsTo referencing composite key does not work properly #91

cjihrig opened this issue Mar 8, 2022 · 0 comments · Fixed by #462
Assignees
Labels
bug Something isn't working graphql-transformer-v2 p2

Comments

@cjihrig
Copy link
Contributor

cjihrig commented Mar 8, 2022

Before opening, please confirm:

  • I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
  • I have searched for duplicate or closed issues.
  • I have read the guide for submitting bug reports.
  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • I have removed any sensitive information from my code snippets and submission.

How did you install the Amplify CLI?

npm

If applicable, what version of Node.js are you using?

Not applicable

Amplify CLI Version

7.6.23

What operating system are you using?

macOS

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

No

Amplify Categories

api

Amplify Commands

Not applicable

Describe the bug

When @belongsTo is used with implicit fields to reference a foreign composite key, queries do not work as expected. See the reproduction steps.

Expected behavior

@belongsTo works properly with foreign composite keys.

Reproduction steps

  1. Push the schema shown in this issue.
  2. Create a new Owner:
mutation MyMutation {
  createOwner(input: {id: "1", name: "Foo", wea: "FooWea"}) {
    blogs {
      items {
        id
      }
    }
    createdAt
    id
    name
    updatedAt
    wea
  }
}
  1. Create another Owner with the same name, but different wea value:
mutation MyMutation {
  createOwner(input: {id: "1", name: "Foo", wea: "FooWea2"}) {
    blogs {
      items {
        id
      }
    }
    createdAt
    id
    name
    updatedAt
    wea
  }
}
  1. Create a new Blog:
mutation MyMutation {
  createBlog(input: {id: "1", name: "FooBlog", ownerBlogsId: "Foo"}) {
    updatedAt
    ownerBlogsId
    owner {
      createdAt
      id
      name
      updatedAt
      wea
    }
    name
    id
    createdAt
  }
}

The Blog item is successfully created, but the @belongsTo does not properly identify the correct Owner object, so the following error is returned:

{
  "data": {
    "createBlog": null
  },
  "errors": [
    {
      "path": [
        "createBlog",
        "owner"
      ],
      "locations": null,
      "message": "Cannot return null for non-nullable type: 'Owner' within parent 'Blog' (/createBlog/owner)"
    }
  ]
}

Similarly, any attempt to query for a Blog with the Owner in the selection set returns the same error for the same reason.

The Blog.owner.req.vtl file is relevant. Note the presence of $ctx.source.undefined:

#if( $ctx.source.deniedField )
  #return($util.toJson(null))
#end
#if( $util.isNull($ctx.source.ownerBlogsId) )
  #return
#else
  #set( $GetRequest = {
  "version": "2018-05-29",
  "operation": "Query"
} )
  $util.qr($GetRequest.put("query", {
  "expression": "#partitionKey = :partitionValue AND #sortKeyName = :sortKeyName",
  "expressionNames": {
      "#partitionKey": "name",
      "#sortKeyName": "wea"
  },
  "expressionValues": {
      ":partitionValue": $util.parseJson($util.dynamodb.toDynamoDBJson($util.defaultIfNullOrBlank($ctx.source.ownerBlogsId, "___xamznone____"))),
      ":sortKeyName": $util.parseJson($util.dynamodb.toDynamoDBJson($util.defaultIfNullOrBlank($ctx.source.undefined, "___xamznone____")))
  }
}))
  #if( !$util.isNullOrEmpty($ctx.stash.authFilter) )
    $util.qr($GetRequest.put("filter", $util.parseJson($util.transform.toDynamoDBFilterExpression($ctx.stash.authFilter))))
  #end
  $util.toJson($GetRequest)
#end

GraphQL schema(s)

input AMPLIFY { globalAuthRule: AuthRule = { allow: public } } # FOR TESTING ONLY!

type Blog @model {
  id: ID!
  name: String!
  owner: Owner! @belongsTo
}

type Owner @model {
  id: ID!
  name: String! @primaryKey(sortKeyFields: ["wea"])
  wea: String!
  blogs: [Blog] @hasMany
}

Log output

Not applicable

Additional information

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working graphql-transformer-v2 p2
Projects
None yet
3 participants