Introduction to Dgraph for SQL Users

Fine-Tune Results

The previous examples comparing how to combine data through SQL joins and GraphQL traversals only covered a single SQL join type, the left join. There are four main join types in SQL: inner, left, right, and full.

A visual representation of various types of joins

A visual representation of various types of joins

GraphQL allows you to express all of these join types using queries, as shown below:

Inner Join SQL syntax:

SELECT
  posts.id,
  posts.title,
  people.id,
  people.username
FROM
  posts
  JOIN people
    ON posts.author = people.id

Similar GraphQL syntax:

query POSTS_HAS_AUTHORS {
  queryPost(filter: { has: author }) {
    id
    title
    author {
      username
    }
  }
}

Left Join SQL Synax:

SELECT
  posts.id,
  posts.title,
  people.id,
  people.username
FROM
  posts
  LEFT JOIN people
    ON posts.author = people.id

Similar GraphQL syntax:

query POSTS_WITH_AUTHORS {
  queryPost {
    id
    title
    author {
      username
    }
  }
}

Right Join SQL syntax:

SELECT
  posts.id,
  posts.title,
  people.id,
  people.username
FROM
  posts
  RIGHT JOIN people
    ON posts.author = people.id

Similar GraphQL syntax:

query AUTHORS_WITH_POSTS {
  queryPerson {
    username
    authoredPosts {
      id
      title
    }
  }
}

Full Outer Join SQL syntax:

SELECT
  posts.id,
  posts.title,
  people.id,
  people.username
FROM
  posts
  FULL OUTER JOIN people
    ON posts.author = people.id

Similar GraphQL syntax:

query POSTS_WITH_AUTHORS {
  queryPost {
    id
    title
    author {
      username
    }
  }
  queryPerson {
    username
    authoredPosts {
      id
      title
    }
  }
}

The full outer join GraphQL equivalence returns two root queries. You can think of this as being somewhat similar to SQL UNION syntax. However, with SQL unions, the two queries being “unioned” together are flattened into a single table output. For this reason, SQL unions require a matching set of field selections. With GraphQL, multiple root queries can have unique field selections and the results are returned as an object with the desired properties, rather than a single table.

SQL uses the term self join when a table is being joined to itself. One or both of the tables must be aliased to avoid table name conflicts with SQL. In GraphQL, this is achieved with the same syntax as traversing an edge.

SQL syntax:

SELECT
  posts.id,
  posts.title,
  comments.id,
  comments.text,
  replies.id,
  replies.text,
  replies2.id,
  replies2.text,
  replies3.id,
  replies3.text
FROM
  posts
  LEFT JOIN comments
    ON comments.on_post=posts.id
  LEFT JOIN comments replies
    ON replies.reply_to=comments.id
  LEFT JOIN comments replies2
    ON replies2.reply_to=replies.id
  LEFT JOIN comments replies3
    ON replies3.reply_to=replies2.id

Similar GraphQL syntax:

query POSTS_W_COMNT_TREE {
  queryPost {
    id title comments {
      id text hasReplies {
        id text hasReplies {
          id text hasReplies {
            id text
          }
        }
      }
    }
  }
}