Data Model
Graphback generates a GraphQL API and client-side documents using your data models. The data models are represented in the GraphQL Schema language as GraphQL types.
In this section you will learn how to design and configure your data models for use with Graphback and how they can be used for multiple datasources.
#
ModelGraphback processes GraphQL Schema types annotated with @model
.
Adding this annotation to your type will generate CRUD resolvers, services, data providers and client queries following the GraphQLCRUD format. For the full annotation definition see @model
.
#
Primary keyGraphback requires each data model to have one primary key field which is used to uniquely represent every object in the database.
#
PostgreSQLFor PostgreSQL, the default primary key format is id: ID!
.
You can also use the @id
annotation to set a custom primary field.
#
MongoDBIf you are using MongoDB you should use _id: ObjectID
, as _id
in MongoDB documents is reserved for the primary key.
#
RelationshipsGraphback provides support for one-to-many, one-to-one and many-to-many relationships.
#
OneToManyThis creates a one-to-many relationship from Note.comments
to Comment.note
. If Comment.note
does not exist Graphback will create it for you, otherwise you can define it yourself.
For more on creating one-to-many relationships see the @oneToMany
annotation reference guide.
#
OneToOneThis creates a one-sided relationship between the Profile
and User
models.
For more on creating one-to-one relationships see the @oneToOne
annotation reference guide.
#
ManyToManyTo create a many-to-many relationship, add a model for your join table and use two one-to-many relationships.
For more on creating many-to-many relationships see the @oneToMany
annotation reference guide.
#
Type fieldsYou can use GraphQL types to get strongly-typed fields which maps to a JSON column in PostgreSQL or an embedded/nested document in MongoDB.
NOTE: The
@db(type: 'json')
annotation is only required for PostgreSQL.