Graphql
- Created by Facebook in 2012 and open-sourced in 2015.
- Graph query language is used for communicating with a server to retrieve data.
- Offers strong data typing, enabling better management of missing data and data overload.
Coexistence with Existing Methods
- GraphQL is an additional querying method alongside existing ones like REST.
- It doesn’t aim to replace existing methods; each has its advantages and disadvantages.
Difference from REST
- In GraphQL, the client writes the queries, specifying the data and its structure to retrieve or modify.
- Contrasting REST, where the server determines the structure of data to be returned.
Example Scenario
- In a site connected to a classic REST API and wanting to fetch complex data:
- With REST: Requires a GET request for an identifier and multiple requests for other details.
- With GraphQL: Can query only the necessary information, reducing unnecessary data retrieval.
Backend Development with GraphQL
- GraphQL is highly typed, requiring significant backend effort to code the schema.
- The schema outlines object types, their relationships, and lists of queries and mutations.
Code examples
In the following example schema, we have two main object types: Book and Author, each with their respective fields. The Query type allows fetching data, and the Mutation type allows creating or modifying data. Note that this is just a basic illustration; real-world schemas can be much more complex and feature-rich.
Keep in mind that GraphQL schemas are designed based on the needs of the application and can include various types, queries, mutations, and directives to manage permissions, validations, etc.
# Query to fetch details of a specific book and its author
query GetBookDetails {
book(id: "123") {
title
publishedYear
author {
name
age
}
}
}
# Query to fetch all books written by a specific author
query GetAuthorBooks {
author(id: "456") {
name
books {
title
publishedYear
}
}
}
# Mutation to create a new book
mutation CreateBook {
createBook(title: "New Book", publishedYear: 2023, authorId: "456") {
id
title
publishedYear
author {
name
}
}
}
Let’s define the schema for the above queries. This is a simple representation, and in real-world scenarios, it would be more extensive.
type Book {
id: ID!
title: String!
publishedYear: Int!
author: Author!
}
type Author {
id: ID!
name: String!
age: Int!
books: [Book!]!
}
type Query {
book(id: ID!): Book
author(id: ID!): Author
}
type Mutation {
createBook(title: String!, publishedYear: Int!, authorId: ID!): Book
}
Nested Queries in GraphQL
- Nested queries in GraphQL allow retrieving related data in a single request.
- They help reduce the number of requests required to fetch interconnected data.
Example Schema
type Post {
id: ID!
title: String!
content: String!
author: User!
}
type User {
id: ID!
name: String!
email: String!
posts: [Post!]!
}
type Query {
post(id: ID!): Post
user(id: ID!): User
}
Example Nested Query
Retrieve a user and their posts in a single GraphQL query:
query GetUserWithPosts {
user(id: "1") {
name
email
posts {
title
content
}
}
}
Corresponding Response
{
"data": {
"user": {
"name": "John Doe",
"email": "john@example.com",
"posts": [
{
"title": "GraphQL Basics",
"content": "Learn the fundamentals of GraphQL."
},
{
"title": "Advanced GraphQL",
"content": "Dive deeper into GraphQL concepts."
}
]
}
}
}
In this example, the nested query retrieves a user and their associated posts. The response includes the user’s name, email, and an array of post objects containing their titles and content. By using a nested query, we minimize the number of requests needed to fetch both user and post data.