GraphQL basic Schema Design

let’s take an example to understand the basics of graphQL Schema design

Example:

type Book {

id: ID!

title: String!

author: String!

}

type Query {

books: [Book!]!

book(id: ID!): Book

}

type Mutation {

createBook(title: String!, author: String!): Book

updateBook(id: ID!, title: String, author: String): Book

deleteBook(id: ID!): Boolean

}

Explanation:

We have defined three main types:

  • Book
  • Query
  • Mutation

Book Type:

The Book type represents a book object with three fields:

  • id: An ID field, which is non-nullable (! indicates that it cannot be null).
  • title: A String field, which is non-nullable.
  • author: A String field, which is non-nullable.

Query Type:

It defines two query fields:

  • books: Returns a list of Book objects, ! indicates a non-null list of non-null Book objects.
  • book(id: ID!): Takes an id argument and returns a single Book object.

Mutation Type:

It defines three mutation fields:

  • createBook: Creates a new book with title and author arguments and returns a Book.
  • updateBook: Updates an existing book with id, title, and author arguments and returns a Book.
  • deleteBook: Deletes a book with the given id and returns a Boolean indicating success.

What is GraphQL?

Client and server communication follows a traditional approach of REST APIs which is good but nowadays applications have become more powerful and complex which requires a more flexible approach rather than REST, so here GraphQL comes into the picture. In this article, we will go through all the concepts of graphQL.

Similar Reads

What is GraphQL?

GraphQL is an open-source data query language for APIs and It is a server-side runtime for executing the query. The server’s GraphQL runtime takes care of executing the query and ensuring that the right data is fetched and sent back....

Key Features of GraphQL

GraphQL has several features that set it apart from traditional REST APIs, offering developers a more flexible and efficient way to manage data. Let’s explore these features as follows:...

Key Components of GraphQL

For Better Understanding of GraphQL, let’s see some key components of GraphQL which are as follows:...

GraphQL basic Schema Design

let’s take an example to understand the basics of graphQL Schema design...

Conclusion

GraphQL is a powerful and flexible data query language for APIs. It is offering a more efficient alternative to traditional REST APIs. It allows clients to request exactly the data they need and reducing network overhead. It provides features like strong typing, real-time updates, and a single endpoint....