How do you create parameterized graphql queries?

In my project, I have a list of events. I want to be able to filter them in various ways. For example:

  • Get the events where the virtual event URL is NOT an empty string.
  • Get events that fall between two dates.
  • Get events within a radius of an address.

I know these filters require custom code to be written. I just don’t know where to put it. I was reading this doc: https://dgraph.io/docs/graphql/custom/dql/

The above doc uses a complex example with two steps. It first counts the number of a user’s followers, then sorts tweets by the number of followers of the author. I can’t tell if this applies to my use case or not.

So, what is the best way to create parameterized queries?

What is your example schema? None of these should require custom dql and can be done with simple queries using filters.

type Event {
  id: ID!
  url: String @search(by: [hash])
  date: DateTime! @search
  location: Point @search
}
  1. filter: { not: { url: { eq: "" } } }
  2. filter: { date: { between: { min: "2020-01-01", max: "2020-02-01" } } }
  3. filter: { location: { near: { coordinate: { latitute: 37.771935, longitude: -122.469829 }, distance: 1000 } } }

My schema was basically the same as your example. When I tried those types of queries I got the error:

Field "virtualEventUrl" is not defined by type EventFilter.

My problem was that I needed to add the search annotation to the virtualEventUrl field:

@search(by: [hash])

Now it works. Thank you.

Created a docs PR. Docs(GraphQL): Emphasize that @search annotation is required for filtering on predicates by catherineluse · Pull Request #247 · dgraph-io/dgraph-docs · GitHub