Use GraphQL Vars In regex

Got it working with my local instance, just wasn’t working in play.dgraph.io for some reason. Thanks.

EDIT: @MichelDiz

The docs say that this ↑ is “variable substitution”, but what does that mean exactly? Is "/Test/i" a default value that is overridden when a value is supplied to the $test parameter, or can it not be overriden?
https://dgraph.io/docs/query-language/graphql-variables/

I’ve run into an issue, I have a custom query that paginates results, and optionally filters by a search term. However, if I run the query without a search keyword, then I get an error:

Uncaught (in promise) Error: GraphQL Request Error: resolving queryTranscriptsIndex failed because Dgraph query failed because Dgraph execution failed because Unexpected error while parsing regex arg:

The only way to resolve this error was to set the parameter to an empty regex string: $keyword: string = "//" in the query definition. However, after adding this, the filter no longer takes effect (instead of receiving filtered results corresponding to the keyword, I receive the first n results unfiltered).

My expectation was that if no value is provided for the keyword parameter, then the filter should not take effect.

type Query {
  queryTranscriptsIndex(first: Int!, offset: Int!, keyword: String): [Transcript] @custom(dql: """
    query q($first: int, $offset: int, $keyword: string = "//") {
      queryTranscriptsIndex(func: type(Transcript), first: $first, offset: $offset) @filter(regexp(Transcript.name, $keyword)){
        id: uid
        isPublished: Transcript.isPublished
        name: Transcript.name
        primaryLanguage: Transcript.primaryLanguage
        slug: Transcript.slug
      }
    }
  """)

  queryTranscriptsIndexMetadata: Metadata @custom(dql: """
    query {
      var(func: type(Transcript)) {
        a as count(uid)
      }

      queryTranscriptsIndexMetadata() @normalize {
        total_count : sum(val(a))
      }
    }
   """)
}

EDIT: I have this working by setting keyword with a default value on client:

Client code:

          return new Promise((resolve, reject) => {
            let variables = {
              keyword: "//i",
              first: 10,
              offset: 0,
              ...opts.body
            }

            graphQLClient.request(queryTranscriptsIndex, variables)
              .then((res) => {
                resolve({
                  data: res.queryTranscriptsIndex.map(transcript => {
                    return [transcript.name, transcript.primaryLanguage, transcript.slug]
                  }),
                  total: res.queryTranscriptsIndexMetadata.total_count
                })
              })
              .catch((error) => {
                reject(error)
              })
          })

GraphQL Query:

query queryTranscriptsIndex($first: Int!, $offset: Int!, $keyword: String!) {
  queryTranscriptsIndex(first: $first, offset: $offset, keyword: $keyword) {
    name
     primaryLanguage
     slug
  }
  queryTranscriptsIndexMetadata(keyword: $keyword) {
    total_count
  }
}

DGraph Custom Query in Schema:

type Query {
  queryTranscriptsIndex(first: Int!, offset: Int!, keyword: String!): [Transcript] @custom(dql: """
    query q($first: int, $offset: int, $keyword: string) {
      queryTranscriptsIndex(func: type(Transcript), first: $first, offset: $offset) @filter(regexp(Transcript.name, $keyword)){
        id: uid
        isPublished: Transcript.isPublished
        name: Transcript.name
        primaryLanguage: Transcript.primaryLanguage
        slug: Transcript.slug
      }
    }
  """)

  queryTranscriptsIndexMetadata(keyword: String!): Metadata @custom(dql: """
    query q($keyword: string) {
      var(func: type(Transcript)) @filter(regexp(Transcript.name, $keyword)) {
        a as count(uid)
      }

      queryTranscriptsIndexMetadata() @normalize {
        total_count : sum(val(a))
      }
    }
   """)
}