Have many `schema`s

I wanted to try out Node types and got this documentation https://docs.dgraph.io/howto/#giving-nodes-a-type

And ended up with this Python code that seems to work.

import pydgraph
import json

client_stub = pydgraph.DgraphClientStub('localhost:9080')
client = pydgraph.DgraphClient(client_stub)

# Create Schema
schema = """type: string @index(exact) .
            name: string ."""
op = pydgraph.Operation(schema=schema)
client.alter(op)

# Data
txn = client.txn()
p = {
    "type": "People",
    "name": "Alpha",
}
assigned = txn.mutate(set_obj=p)

q = {
    "type": "People",
    "name": "Beta",
}
assigned = txn.mutate(set_obj=q)
txn.commit()

# Fetch type people
# Fetch Data
query = """query all($a: string) {
    all(func: eq(type, $a)) {
        expand(_all_) {}
    }
}"""
variables = {'$a': 'People'}


# Print
res = client.query(query, variables=variables)
ppls = json.loads(res.json.decode('utf-8'))

for ppl in ppls["all"]:
    print(ppl)

But I want to have isolated/different Schema for different node types, with different properties. (ie. instead of one big schema with 1000 properties, have 100 separate schema with 10 properties each)

I saw this post, but didn’t find a working sample Dgraph schema system - #5 by ashwin95r

1 Like

I also have same question here

1 Like

This separation does not exist. Generally many nodes share the same predicate. E.g: Name.
If you want extraordinarily separate you can create specific predicates like:

name.user
name.post
name.board

your code is different from the one shown in the documentation. The documentation approach is about giving nodes types via unique predicates. And not a single predicate named “Type” indexed. This is another possible approach, but not specifically recommended.

In the case of the recommended approach your code should look like this:


p = {
    "_People": "*",
    "name": "Alpha",
}

And your query:

{
  q(func: has(_People)) {
  uid
  expand(_all_)
  }
}
1 Like

If i have more then 10 node types, then how we get the list of distinct node type?

Usually you create a pattern of Kinds. You can query for predicates like this:
schema {}

or use Ratel and filter by the pattern you used.

Let’s say that your Kinds pattern starts with has (type_person)
In Ratel you would write in the filter “type_” and a list of predicates would appear with

type_person
type_board
type_document

thank you, if this was documented in a tutorial it would have been much better

1 Like