Query of field with @hasInverse of list returns no data, why?

I hope it’s OK to ask a simple question here, I’ve been trying to figure this out with the docs and haven’t been successful.

I can’t tell why my query isn’t returning data from a list. I’m not sure if the issue is in the schema, how the data is represented in the RDFs, or with the query itself.

Here is an example with some dummy data:

Schema:

type BookTitle {
    id: String! @id
    title: String!
    book: Book @hasInverse(field: bookTitles)
}
type Book {
    id: String! @id
    bookTitles: [BookTitle]! @hasInverse(field: book)
}

Data:

_:Book.123 <dgraph.type> "Book" .
_:Book.123 <Book.id> "Book.123"^^<xs:string> .
_:BookTitle.123 <dgraph.type> "BookTitle" .
_:BookTitle.123 <BookTitle.id> "BookTitle.123"^^<xs:string> .
_:BookTitle.123 <BookTitle.title> "War and Peace"^^<xs:string> .
_:BookTitle.123 <BookTitle.book> _:Book.123 .

Query:

query {
  getBook(id: "Book.123") {
    id
    bookTitles {
        title
    }
  }
}

Result:

{"data":{
  "getBook":
    {"id": "Book.123",
     "bookTitles": []
    }
  }
}

Why is bookTitles empty?

The hasInverse directive does not use the reverse index. Instead it creates two actual inverse edges and manages them in the API layer. So when you add the rdf triples, you need to add both edges for every relationship having the hasInverse directive.


Check out this script to maybe help you fix the missing relationships:

2 Likes

Thank you @amaster507, that did it!

I reset my dummy database and reloaded the data, this time with the data below (last line added):

_:Book.123 <dgraph.type> "Book" .
_:Book.123 <Book.id> "Book.123"^^<xs:string> .
_:BookTitle.123 <dgraph.type> "BookTitle" .
_:BookTitle.123 <BookTitle.id> "BookTitle.123"^^<xs:string> .
_:BookTitle.123 <BookTitle.title> "War and Peace"^^<xs:string> .
_:BookTitle.123 <BookTitle.book> _:Book.123 .
_:Book.123 <Book.bookTitles> _:BookTitle.123 .

and now the results of the same query look like this:

{
  "data": {
    "getBook": {
      "id": "Book.123",
      "bookTitles": [
        {"title": "War and Peace"}
      ]   
    }
  }
}

I really appreciate the response.

1 Like