@unique directive

Specifies the name of a Fauna index that enforces a uniqueness constraint.

Schema location

This directive is valid for field declarations in a Type declaration within a GraphQL schema.

Arguments

Argument Type Required Description

index

String

no

The name for the unique index to use.

Description

The @unique directive marks a field as being unique. Unique fields have an associated unique index within the database, and it is the index that performs the uniqueness constraint.

When the associated index does not exist, the GraphQL API automatically creates one. The index argument controls the name of the unique index. By default, the index name uses this template: unique_<type name>_<field name>.

When your @unique directive specifies the name of an existing index, if the index definition does not match your type, your queries may result in errors due to the shape of your type being incompatible with the index definition.

Example

When the following GraphQL schema has been imported:

type User {
  name: String! @unique(index: "unique_username")
}

The database contains the following index:

Get(Index("unique_username"))
{ ref: Index("unique_username"),
  ts: 1560525943310000,
  active: true,
  partitions: 1,
  name: 'unique_username',
  source: Collection("User"),
  data: { gql: { ts: Time("2019-06-14T15:25:43.226352Z") } },
  terms: [ { field: [ 'data', 'name' ] } ],
  unique: true }

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!