Previously, we have seen how to create a link between two objects using hypi.ids.
link
function also creates a link between two objects. The link can be a one-to-one reference or a one-to-many reference.
Let’s see how to use the link
function. We will use the below schema.
type OldObject {
field1: String
}
type NewObject {
fld1: String
fld2: OldObject
fld3: [OldObject]
}
- There is a one-to-one reference between NewObject and OldObject via field
fld2
. - There are one-to-many references between NewObject and OldObject via field
fld3
array of type OldObject. - Link function has the below signature.
link(
from:HypiMutationType!
to:HypiMutationType!
via:String!
whereFromID:String!
andToID:String!
):Boolean!
-
from
andto
fields indicate the data types to create a link -
via
is the field of data type ‘to’ in the data type ‘from’ -
whereFromID
andandToID
are the hypi.id of the objects to be linked.
Let’s create objects of type OldObject and NewObject
#OldObject
mutation {
upsert(
values: {
OldObject: [
{ field1: "Hypi's Low code Platform!" }
{ field1: "Hypi easy to use backend-as-a-service" }
]
}
) {
id
}
}
# Result
{
"data": {
"upsert": [
{
"id": "01FHZM11Q6MG3NKVR8D14QGKFH"
},
{
"id": "01FHZM11Q9C03SJK1XM9SK603R"
}
]
}
}
#NewObject
mutation {
upsert(values: { NewObject: [{ fld1: "Create link between two objects" }] }) {
id
}
}
#result
{
"data": {
"upsert": [
{
"id": "01FHZM6G4P3N7AARH85755VX0X"
}
]
}
}
Sample Query
Now we will link the created objects using the link
function
#one-to-one reference
mutation {
link(
from: NewObject
to: OldObject
via: "fld2"
whereFromID: "01FHZM6G4P3N7AARH85755VX0X"
andToID: "01FHZM11Q6MG3NKVR8D14QGKFH"
)
}
#one-to-many references
mutation {
link(
from: NewObject
to: OldObject
via: "fld3"
whereFromID: "01FHZM6G4P3N7AARH85755VX0X"
andToID: "01FHZM11Q6MG3NKVR8D14QGKFH"
)
}
mutation {
link(
from: NewObject
to: OldObject
via: "fld3"
whereFromID: "01FHZM6G4P3N7AARH85755VX0X"
andToID: "01FHZM11Q9C03SJK1XM9SK603R"
)
}
#results
{
"data": {
"link": true
}
}
Output
The function returns true
if the linking was successful otherwise false
.
We will check the linked data using get function.
{
get(type: NewObject, id: "01FHZM6G4P3N7AARH85755VX0X") {
... on NewObject {
fld1
fld2 {
field1
}
fld3 {
field1
}
}
}
}
#result
{
"data": {
"get": {
"fld1": "Create link between two objects",
"fld2": {
"field1": "Hypi's Low code Platform!"
},
"fld3": [
{
"field1": "Hypi's Low code Platform!"
},
{
"field1": "Hypi easy to use backend-as-a-service"
}
]
}
}
}
Query with Variables
mutation linkObjecs(
$from: HypiMutationType!
$to: HypiMutationType!
$via: String!
$whereFromID: String!
$andToID: String!
) {
link(
from: $from
to: $to
via: $via
whereFromID: $whereFromID
andToID: $andToID
)
}
Check the POSTMAN collection for the link
requests in different programming languages! Click </> and choose the programming language of your choice.
Don’t forget to insert your own Authorization key and Hypi Domain under Headers to test the results!