This repo serves as a quick start for spinning up a Dgraph cluster, updating a schema and loading data. Everything's
done with make, the only other requirement is Docker and optionally jq and gql.
- Docker
- make
- curl (optional, for queries from the command line)
- gql (optional, for graphql queries, download from here)
- jq (optional, for queries from the command line)
This branch illustrates converting CSV data to JSON data. Check out the data.csv file. The first two records represent the same person, John. The third is a new entity, Mary.
Have a look at the GraphQL schema: schema.graphql.
I manually created the JSON-formatted, Dgraph-compatible import file gql-data.json for this example, but you could use any CSV toolkit along with your prog lang of your choice to create this file from your CSV file.
In gql-data.json, take note of the "dgraph.type" and "uid" fields. The "dgraph.type" binds the record to the type in your schema. The "uid" field allows the live loader to map that value with other records. For instance, I could have duplicated the "_:John" record into two records as illustrated in this file, each with a single "Person.phones" entry, and the results would still be the same in the graph.
make up
then in a new terminal window...
make gql-schemamake load-gql-datamake query-gql
-
Clone this repo. It's possible I've created a branch for some issue we're collaborating on. If so, check out the branch for the issue.
-
Spin up the cluster
make up
To spin one up with lambda support: make up-with-lambda.
- Then in another terminal, load the schema
make schema-dql
or make schema-gql for an SDL-based one.
Lists all available make targets and a short description.
Brings up a simple alpha and zero node using docker compose in your local Docker environment.
Brings up the alpha and zero containers along with the dgraph lambda container. Note this lambda container is based on dgraph/dgraph-lambda:1.4.0.
Stops the containers.
Updates dgraph with the schema defined in schema.dql.
Example schema.dql:
type Person {
name
boss_of
works_for
}
type Company {
name
industry
work_here
}
industry: string @index(term) .
boss_of: [uid] .
name: string @index(exact, term) .
work_here: [uid] .
boss_of: [uid] @reverse .
works_for: [uid] @reverse .
Updates dgraph with the schema defined in schema.graphql
Example schema.gql:
type Post {
id: ID!
title: String!
text: String
datePublished: DateTime
author: Author!
}
type Author {
id: ID!
name: String!
posts: [Post!] @hasInverse(field: author)
}Drops all data from the cluster, but not the schema.
Drops all data and the schema from the cluster.
Loads JSON data defined in gql-data.json. This target is useful for loading data into schemas defined with GraphQL SDL.
Example gql-data.json:
[
{
"uid": "_:katie_howgate",
"dgraph.type": "Author",
"Author.name": "Katie Howgate",
"Author.posts": [
{
"uid": "_:katie_howgate_1"
},
{
"uid": "_:katie_howgate_2"
}
]
},
{
"uid": "_:timo_denk",
"dgraph.type": "Author",
"Author.name": "Timo Denk",
"Author.posts": [
{
"uid": "_:timo_denk_1"
},
{
"uid": "_:timo_denk_2"
}
]
},
{
"uid": "_:katie_howgate_1",
"dgraph.type": "Post",
"Post.title": "Graph Theory 101",
"Post.text": "https://www.lancaster.ac.uk/stor-i-student-sites/katie-howgate/2021/04/27/graph-theory-101/",
"Post.datePublished": "2021-04-27",
"Post.author": {
"uid": "_:katie_howgate"
}
},
{
"uid": "_:katie_howgate_2",
"dgraph.type": "Post",
"Post.title": "Hypergraphs – not just a cool name!",
"Post.text": "https://www.lancaster.ac.uk/stor-i-student-sites/katie-howgate/2021/04/29/hypergraphs-not-just-a-cool-name/",
"Post.datePublished": "2021-04-29",
"Post.author": {
"uid": "_:katie_howgate"
}
},
{
"uid": "_:timo_denk_1",
"dgraph.type": "Post",
"Post.title": "Polynomial-time Approximation Schemes",
"Post.text": "https://timodenk.com/blog/ptas/",
"Post.datePublished": "2019-04-12",
"Post.author": {
"uid": "_:timo_denk"
}
},
{
"uid": "_:timo_denk_2",
"dgraph.type": "Post",
"Post.title": "Graph Theory Overview",
"Post.text": "https://timodenk.com/blog/graph-theory-overview/",
"Post.datePublished": "2017-08-03",
"Post.author": {
"uid": "_:timo_denk"
}
}
]Loads JSON data defined in dql-data.json. This target is useful for loading data into schemas defined with base dgraph types.
Example dql-data.json:
{
"set": [
{
"uid": "_:company1",
"industry": "Machinery",
"dgraph.type": "Company",
"name": "CompanyABC"
},
{
"uid": "_:company2",
"industry": "High Tech",
"dgraph.type": "Company",
"name": "The other company"
},
{
"uid": "_:jack",
"works_for": { "uid": "_:company1"},
"dgraph.type": "Person",
"name": "Jack"
},
{
"uid": "_:ivy",
"works_for": { "uid": "_:company1"},
"boss_of": { "uid": "_:jack"},
"dgraph.type": "Person",
"name": "Ivy"
},
{
"uid": "_:zoe",
"works_for": { "uid": "_:company1"},
"dgraph.type": "Person",
"name": "Zoe"
},
{
"uid": "_:jose",
"works_for": { "uid": "_:company2"},
"dgraph.type": "Person",
"name": "Jose"
},
{
"uid": "_:alexei",
"works_for": { "uid": "_:company2"},
"boss_of": { "uid": "_:jose"},
"dgraph.type": "Person",
"name": "Alexei"
}
]
}Loads RDF data defined in dql-data.rdf. This target is useful for loading data into schemas defined with base dgraph types.
Example dql-data.rdf:
{
set {
_:company1 <name> "CompanyABC" .
_:company1 <dgraph.type> "Company" .
_:company2 <name> "The other company" .
_:company2 <dgraph.type> "Company" .
_:company1 <industry> "Machinery" .
_:company2 <industry> "High Tech" .
_:jack <works_for> _:company1 .
_:jack <dgraph.type> "Person" .
_:ivy <works_for> _:company1 .
_:ivy <dgraph.type> "Person" .
_:zoe <works_for> _:company1 .
_:zoe <dgraph.type> "Person" .
_:jack <name> "Jack" .
_:ivy <name> "Ivy" .
_:zoe <name> "Zoe" .
_:jose <name> "Jose" .
_:alexei <name> "Alexei" .
_:jose <works_for> _:company2 .
_:jose <dgraph.type> "Person" .
_:alexei <works_for> _:company2 .
_:alexei <dgraph.type> "Person" .
_:ivy <boss_of> _:jack .
_:alexei <boss_of> _:jose .
}
}Runs the query defined in query.dql.
Example query.dql:
{
q(func: eq(name, "CompanyABC")) {
name
works_here : ~works_for {
uid
name
}
}
}
Runs the query defined in query.gql and optional variables defined in variables.json.
Example query-gql:
query QueryAuthor($order: PostOrder) {
queryAuthor {
id
name
posts(order: $order) {
id
datePublished
title
text
}
}
}Example variables.json:
{
"order": {
"desc": "datePublished"
}
}