Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 492
Chance Smith Avatar asked Mar 13 '19 15:03

Chance Smith


1 Answers

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
}
like image 82
Daniel Rearden Avatar answered Sep 19 '22 12:09

Daniel Rearden