Introduction to Dgraph for SQL Users

Filter Results

Most of the time a use case for selecting data will not need all of the data from a table (in SQL) or a type (in GraphQL). To get a subset of data, you add a filter. Filters in SQL take the form of the WHERE clause. In GraphQL, filters are arguments to a type block.

SQL syntax:

SELECT posts.id
FROM posts
WHERE ...

Similar GraphQL syntax:

query FILTERED_POSTS {
  queryPost(filter: { ... }) {
    id
  }
}

As a SQL user, you are accustomed to building filters using operators like = to separate the field you’re searching in from the search term. GraphQL takes a slightly different approach, where you build filters starting with the field (or predicate) to search, and then pair an operator (i.e, eq, GraphQL’s version of =) with the search term, as follows:

SQL syntax:

SELECT posts.id
FROM posts
WHERE
  posts.title = "Reflections"

Similar GraphQL syntax:

query FILTERED_POSTS {
  queryPost(filter: {
    title: { eq: "Reflections" }
  }) {
    id
  }
}

Filtering with and, or and not

Just as in SQL, you can combine filters in GraphQL with and, or, and not boolean connectives. In GraphQL, multiple object filters can be wrapped inside a list attached to one of these connectives, as follows:

SQL syntax:

posts.title = "Reflections" AND
posts.date = "2020-01-01"

Similar GraphQL syntax:

and: [
  {title: { eq: "Reflections }}
  {date: { eq: "2020-01-01" }}
]

When a GraphQL object filter contains multiple filters, they are implicitly joined with and logic. So, the syntax shown above is is logically the same as the following syntax:

title: { eq: "Reflections" }
date: { eq: "2020-01-01" }

SQL syntax:

posts.title = "Reflections" OR
posts.date = "2020-01-01"

Similar GraphQL syntax:

or: [
  {title: { eq: "Reflections" }}
  {date: { eq: "2020-01-01" }}
]

SQL syntax:

NOT posts.title = "Reflections"

Similar GraphQL syntax:

not: {title: { eq: "Reflections" }}

Nested filters

And, just as with SQL, GraphQL also lets you combine filters in nested statements. This lets you express advanced filter logic like “Fetch posts called ‘Reflections’, excluding posts dated 2020-01-01, 2020-02-15, or 2020-03-20”, as follows:

SQL syntax:

posts.title = "Reflections" AND
NOT (
  posts.date = "2020-01-01" OR
  posts.date = "2020-02-15" OR
  posts.date = "2020-03-20"
)

Similar GraphQL syntax:

title: { eq: "Reflections" }
not: {
  or: [
    {date: { eq: "2020-01-01" }}
    {date: { eq: "2020-02-15" }}
    {date: { eq: "2020-03-20" }}
  ]
}

After you get used to the syntax, you might find filtering in GraphQL with Dgraph’s generated inputs easier to read than SQL WHERE clauses. This is in part due to non-repeating conjunction logic words and reading the conjunction prefixed to the filters rather than postfixed. Proper white space formatting of placing each filter object on a separate line and indenting inside of conjunction blocks will go a long way to making your complex filters simple to read and understand.

Note: To learn more about Dgraph’s support for filtering query results, see Search and Filtering in the Dgraph documentation.