@id directive and interfaces

I am planning my schema and here is something I didn’t understand from the documentation
if I used the @id directive inside an interface, does this enforce uniqueness of that value across all types that use that interface?

Example

interface Named {
    name: String! @id @search(by: [hash, regexp])
}

type Person implements Named {
  textA: String
  textB: String
}

type Company implements Named {
  textC: String
  textD: String 
}

with this given schema Dgraph generates the predicate Named.name type String index/upsert
which is used in both the Type Person and Company

does this prevent me from storing a Person with name Mercedes at the same time as a Company called Mercedes

relevant section from the documentation https://dgraph.io/docs/graphql/schema/types/#interfaces

every hint is welcome,
thanks

1 Like

Hi @jackob, no this doesn’t prevent you from having the same name in Person and Company. Although two different types are inheriting the same interface, there is no link between objects of these two types. You can think of it as two different types that have the same field name.

ok, thanks for confirming that, I was unclear because the schema also generates a Query called

getNamed(
name: String!
)

and the get queries are supposed to be about finding a single object
https://dgraph.io/docs/graphql/queries/search-filtering/#get-a-single-object

I don’t have to use that query, just was surprised that an interface does generate queries

for completeness to those who stumbled on this thread
if both Person and Company Mercedes exists a call on the interface query like this

query{
  getNamed(name: "Mercedes"){
    name    
  }
}

returns data and a helpful error

{
  "errors": [
    {
      "message": "Dgraph returned a list, but getNamed (type Named) was expecting just one item.  The first item in the list was used to produce the result.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ]
    }
  ],
  "data": {
    "getNamed": {
      "name": "Mercedes"
    }
  },
1 Like

Yeah, looks like a bug to me also. Creating a ticket for it and we will get back to you.