Using a GraphQL Schema + Live Loader to ingest data

After spending more time searching through this forum and the docs I was finally able to figure out a way to use Live Loader in combination with a GraphQL-based schema.

As @pshaddel pointed out in How do you go from GraphQL to RDF for bulk loading? you can specify the type and predicate names in the GraphQL Schema.

See the documentation here: https://dgraph.io/docs/graphql/dgraph/#mapping-graphql-to-a-dgraph-schema

Based on that I came up with the following solution.

The GraphQL Schema:

type Person {
  id: ID!
  xid: String! @id @dgraph(pred: "xid")
  slug: String! @id
  firstName: String!
  lastName: String!
  posts: [Post] @hasInverse(field: author)
}

type Post {
  id: ID!
  xid: String! @id @dgraph(pred: "xid")
  slug: String! @id
  author: Person
}

The .rdf file:

<person/jdoe> <Person.slug> "jdoe" .
<person/jdoe> <Person.firstName> "John" .
<person/jdoe> <Person.lastName> "Doe" .
<person/jdoe> <dgraph.type> "Person" .
<person/jdoe> <xid> "person/jdoe" .

<post/post-1> <Post.slug> "post-1" .
<post/post-1> <dgraph.type> "Post" .
<post/post-1> <xid> "post/post-1" .

<post/post-1> <Post.author> <person/jdoe> .

If I now run dgraph live --files data.rdf --upsertPredicate "xid" everything gets inserted accordingly. Running the command multiple times doesn’t insert the data multiple times (given that we’re using the xid to uniquely identify it).

TBH I’m not entirely sure why this works (especially why I have to use the xid as a Node’s subject like <post/post-1>) but it does what it should do.

Would be awesome if someone from the Dgraph team could chime in if this is the best practice to accomplish this task. Thanks in advance!

1 Like