In the previous post, we have seen how to check null string using @notEmpty directive
In this post, we will see how to insert only unique values to a field using Hypi directive.
Again a brief info on Hypi Directives:
- Hypi directives help to customise the behaviour of the data fields.
-
@
character depicts the Hypi directive. It is followed by a series of characters. - A directive may have a list of optional named arguments.
- Disable directive by adding
#
in front of it.
Let’s check the @unique directive!
strUnique: String @unique
- Use the
@unique
directive to ensure no duplicate values in a field. - While creating an object, no previously inserted values are allowed in a field.
- It returns an error if a repeated value is inserted in a field while creating a new object
We will work with the below schema.
type DirectiveObj {
strUnique: String @unique
}
Sample Query
Let’s see how the @unique
directive works. We have already inserted the value in the strUnique
field. Again adding the same value - Hypi
in the same field results in an error.
mutation {
upsert(values: { DirectiveObj: { strUnique: "Hypi" } }) {
id
}
}
#result
{
"data": {
"upsert": [
{
"id": "01FNBDPVC4GHK67KP0CEJRT1J2"
}
]
},
"errors": [
{
"message": "'DirectiveObj.strUnique' already exists with a value of Hypi",
"extensions": {}
}
]
}
Check the POSTMAN collection for the @unique directive
in different programming languages! Click </>
and choose the programming language of your choice.
Don’t forget to insert your own Authorization key and Hypi Domain under Headers to test the results!