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
Published 2 years ago
Updated 2 years ago

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.

egghead
egghead
~ 2 hours ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today