Skip to content
This repository was archived by the owner on Mar 29, 2025. It is now read-only.

Commit 2243f8d

Browse files
committed
Update ids.md
1 parent a7e1d06 commit 2243f8d

File tree

1 file changed

+27
-1
lines changed
  • content/graphql/schema/directives

1 file changed

+27
-1
lines changed

content/graphql/schema/directives/ids.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Dgraph provides two types of built-in identifiers: the `ID` scalar type and the
1313
* The `@id` directive is used for external identifiers, such as email addresses.
1414

1515

16-
### The `@id` directive
16+
## The `@id` directive
1717

1818
For some types, you'll need a unique identifier set from outside Dgraph. A common example is a username.
1919

@@ -63,6 +63,32 @@ query {
6363

6464
This will yield a positive response if both the `name` **and** `isbn` match any data in the database.
6565

66+
### `@id` and interfaces
67+
68+
By default, if used in an interface, the `@id` directive will ensure field uniqueness for each implementing type separately.
69+
In this case, the `@id` field in the interface won't be unique for the interface but for each of its implementing types.
70+
This allows two different types implementing the same interface to have the same value for the inherited `@id` field.
71+
72+
There are scenarios where this behavior might not be desired, and you may want to constrain the `@id` field to be unique across all the implementing types. In that case, you can set the `interface` argument of the `@id` directive to `true`, and Dgraph will ensure that the field has unique values across all the implementing types of an interface.
73+
74+
For example:
75+
76+
```graphql
77+
interface Item {
78+
refID: Int! @id(interface: true) # if there is a Book with refID = 1, then there can't be a chair with that refID.
79+
itemID: Int! @id # If there is a Book with itemID = 1, there can still be a Chair with the same itemID.
80+
}
81+
82+
type Book implements Item { ... }
83+
type Chair implements Item { ... }
84+
```
85+
86+
In the above example, `itemID` won't be present as an argument to the `getItem` query as it might return more than one `Item`.
87+
88+
{{% notice "note" %}}
89+
`get` queries generated for an interface will have only the `@id(interface: true)` fields as arguments.
90+
{{% /notice %}}
91+
6692

6793
### Combining `ID` and `@id`
6894

0 commit comments

Comments
 (0)