Well, I am not sure how or when, but somewhere along the way I lost all of my inverse relationships for at least one three types. I have one side of the relationships still just need to recreate the opposite hasInverse side. There is a mixture of interfaces in here too which make the DQL a little complicated. Let me provide some data examples to work with:
GraphQL Schema Snippet:
type State {
name: String! @id
usedBy: [Address] @hasInverse(field: state)
}
interface Addr {
id: ID!
state: State
}
type Address implements Addr {
usedBy: [HasAddress] @hasInverse(field: address)
}
type HasAddress {
id: ID!
for: Contact @hasInverse(field: hasAddresses)
address: Address! @hasInverse(field: usedBy)
isPrimary: Boolean @search
isMailing: Boolean @search
}
type Contact {
id: ID!
hasAddresses: [HasAddress] @hasInverse(field: for)
}
Here are two DQL queries showing unmatched data:
{
queryState(func: type(State)) @cascade {
uid
State.name
State.usedBy {
uid
dgraph.type
}
}
queryAddress(func: type(Address)) @cascade {
uid
Addr.state {
uid
State.name
dgraph.type
}
}
}
Results Snippet:
{
"data": {
"queryState": [
{
"uid": "0x478a8",
"State.name": "Oklahoma",
"State.usedBy": [
{
"uid": "0x40355",
"dgraph.type": [
"Address"
]
}
]
},
{
"uid": "0x47b87",
"State.name": "Arkansas",
"State.usedBy": [
{
"uid": "0x2a558e",
"dgraph.type": [
"Addr",
"Address"
]
}
]
}
],
"queryAddress": [
{
"uid": "0x2f2e",
"Addr.state": {
"uid": "0x167d2",
"State.name": "Alaska",
"dgraph.type": [
"State"
]
}
},
{
"uid": "0x2f34",
"Addr.state": {
"uid": "0x167d2",
"State.name": "Alaska",
"dgraph.type": [
"State"
]
}
},
{
"uid": "0x2f4c",
"Addr.state": {
"uid": "0x2a05f",
"State.name": "Alabama",
"dgraph.type": [
"State"
]
}
},
{
"uid": "0x2f5c",
"Addr.state": {
"uid": "0x2a05f",
"State.name": "Alabama",
"dgraph.type": [
"State"
]
}
},
{
"uid": "0x2f62",
"Addr.state": {
"uid": "0x2a05f",
"State.name": "Alabama",
"dgraph.type": [
"State"
]
}
},
{
"uid": "0x2f63",
"Addr.state": {
"uid": "0x2a05f",
"State.name": "Alabama",
"dgraph.type": [
"State"
]
}
},
{
"uid": "0x2f66",
"Addr.state": {
"uid": "0x2a05f",
"State.name": "Alabama",
"dgraph.type": [
"State"
]
}
},
# ...
]
},
"extensions": {
"server_latency": {
"parsing_ns": 100942,
"processing_ns": 53174272,
"encoding_ns": 35421799,
"assign_timestamp_ns": 1386688,
"total_ns": 92303661
},
"txn": {
"start_ts": 1677836
},
"metrics": {
"num_uids": {
"Addr.state": 14197,
"State.name": 608,
"State.usedBy": 307,
"_total": 30222,
"dgraph.type": 303,
"uid": 14807
}
}
}
}
I would expect that the Addr.state
num_uids should be closely relative to State.usedBy
, but they are not. I literally only get the first two results only for the queryState function.
What I need to do is write the perfect script that for every Addr.state
produces the inverse State.usedBy
Then I need to go through at least a half dozen other relationships and fix them with the similar script.
What I don’t want to do is with Upsert make it where it maps all to one and one to all. This is a live data set so this fix needs to be correct the first time.