Basic components of GraphQL — Schema , Queries

Soumitya Chauhan
2 min readJun 20, 2023

--

Previous story [click on me]

Basic components of GraphQL —
1. Schema
2. Query

Schema —

schema defines the structure, types, and relationships of the data available in your GraphQL API. It acts as a contract between the server and the clients, specifying what operations can be performed and what data can be accessed.

A GraphQL schema consists of two main components: types and fields.

Queries —
Queries allow clients to fetch data from the API. They are read-only operations and do not modify any data. For example, a client can send a query to retrieve a user’s details.


type Query {
book(id: ID!): Book
allBooks: [Book!]!
}

type Book {
id: ID!
title: String!
publicationYear: Int!
author: Author
}

type Author {
id: ID
firstName: String
lastName: String
}

example —
In this example, we have two main types: “Book” and “Query”.

The “Book” type represents a book object with the following fields:

  • id (of type ID): An identifier for the book.
  • title (of type String): The title of the book.
  • author (of type Author): the relationship between a book and its author. It references the Author type.
  • publicationYear (of type Int): The year the book was published.

Author: Represents an author object with the following fields:

  • id (of type ID): An identifier for the author.
  • firstName (of type String): The first name of the author.
  • lastName (of type String): The last name of the author.

The “Query” type represents the available queries in our API. It includes the following fields:

  • book (of type Book): A query that takes an id argument and returns a single book based on the provided ID.
  • allBooks (of type [Book!]!): A query that returns a list of all books.

send queries —
clients can send queries to retrieve specific books by ID or fetch all the books available in the API.
For example, a client could send the following GraphQL query:
Query-1:

query {
book(id: "123") {
title
publicationYear
}
}

asks for the title, and publication year of a book with the ID “123”.
and Server will then respond with the corresponding book object:

{ "data": { 
"book": {
"title": "Sample Book",
"publicationYear": 2020
}
}
}

Query-2:

query {
book(id: "123") {
title
author {
firstName
}
publicationYear
}
}

the query asks for the title, author (including the firstName and lastName), and publicationYear fields of the book with the ID "123".
server response will be

{
"data": {
"book": {
"title": "Sample Book",
"author": {
"firstName": "John",
},
"publicationYear": 2021
}
}
}

Query-3

query {
book(id: "123") {
title
publicationYear
}
allBooks {
title
author
publicationYear
}
}

The server’s response could be structured as follows:

{
"data": {
"book": {
"title": "Sample Book",
"author": {
"firstName": "John",
"lastName": "Doe"
},
"publicationYear": 2021
},
"allBooks": [
{
"title": "Book 1",
"author": {
"firstName": "Jane",
"lastName": "Smith"
},
"publicationYear": 2022
},
{
"title": "Book 2",
"author": {
"firstName": "Mark",
"lastName": "Johnson"
},
"publicationYear": 2023
},
{
"title": "Book 3",
"author": {
"firstName": "Sarah",
"lastName": "Williams"
},
"publicationYear": 2021
}
]
}
}

--

--