Morrious
Morrious

Reputation: 179

AWS AppSync Schema - Arrays of Connections Both Directions

My issue is that I need to have a connection between two schema models where each links to an array of the other. The scenario here is I have multiple Location, each Location has multiple Staff. Each Staff can also have multiple Location that they are authorized at.

I don't see a way to do this in the documentation. Originally I have each Location with multiple Staff, but this limits each Staff to 1 Location. I'm toying with the idea of altLocationNames as an array of strings with each Staff, but this would require me to perform an extra query of all Staff or all Locations if the altLocationNames is needed.

Does anybody know a clever way to do this, or even just the best way for me to get all Staff at Location as well as all Locations for a specific Staff?

Schemas:

type Staff
  @model
  @auth(rules: [{ allow: private, provider: iam }, { allow: private, provider: userPools }])
  @key(fields: ["email"])
  @key(name: "byId", fields: ["id"], queryField: "listStaffsById")
  @key(name: "byLocation", fields: ["locationName", "recordedAt"], queryField: "listStaffsByLocation")
  @key(name: "byRole", fields: ["role", "recordedAt"], queryField: "listStaffsByRole")
  @key(name: "byStatus", fields: ["status", "recordedAt"], queryField: "listStaffsByStatus") {
  email: AWSEmail!
  altEmails: [AWSEmail]
  id: ID!
  locationName: String!
  location: Location @connection(fields: ["locationName"])
  altLocationNames: [String]
  firstName: String
  lastName: String
  role: StaffRole
  status: StaffStatus
  recordedAt: AWSDateTime!
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime!
}

type Location
  @model
  @auth(rules: [{ allow: private, provider: iam }, { allow: private, provider: userPools }])
  @key(fields: ["name"]) {
  name: String!
  subdomain: String
  address: String
  phone: String
  email: AWSEmail
  staff: [Staff] @connection(keyName: "byLocation", fields: ["name"])
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime!
}

Upvotes: 0

Views: 452

Answers (1)

mkaya387
mkaya387

Reputation: 388

Can you create middle table between location and staff table and fetch via that table. Example:

type Staff
  @model {
  id: ID!
  name: String
  locations: [StaffLocation] @connection(keyName: "staffLocationByStaffId", fields: ["id"])
}
type Location
  @model {
  id: ID!
  name: String
  staffs: [StaffLocation] @connection(keyName: "staffLocationByLocationId", fields: ["id"])
}
type StaffLocation
  @key(name: "staffLocationByStaffId", fields: ["staffId"])
  @key(name: "staffLocationByLocationId", fields: ["locationId"])
  @model {
  id: ID!
  staffId: ID!
  locationId: ID!
  staff: Staff @connection(fields: ["staffId"])
  location: Location @connection(fields: ["locationId"])
}

Upvotes: 1

Related Questions