Use GraphQL Vars In regex

I Want to Do

Use GQL variables in DQL query inside a regex filter

What I Did

query find_user($term: string){
  find_user(func: type(User)) @filter(regexp(User.username, /$term/i) or regexp(User.email, /^$term/i)){
    uid
    usern_name: User.username
    User.email
    User.full_name
  }
}

Problem

$ is a special character in regex

1 Like

You should try something like this

query q( $name: string = "/^Steven Sp.*$/", $name_ryan: string = "/ryan/i")  {
  directors(func: regexp(name@en, $name )) {
    name@en
    director.film @filter(regexp(name@en, $name_ryan)) {
      name@en
    }
  }
}
1 Like

Hi Michel,

I’m also struggling with this:




schema:

type Transcript {
  id: ID!
  name: String! @search(by: [regexp])
  primaryLanguage: String!
  isPublished: Boolean! @search
  shareDescription: String
  shareTitle: String
  slug: String! @id
  video: Video @hasInverse(field: transcript)
}

Seems that the either the interpolation of the variable, or how the variable is passed in Ratel is the issue?

I noticed that when I input a variable in Ratel, it is prefixed with $ in the sent request:

image

EDIT: Ok, that’s not the issue. Pasted the query into Paw and removed the $ and it throws an error:

Not sure what is happening to your context. But the right query should be like bellow.

query q($test: string = "/Test/i")  {
  directors(func: has(name), first:10) @filter(regexp(name@en, $test)) {
    name@en
    director.film {
      name@en
    }
  }
}

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))
      }
    }
   """)
}

In general it is the Chrome blocking non-https connections with a https application.

This is a default value. If you add a value in the variable method the default value will be ignored.

Please, new issues, new tickets. To not confuse and also help others based on title and context.

Is that solved?

Cheers.

1 Like

Where do I file a new issue or ticket?

1 Like

Just start a new topic for new issues in this forum instead of continuing a thread out of scope of OP

2 Likes