Type-defined predicates seem to accept all types of UIDs, not just the defined type

What I want to do

I have a schema similar to this:

User.departments: [uid] @reverse .
type User {
    User.departments: [Department]
}

type Department {
    <~User.departments>: [User]
}

I want to strictly type the UIDs entered into User.departments so that it only accepts nodes of type Department. However, upon trying to test the schema, I am able to put all types of nodes into the User.departments predicate (i.e., nodes which are not of the Department type).

What I did

Example using JSON mutation format:

  {
    uid: '0x1',
    'dgraph.type': ['Department'],
  },
  {
    uid: '0x2',
    'dgraph.type': ['DefinitelyNotADepartment'],
  },
  {
    uid: '_:user',
    'User.departments': [
      {
        uid: '0x1',
      },
      {
        uid: '0x2',
      },
    ],
    'dgraph.type': ['User'],
  }

Dgraph accepts these values, and when queried, it returns:

{ uid: '0xd', departments: [ { uid: '0x1' }, { uid: '0x2' } ] }

Is this natural behavior? Should I instead implement a function to verify the type before adding it to the predicate? Thank you very much!

Dgraph metadata

dgraph version

Dgraph version : v21.12.0
Dgraph codename : zion
Dgraph SHA-256 : 078c75df9fa1057447c8c8afc10ea57cb0a29dfb22f9e61d8c334882b4b4eb37
Commit SHA-1 : d62ed5f15
Commit timestamp : 2021-12-02 21:20:09 +0530
Branch : HEAD
Go version : go1.17.3
jemalloc enabled : true

1 Like

If you had what you wanted then GraphQL interfaces would not work.

This is all by design.

1 Like

Thank you for the reply! I would be implementing a function to verify the types instead. Thanks again!

Thats basically what Dgraph’s GraphQL does, it verifies the types of what you are connecting in relationships. In DQL though it is all very loosely typed and allows you to do weord relationships like you are doing/seeing.

1 Like