Amplify-cli: How To use @connection to create a self-relation?

Created on 23 Apr 2020  路  1Comment  路  Source: aws-amplify/amplify-cli

Hi

I'm trying to create a self-relation on a model but I can't get it working. Could you probably post some example how we can do it with the new parameterization of @connection?

I can only find some old examples:

https://github.com/aws-amplify/amplify-cli/issues/1303
https://github.com/aws-amplify/amplify-cli/issues/1799

I tried something like this

type Category 
  @model (subscriptions: null)
  @key(name: "ByTitle", fields: ["title"] queryField: "categoryByTitle")
{
  id: ID!
  title: String!
  parentCategoryID: ID!
  parent: Category @connection(fields: ["parentCategoryID"])
  children: [Category] @connection(fields: ["id"])
}

With the following mutations

# parent
mutation Restaurants {
  createCategory(input: {title: "Restaurants", parentCategoryID: "0", id: "a30b2921-e8dd-4003-9310-c416c56a6dfc"}) {
    title
    parentCategoryID
    id
  }
}

# child
mutation Pizza {
  createCategory(input: {title: "Pizza", parentCategoryID: "a30b2921-e8dd-4003-9310-c416c56a6dfc", id: "c85cdee4-ef10-4603-8194-b6b86bc43c63"}) {
    title
    parentCategoryID
    id
  }
}
  • query - wrong child
# request
query GetCategories {
  getCategory(id: "a30b2921-e8dd-4003-9310-c416c56a6dfc") {
    id
    title
    parentCategoryID
    parent {
      title
    }
    children {
      items {
        id
        title
      }
    }
  }
}

# response
{
  "data": {
    "getCategory": {
      "id": "a30b2921-e8dd-4003-9310-c416c56a6dfc",
      "title": "Restaurants",
      "parentCategoryID": "0",
      "parent": null,
      "children": {
        "items": [
          {
            "id": "a30b2921-e8dd-4003-9310-c416c56a6dfc",
            "title": "Restaurants"
          }
        ]
      }
    }
  }
}
@connection graphql-transformer question

Most helpful comment

@regenrek I think the following model produces the behavior you want:

type TreeNode @model @key(name: "ByParent", fields: ["parentID"]) {
  id: ID!
  parentID: ID!
  parent: TreeNode @connection(fields: ["parentID"])
  children: [TreeNode] @connection(keyName: "ByParent", fields: ["id"])
  description: String
}

The key piece that you didn't have above is the GSI (created by @key) to lookup items by the parent id and then specify that the children @connection should use that GSI to resolve the fields.

Closing this, but feel free to follow up if you have additional questions.

>All comments

@regenrek I think the following model produces the behavior you want:

type TreeNode @model @key(name: "ByParent", fields: ["parentID"]) {
  id: ID!
  parentID: ID!
  parent: TreeNode @connection(fields: ["parentID"])
  children: [TreeNode] @connection(keyName: "ByParent", fields: ["id"])
  description: String
}

The key piece that you didn't have above is the GSI (created by @key) to lookup items by the parent id and then specify that the children @connection should use that GSI to resolve the fields.

Closing this, but feel free to follow up if you have additional questions.

Was this page helpful?
0 / 5 - 0 ratings