Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(graphql): Update id directive for interfaces (#209) #382

Merged
merged 3 commits into from
Jul 26, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions content/graphql/schema/directives/ids.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Dgraph provides two types of built-in identifiers: the `ID` scalar type and the
* The `@id` directive is used for external identifiers, such as email addresses.


### The `@id` directive
## The `@id` directive

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

Expand Down Expand Up @@ -63,8 +63,33 @@ query {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rderbier told me that this feature does not work on v23. Do I understand it correctly?

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

### `@id` and interfaces

### Combining `ID` and `@id`
By default, if used in an interface, the `@id` directive will ensure field uniqueness for each implementing type separately.
In this case, the `@id` field in the interface won't be unique for the interface but for each of its implementing types.
This allows two different types implementing the same interface to have the same value for the inherited `@id` field.

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.
rderbier marked this conversation as resolved.
Show resolved Hide resolved

For example:

```graphql
interface Item {
refID: Int! @id(interface: true) # if there is a Book with refID = 1, then there can't be a chair with that refID.
itemID: Int! @id # If there is a Book with itemID = 1, there can still be a Chair with the same itemID.
}

type Book implements Item { ... }
type Chair implements Item { ... }
```

In the above example, `itemID` won't be present as an argument to the `getItem` query as it might return more than one `Item`.

{{% notice "note" %}}
`get` queries generated for an interface will have only the `@id(interface: true)` fields as arguments.
{{% /notice %}}

## Combining `ID` and `@id`

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.

Expand All @@ -84,7 +109,7 @@ With this schema, Dgraph requires a unique `username` when creating a new user.
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.
{{% /notice %}}

<!--
<!--
### More to come

We are currently considering allowing types other than `String` with `@id`, see [here](https://discuss.dgraph.io/t/id-with-type-int/10402)
Expand Down