Stepping out on a limb here. Is there any way to possibly do logic between two different levels in a single query?
type Person {
id: ID!
name: String @search(by: [fulltext])
}
type Book {
id: ID!
title: String @search(by: [fulltext])
author: [Person]
}
What if I wanted to find every book that had either the word James in the title or as the author?
{
queryBook(filter: {title: {alloftext: "James"}}) {
id
title
author(filter: {name: {alloftext: "James"}}) {
id
name
}
}
}
This would only get the books that have an AND logic but how could this be done in a single query with OR logic? An answer using Dgraph (GraphQL±) is acceptable, but not preferable. I understand that GraphQL has its limitations to predefined schema where gragphql± is a bit more flexible in that area and it can use variables.
I think by AND you meant: query only the books that have a title "James" AND their author's name is "James" as well. But, this is a bit different.
The query here will mean this: query only the books that have a title "James" and then for those books, query their author only if the author name is "James". i.e., even if some book’s title is “James” and it’s author’s title is “Harold”, it will still fetch the book, but not the author.
And, for the AND I explained here in 1, it can be done with @cascade on top query, like this:
{
queryBook(filter: {title: {alloftext: "James"}}) @cascade {
id
title
author(filter: {name: {alloftext: "James"}}) {
id
name
}
}
}
I don’t think it could be done in a single query with GraphQL, at present. We would have to think about supporting this kind of logic in GraphQL.
With GraphQL± this is possible in the following way:
query {
JamesAsTitle as var(func: alloftext(Book.title, "James"))
JamesAsAuthor as var(func: type(Book)) @cascade {
Book.author @filter(alloftext(Person.name, "James"))
}
JamesBooks(func: type(Book)) @filter(uid(JamesAsTitle) OR uid(JamesAsAuthor)) {
uid
expand(_all_) {
expand(_all_)
}
}
}
Just FYI, experimental DQL support in GraphQL is available in master. Have a look here. So, maybe you could use that to make your flow more efficient. But, it is the very beginning of this feature, so things may change anytime.
yes! I have been watching the integration of DQL into custom as well as upcoming JS integration into Custom. Maybe that could make this whole process doable without an external script (lambda function)