Copyright ©
2022- basebox GmbH, all rights reserved.
Licensed to be used in conjunction with basebox, only.
Licensed to be used in conjunction with basebox, only.
@bb_primaryKey
Syntax
fieldname:
FieldType @bb_primaryKey
Notes
The @bb_primaryKey
directive specifies that a field in a GraphQL object serves as the primary key for the corresponding database table. A primary key is an identifier in a database table that allows someone to uniquely identify a record in a database table.
Note
If an object type has a single field of type ID
, the compiler will assume that this is the corresponding table's primary key. This will be created as an autogenerated UUID
column in the database. If you would like to create an alternative primary key to the ID
field, have a primary key of a different type (e.g. externalId: String!
in a type) or have multiple ID
fields in an object type then it is essential to use the @bb_primaryKey
directive.*
Examples
Externally generated primary key
In this example, the type Patient {
# The reference number refers to an externally generated reference number
referenceNumber: String @bb_primaryKey
name: String!
databaseId: ID!
}
Patient
object uses a reference number as the primary key for the corresponding table created in the database. We have also added a databaseId
that is of type ID
. This will be created as a UUID
field and be autogenerated by the database. It is however not the primary key here.
In the above example, the object type has two ID
fields. The compiler cannot assume which one is the primary key so the @bb_primaryKey
directive has to be specified.