1. 2
    Model our Application Data with a GraphQL Schema
    1m 59s

Model our Application Data with a GraphQL Schema

Shadid Haque
InstructorShadid Haque
Share this video with your friends

Social Share Links

Send Tweet

The next step is to model the data that we are going to use for our application, we'll have a Shop and Product. Shops will 'have many' Products.

You'll see how to type this in a GraphQL schema with the different properties we want on Shop and Product. Additionally we will be introduced to the faunadb @relation directive which is similar to a foreign key constraint.

Read more about the @relation directive in the docs.

# schema.graphql
type Shop {
  name: String!
  description: String!
  coverImg: String!
  products: [Product]!
  ownerID: String!
}

type Product {
  name: String!
  description: String!
  price: Float!
  category: String!
  shop: Shop! @relation
}

Shadid Haque: [0:00] Let's go ahead and take a look at the data model of our application. Our application will have many shops. Each of these shops will have many products. Both shops and products are going to be collections in our database. Essentially, there is a has_many relationship between a shop and its products.

[0:18] Let's turn this into a GraphQL schema. I'm going to create a new file called schema.graphql. We're going to create a new type called Shop. Shop will have a name which is a string type, a description also a string, a coverImg another string type, and products which is an array of Product. We haven't created the product type yet, but we're going to create that next, and ownerID which is also a string.

[0:48] Next, let's go ahead and create the product type. Product type also will have a name which is a string, then a description also a string, a price which is a float, and category which is also a string, and shop which is a shop type.

[1:07] Notice we have a @relation directive here. This will define the relationship between Shop and Product collections. The @relation directive is unique to Fauna. It establishes a one-to-many relationship between two models.

[1:21] If you have prior knowledge of any SQL-based database, then you might be familiar with this concept of creating relationship between two tables through a Foreign Key constraint. With this @relation directive, we pretty much do the same thing.

[1:35] Although Fauna is not a SQL-based database, it does certainly take a lot of inspiration from SQL-based databases when it comes to data modeling. We can learn more about the @relation directive at docs.fauna.com.