If I have nested ands/ors in a @auth directive. Are they processed first to last until a condition is met?
In other words, should my simpler queries be listed first in the array?
Example Schema:
type Post @auth(
query: {or: [ # either is published w/(public or authenticated user) or author or admin
{and: [ # is published and either public or authenticated user
{rule: "query ($NOW: DateTime!) { queryPost(filter: {published: {ge: $NOW}}) { id } }"} # publish date >= now
{or: [ # either public or authenticated user
{rule: "query { queryPost(filter: {isPublic: true}) { id } }"}, # allow anyone to see public
{rule: "$USERROLE: {eq: \"USER\" }"}, # allow users
]}
]}
{rule: "query ($USERID: String!) { queryPost { author(filter: {id: [$USERID] }) { id } } }"} # allow authors to see their own
{rule: "$USERROLE: {eq: \"ADMIN\" }"} # allow admin to see all
]}
) {
id: ID!
isPublic: Boolean! @search
published: DateTime! @search
author: Person
content: String
}
I understand that in most logic processing a response is returned as soon as possible without needing to continue through the logic if not necessary. I am assuming that Dgraph works the same way.
For performance, I understand that:
- A variable check against a string value > a variable check against a query
- A filter against a id > a filter against a Boolean
- A filter against a Boolean > a filter against a DateTime
- A filter against a DateTime > a filter against a String
- A query w/filter on top level > a query w/filter on lower levels
So I think the following would allow for better performance:
type Post @auth(
query: {or: [ # either admin or author or is published w/(public or authenticated user)
{rule: "$USERROLE: {eq: \"ADMIN\" }"} # allow admin to see all
{rule: "query ($USERID: String!) { queryPost { author(filter: {id: [$USERID] }) { id } } }"} # allow authors to see their own
{and: [ # is published and either public or authenticated user
{or: [ # either public or authenticated user
{rule: "$USERROLE: {eq: \"USER\" }"}, # allow users
{rule: "query { queryPost(filter: {isPublic: true}) { id } }"}, # allow anyone to see public
]}
{rule: "query ($NOW: DateTime!) { queryPost(filter: {published: {ge: $NOW}}) { id } }"} # publish date >= now
]}
]}
) {
id: ID!
isPublic: Boolean! @search
published: DateTime! @search
author: Person
content: String
}
Questions:
- Is my understanding of performance above accurate?
- Is this accurate that ordering of @auth nested directives matters and is read FIFO?
- Is there any way to use replace $NOW in the schema with the current DateTime instead of passing it through a JWT? (maybe should be its own topic)