What is a GraphQL Schema?

What is a GraphQL Schema?

ยท

4 min read

In my previous Article. I talked about the basic understanding of Graph QL and why we need it. In the article we are going to understanding about schema and its first thing that is "type" in Graph QL schema. But before moving to let's understand

What is a GraphQL Schema?

A GraphQL schema serves as a contract between the client and the server, specifying how the data can be queried, what types of data are available, and how different types relate to each other.

Components of a GraphQL Schema:

1. Types:

2. Queries:

3. Mutations:

4. Subscriptions:

lets understand the 1 one in deep

Types are the building blocks of a GraphQL schema. These can represent entities like 'User,' 'Post,' or 'Comment.' Each type defines a set of fields, and the relationships between types are established through these fields.

type User {
  id: ID!
  name: String!
  email: String!
  posts: [Post!]!
}

type Post {
  id: ID!
  title: String!
  content: String!
  author: User!
  comments: [Comment!]!
}

type Comment {
  id: ID!
  text: String!
  author: User!
}
  • type User {:
    This line begins the definition of the GraphQL type named User. In GraphQL, a type represents a data structure, and in this case, it represents a user.

  • id: ID!:

  • id is a field within the User type.

  • ID! indicates that the id field is of type ID and is non-nullable (the exclamation mark ! denotes non-nullable). An ID is often used to uniquely identify an object.

  • posts: [Post!]!:

    • posts is a field within the User type, and it has square brackets [...] around Post!. This indicates that posts is an array of Post objects.

    • The exclamation mark after Post! signifies that the array itself and each element inside it are non-nullable.

  • posts is a field within the User type. It represents a relationship between a user and their posts.

  • Understanding the Relationship:

    The posts field establishes a one-to-many relationship between a user and their posts. This means that one user can have multiple posts, and each post belongs to one specific user.

  •           {
                id: "123",
                name: "John Doe",
                email: "john@example.com",
                posts: [
                  { id: "post1", title: "GraphQL Basics", content: "An introduction to GraphQL." },
                  { id: "post2", title: "GraphQL Best Practices", content: "Tips for using GraphQL effectively." }
                ]
              }
    

    Here, the User with id: "123" has two posts (post1 and post2). Each post is an object with its own set of fields (id, title, and content). This structure allows for a clear representation of the relationship between a user and their posts within the GraphQL schema.

    Lets create a simple schema file which contain the "type" and we will be exporting that file

export const typeDefs =`#graphql
  type Game {
    id:ID!
    title:String!
    platform:[String!]!
    reviews:[Review!]
  }
}`

// typeDefs Name of the defination(You can give any)

More about Graph QL type

1. Scalar Types:

  • Definition: Scalar types represent atomic values and are the most basic building blocks of a GraphQL schema.

  • Examples:

    • Int: Represents a signed 32-bit integer.

    • Float: Represents a signed double-precision floating-point value.

    • String: Represents a sequence of characters.

    • Boolean: Represents true or false.

    • ID: Represents a unique identifier, often used for fetching an object or as a key.

      2. Object Types:

      • Definition: Object types represent a complex set of fields and may have relationships with other object types. They are used to model entities.

          type Person {
            id: ID!
            name: String!
            age: Int!
          }
        

        Certainly! Let's delve deeper into GraphQL types:

        3. List Types:

        • Definition: Lists represent arrays of a particular type. They are used when a field can have multiple values.

            type Team {
              name: String!
              members: [Person!]!
            }
          

          4. NonNull Types:

          • Definition: The exclamation mark (!) is used to mark a type as non-nullable, indicating that a field must always have a value.

              type Book {
                title: String!
                author: Author!
              }
            

            title is a non-nullable string, and author is a non-nullable Author object.

In the upcoming Article we will be going to learn how we can implement the Schema (Query by user) in Graph QL.

I Hope you got something from my Article To know more about GraphQL follow the Link

Thank you for reading my content. Be sure to follow and comment on what you want me to write about next ๐Ÿค“.

Did you find this article valuable?

Support Saurabh verma by becoming a sponsor. Any amount is appreciated!

ย