Query not working for @relation(name: "name")

I have the following objects:

User {
    name: String!
    bought: [ItemTransaction!] @relation(name: "buyer")
    sold: [ItemTransaction!] @relation(name: "seller")
    items: [Item!] @relation
}

Item {
    seller: User!
}

ItemTransaction {
   seller: User! @relation(name: "seller")
   buyer: User! @relation(name: "buyer")
}

I can upload the schema, but can’t query for sold or bought. All other fields work fine also the items field.

This is an example result. There should be data in bought:

{
  "data": {
    "findUserByID": {
       "name": "Test",
      "bought": {
        "data": []
      }
    }
  }
}

Hi @Leon and welcome!

The actual Ref values behind the relation should be stored in the ItemTransaction Documents. Can you inspect those Documents in the Dashboard and confirm they are there? Otherwise, how did you create the related documents?

Hi,

I am storing the actual refs inside ItemTransaction. I am adding those with fql. The result after the fql in an ItemTransaction document is:

{
   seller: Ref(Collection("User"), "312980746481414822"),
   buyer: Ref(Collection("User"), "312980746481234824"),
}

I have also tried adding them with gql, using connect, but the behavior was the same.

I’ve imported your schema, but I cannot replicate your issue.

// in the shell
Get(Ref(Collection("ItemTransaction"), "313434726578782272"))

{
  ref: Ref(Collection("ItemTransaction"), "313434726578782272"),
  ts: 1635173498620000,
  data: {
    seller: Ref(Collection("User"), "313434726571442240"),
    buyer: Ref(Collection("User"), "313434726572490816")
  }
}
# in graphql
{
  findUserByID(id: "313434726572490816") {
    _id
    name
    bought {
      data {
        _id
      }
    }
  }
}

{
  "data": {
    "findUserByID": {
      "_id": "313434726572490816",
      "name": "Test Buyer",
      "bought": {
        "data": [
          {
            "_id": "313434726578782272"
          },
          {
            "_id": "313434858610229314"
          },
          {
            "_id": "313434864626958402"
          },
          {
            "_id": "313434870671999042"
          }
        ]
      }
    }
  }
}

In addition to double checking the documents and schema, can you make sure that the index is defined correctly for the current schema? It should look like this:

Get(Index("buyer_by_user"))

{
  ref: Index("buyer_by_user"),
  ts: 1635173382940000,
  active: true,
  serialized: true,
  name: "buyer_by_user",
  source: Collection("ItemTransaction"),
  data: {
    gql: {
      ts: Time("2021-10-25T14:49:42.871975Z")
    }
  },
  terms: [
    {
      field: ["data", "buyer"]
    }
  ],
  unique: false,
  partitions: 1
}

Hi,

this helped. The Problem was that I had two other fields

reviewsReceived: [Review!] @relation(name: "seller")
reviewsGiven:  [Review!] @relation(name: "buyer")

Only two indexes for both of these relations were created. The problem here was that the field had the value buyerRef, which was a field in Review. After changing the Review relation name, 4 indexes where created.

Thanks a lot for your answer!!

1 Like

Hi @Leon. I’m glad you figured it out!

This looks like a bug in the way that schema are uploaded. Reusing a name for an @relation should not be allowed. I’ve created an internal ticket to track and prioritize fixing that.

Cheers!

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.