How to set many variables into an anyofterms query?

Im trying to use variables to create an “anyofterms” parameters, like so:

query MyQuery($var1: String, $var2: String) {
  queryObject(filter: {origin: {anyofterms: `${var1} ${var2}`}}) {
    name
    origin
  }
}

Of course this does not work cause the syntax is incorrect.
Is there a proper way to get this to work?

PS: goal is to use this in the @auth directive, since my JWT will bring a list of allowed origins for that user.

I’ve tried this alternative as well:

@auth(
    query: { rule: """
          query ($var1: String, $var2: String) { 
            queryObject(filter: { origin: { eq: [$var1, $var2]  } } ) { 
                origin 
            } 
          }"""
       }
)

But it does not work as well, since the “eq” expects a string and not a list as DQL does. I’ve tried using DQL rules but I got as an answer that it is “not a valid rule”.

Wouldn’t an or conjunction work here?

1 Like

Thank you for your reply, @amaster507 !

You are correct, it does work! I was looking for some indexOf or contains kind of thing, but this approach is also possible. It translates in some nasty code, specially if we go for more probable variables, but I think it will do for now.

 @auth(
    query: { or: [
      { rule:"""
          query ($var1: String) { 
            queryObject(filter: {origin: {eq: $var1}}) {
                name 
            } 
          }
          """ 
        },
        { rule:"""
          query ($var2: String) { 
            queryObject(filter: {origin: {eq: $var2}}) {
                name 
            } 
          }
          """ 
        }
    ]
       }
) 

I’m trying to get a different solution by providing the list already translated into a "var1 var2 var3 … " format from my authentication service and using it with anyofterms instead, it should make it cleaner.

Thank you!

What I meant was:

query MyQuery($var1: String $var2: String) {
  queryObject(filter: { origin: { anyofterms: $var1 } or: { origin: { anyofterms: $var2 } } }) {
    name
    origin
  }
}

Hopefully in 20.11 the syntax will be easier with this change as:

query MyQuery($var1: String $var2: String) {
  queryObject(filter: { or: [ { origin: { anyofterms: $var1 } }, { origin: { anyofterms: $var2 } } ] }) {
    name
    origin
  }
}