Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dpeek/dgraphql

DgraphQL: Build a GraphQL service from a schema
https://github.com/dpeek/dgraphql

dgraph graphql

Last synced: about 1 month ago
JSON representation

DgraphQL: Build a GraphQL service from a schema

Awesome Lists containing this project

README

        

# DgraphQL: Build a GraphQL service from a schema

[![npm](https://img.shields.io/npm/v/dgraphql.svg)](https://www.npmjs.com/package/dgraphql)
[![Documentation](https://img.shields.io/badge/support-docs-blue.svg)](http://dpeek.com/dgraphql/)
[![Travis](https://img.shields.io/travis/dpeek/dgraphql.svg)](https://travis-ci.org/dpeek/dgraphql)
[![Codecov](https://img.shields.io/codecov/c/github/dpeek/dgraphql.svg)](https://codecov.io/gh/dpeek/dgraphql)

## Introduction

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](http://dpeek.com/dgraphql/) for more.

## Getting Started

The [example](https://github.com/dpeek/dgraphql/tree/master/example) describes
basic usage. First, install dependencies:

```sh
yarn install
```

The example and test suite expect a Dgraph instance that you don't mind filling
with junk running at . You can either [install Dgraph](https://docs.dgraph.io/v0.7.7/get-started#system-installation)
or, better yet, run it in Docker:

```sh
yarn run dgraph:start
```

Run the example:

```sh
yarn start
```

Or run the test suite:

```sh
yarn test
```

To stop the containers:

```sh
yarn run dgraph:stop
```

## Using DgraphQL

Install DgraphQL from npm

With yarn:

```sh
yarn add dgraphql
```

The entry point to the library is `Client`

```javascript
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, ' '))
})
})
```