Reputation: 1894
I am new to graphql and I need to query multiple tables at once to display some data. I have a dashboard that shows information on a home where it comes from 5 tables: address, person, hostinfo, room, and image. I initially have the person_id
to query address
table which contains the person_id
etc... Here's what a brief scratch up of a uml looks like:
entity Address {
AddressId BigInteger,
FK_PersonId BigInteger,
StreetNumber Integer,
...
}
entity HostInfo {
HostInfoId BigInteger,
FK_AddressId BigInt,
HomestayName String,
...
}
entity Room {
RoomId BigInteger,
FK_HostInfoId BigInteger,
RoomName String,
...
}
entity Image {
FK_AddressId BigInt,
FK_RoomID BigInt,
Name String,
....
}
entity Person {
PersonId,
FirstName String,
Age Integer required, //TODO remember to check age
}
My question is, how do I use graphql to grab all the data from these tables using just PersonId
?
EDIT--------
I refactored the typedefiniton as follows:
export type Address = {
StreetNumber: number,
//..
Person:Person
}
export type HostInfo = {
HomestayName: string,
//..
Person:Person,
Room:[Room!],
Address:Address
}
export type Room = {
RoomName: string,
//..
RoomImage:[RoomImage!],
HostInfo:HostInfo
}
export type RoomImage = {
Name: string,
//..
Room:Room,
}
export type HostInfoImage = {
Name: string,
..
HostInfo:HostInfo
}
export type PersonImage = {
Name: string,
//..
Person:Person
}
export type Person = {
FirstName: string,
..
PersonImage:[PersonImage]
Address:Address
}
and perform the query as so:
query HostInfo($id: ID!) {
node(id: $id) {
... on Person {
firstName
address {
streetNumber
hostInfo {
HostName,
//...
Room {
RoomName,
[RoomImage],
//....
}
}
}
}
}
}
I would explicitly show the relationship both ways in my entities... I was expecting more of a sql(ly) way to do it.
Upvotes: 1
Views: 11054
Reputation: 158995
Typically GraphQL would represent all of the links between objects explicitly in the schema: if in your database model an Address
references a Person
, then your Person
GraphQL type would have a list of addresses. In the GraphQL schema language, you might have:
type Person implements Node {
id: ID!,
firstName: String,
age: Int!,
address: [Address!]!
}
If you knew the ID of a person, a typical query to retrieve much of the available data might hypothetically look like
query Person($id: ID!) {
node(id: $id) {
... on Person {
firstName
age
address {
streetNumber
hostInfo { ... }
}
}
}
}
Upvotes: 2