Variant relations

Define variant types with relations

Variant types support entity relationship in a different way unlike the normal entity relationship. There are some limitations with variant relations:

  • Only one-to-many and many-to-one relations are supported

  • Reverse lookup is not supported

Let's take a look an example:

  1. Schema: in the schema below there are two variant types with the relations

type BoughtMemberEvent @entity {
  id: ID!
  name: String
  handle: String!
}

type InvitedMemberEvent @entity {
  id: ID!
  name: String
  handle: String!
}

type MemberInvitation @variant {
  event: InvitedMemberEvent!
}

type MemberPurchase @variant {
  event: BoughtMemberEvent!
}

union MemberSource = MemberInvitation | MemberPurchase

type Member @entity {
  id: ID!
  isVerified: Boolean!
  handle: String!
  source: MemberSource!
}
  1. Mappings: insert data into database

For variant relations to work an additional field is added to variant type which is db only field (which means it is not visible in the graphql API). This field is will be generated from relation field name + 'id' ie. in the schema above relation name is event so the auto generated field name is eventId. This field is not optional and mapping author must set it properly.

  1. Query: fetch all members' source:

Last updated

Was this helpful?