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

Commit 6831437

Browse files
MichelDizbucaneroabhimanyusinghgaurrderbier
authored
docs(graphql): Update id directive for interfaces (#209) (#382)
Fixes DOC-275 Co-authored-by: Damián Parrino <[email protected]> Co-authored-by: Abhimanyu Singh Gaur <[email protected]> Co-authored-by: rderbier <[email protected]>
1 parent 9353342 commit 6831437

File tree

1 file changed

+28
-3
lines changed
  • content/graphql/schema/directives

1 file changed

+28
-3
lines changed

content/graphql/schema/directives/ids.md

Lines changed: 28 additions & 3 deletions
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,8 +63,33 @@ 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
6667

67-
### Combining `ID` and `@id`
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+
92+
## Combining `ID` and `@id`
6893

6994
You can use both the `ID` type and the `@id` directive on another field definition to have both a unique identifier and a generated identifier.
7095

@@ -84,7 +109,7 @@ With this schema, Dgraph requires a unique `username` when creating a new user.
84109
If in a type there are multiple `@id` fields, then in a `get` query these arguments will be optional. If in a type there's only one field defined with either `@id` or `ID`, then that will be a required field in the `get` query's arguments.
85110
{{% /notice %}}
86111

87-
<!--
112+
<!--
88113
### More to come
89114
90115
We are currently considering allowing types other than `String` with `@id`, see [here](https://discuss.dgraph.io/t/id-with-type-int/10402)

0 commit comments

Comments
 (0)