DgraphQL: Build a GraphQL service from a schema

npm Documentation Travis Codecov


Dgraph is a distributed, highly available graph database that uses a language similar to GraphQL to query and mutate data. Unlike GraphQL, Dgraph only defines schema for predicates (properties) within the graph; there is no concept of complex types or groups of properties. Because of this it is straight forward to store any GraphQL schema in Dgraph provided a few restrictions are met.

Given a GraphQL schema, DgraphQL can do four things:

  1. Generate a GraphQL-JS schema that maps GraphQL queries to Dgraph queries
  2. Transform Dgraph responses into GraphQL responses (including support for the relay connection specification)
  3. Generate defaults for create/update/delete/query operations (with filtering, ordering and nested create/update mutations)
  4. Configure Dgraph’s schema with types and indexes each property.

Check out the complete documentation for more.

Getting Started

The example describes basic usage. First, install dependencies:

yarn install

The example and expects a Dgraph instance that you don’t mind filling with junk running at http://localhost:8080. You can either install Dgraph or (much better) run it in Docker:

Install the Dgraph Docker image:

docker pull dgraph/dgraph

And run Dgraph:

yarn run dgraph

Run the example (in another terminal):

yarn start

Or run the test suite:

yarn test

Using DgraphQL

Install DgraphQL from npm

With yarn:

yarn add dgraphql

The entry point to the library is buildSchema

import { graphql } from 'graphql'
import { Client } from 'dgraphql'

const schema = `
type Person {
  id: ID!
  name: String @filter(types: [EQUALITY])
  children: [Person!]! @reverse(name: "parents")
  parents: [Person!]! @reverse(name: "children")

const client = new Client({
  server: 'http://localhost:8080/query',
  schema: schema,
  debug: false

const mutation = `
mutation {
  createPerson(input: { name: "David" }) {
    person {

  schema: client.schema,
  source: mutation,
  contextValue: client.getContext()
}).then(result => {
  console.log(JSON.stringify(result, null, '  '))