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:
- Generate a GraphQL-JS schema that maps GraphQL queries to Dgraph queries
- Transform Dgraph responses into GraphQL responses (including support for the relay connection specification)
- Generate defaults for create/update/delete/query operations (with filtering, ordering and nested create/update mutations)
- Configure Dgraph's schema with types and indexes each property.
Check out the complete documentation for more.
The example describes basic usage. First, install dependencies:
yarn install
The example and test suite expect a Dgraph instance that you don't mind filling with junk running at http://localhost:8080. You can either install Dgraph or, better yet, run it in Docker:
yarn run dgraph:start
Run the example:
yarn start
Or run the test suite:
yarn test
To stop the containers:
yarn run dgraph:stop
Install DgraphQL from npm
With yarn:
yarn add dgraphql
The entry point to the library is Client
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 mutation = `
mutation {
createPerson(input: { name: "David" }) {
person {
id
name
}
}
}`
const client = new Client({ debug: false })
client.updateSchema(schema).then(() => {
graphql({
schema: client.schema,
source: mutation,
contextValue: client.getContext()
}).then(result => {
console.log(JSON.stringify(result, null, ' '))
})
})