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 namedUser
. In GraphQL, a type represents a data structure, and in this case, it represents a user.id: ID!
:id
is a field within theUser
type.ID!
indicates that theid
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 theUser
type, and it has square brackets[...]
aroundPost!
. This indicates thatposts
is an array ofPost
objects.The exclamation mark after
Post!
signifies that the array itself and each element inside it are non-nullable.
posts
is a field within theUser
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
withid: "123"
has two posts (post1
andpost2
). Each post is an object with its own set of fields (id
,title
, andcontent
). 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
: Representstrue
orfalse
.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, andauthor
is a non-nullableAuthor
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
๐ค.