Introduction to Dgraph for SQL Users

Schema Comparison: GraphQL vs. SQL

For a point of reference while working through the rest of this tutorial, it’s important to have an understanding of the GraphQL and SQL schemas used for the examples in this tutorial.

The following GraphQL schema, is used throughout this tutorial:

type Person {
  username: String!
  likedPost: [Post]
  likesComments: [Comment]
  authoredPosts: [Post]
  authoredComments: [Comment]
}

type Post {
  id: ID
  title: String!
  date: DateTime!
  author: Person!
  comments: [Comment]
  likedBy: [Person]
}

type Comment {
  id: ID
  text: String!
  datetime: DateTime!
  author: Person!
  onPost: Post
  replyTo: Comment
  hasReplies: [Comment]
  likedBy: [Person]
}

This is parallel to the SQL schema shown below, which is best understood by viewing the following tabular diagram:

SQL Schema Diagram

A SQL database entity relationship diagram

You can also refer to the following SQL schema that is specific to the MySQL implementation, to compare it to the GraphQL schema shown above:

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(11) NOT NULL,
  `text` int(11) NOT NULL,
  `datetime` int(11) NOT NULL,
  `author` int(11) NOT NULL,
  `on_post` int(11) DEFAULT NULL,
  `reply_to` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `likes` (
`id` int(11) NOT NULL,
  `by_id` int(11) NOT NULL,
  `post_id` int(11) NOT NULL,
  `comment_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `people` (
`id` int(11) NOT NULL,
  `username` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `posts` (
  `id` int(11) NOT NULL,
  `title` varchar(255) NOT NULL,
  `date` date NOT NULL,
  `author` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `comments`
 ADD PRIMARY KEY (`id`), ADD KEY `author` (`author`), ADD KEY `on_post` (`on_post`), ADD KEY `reply_to` (`reply_to`);
ALTER TABLE `likes`
 ADD PRIMARY KEY (`id`), ADD KEY `by_id` (`by_id`), ADD KEY `comment_id` (`comment_id`), ADD KEY `post_id` (`post_id`);
ALTER TABLE `people`
 ADD PRIMARY KEY (`id`);
ALTER TABLE `posts`
 ADD PRIMARY KEY (`id`), ADD KEY `author` (`author`);
ALTER TABLE `comments`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE `likes`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE `people`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE `comments`
ADD CONSTRAINT `comments_ibfk_1` FOREIGN KEY (`author`) REFERENCES `people` (`id`),
ADD CONSTRAINT `comments_ibfk_2` FOREIGN KEY (`on_post`) REFERENCES `posts` (`id`),
ADD CONSTRAINT `comments_ibfk_3` FOREIGN KEY (`reply_to`) REFERENCES `comments` (`id`);
ALTER TABLE `likes`
ADD CONSTRAINT `likes_ibfk_1` FOREIGN KEY (`by_id`) REFERENCES `people` (`id`),
ADD CONSTRAINT `likes_ibfk_2` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`),
ADD CONSTRAINT `likes_ibfk_3` FOREIGN KEY (`comment_id`) REFERENCES `comments` (`id`);
ALTER TABLE `posts`
ADD CONSTRAINT `posts_ibfk_1` FOREIGN KEY (`author`) REFERENCES `people` (`id`);
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

From comparing these schemas, you can already see that GraphQL has one advantage over the equivalent SQL schema syntax: brevity.