Search code examples
graphqlaws-amplify

In graphql schema, how can I create a parent/child relationship of same model?


I'm needing to create a searchable listing table where some records are of type ORGANIZATION or RESOURCE. The relationshipis one-to-many. So, an Organization can have many Resources. How can I create this relationship under one model?

Using AWS Amplify GraphQL API...

Like this? schema.graphql

enum ListingType {
  ORGANIZATION
  RESOURCE
}
type Listing @model {
  id: ID!
  title: String!
  type: ListingType!
  orginzation: Listing
}

Yet, in Mutations, I can't reference a parent organization when creating my first Resource:

enter image description here


Solution

  • You need to include a @connection directive for any field that is a relationship, as outlined in the docs. In this case, something like this should work:

    type Listing @model {
      id: ID!
      title: String!
      type: ListingType!
      organization: Listing @connection
    }