Query
- A GraphQL query is a type of read operation.
- It is a request for specific data from a server.
- Clients use queries to specify the structure of the response they need.
- The server processes the query and returns the requested data.
Defining the Schema
Let’s define a sample query for Blog Posts, with small queries.
import graphene
class Author(graphene.ObjectType):
id = graphene.Int(required=True)
name = graphene.String(required=True)
email = graphene.String(required=True)
class Post(graphene.ObjectType):
id = graphene.Int(required=True)
title = graphene.String(required=True)
content = graphene.String(required=True)
author = graphene.Field(Author, required=True)
createdAt = graphene.String(required=True)
class Query(graphene.ObjectType):
get_all_posts = graphene.List(Post, required=True, resolver=resolver_get_posts)
get_post_by_id = graphene.Field(Post, required=True, params=GetPostParams, resolver=resolver_get_post_by_id)
Explanation:
- In this schema, we have two types: Author and Post.
- The Post type includes basic fields id, title, content, author, and createdAt.
- The Author type includes basic fields like id, name, and email.
- The Query type includes two queries: getAllPosts to fetch all blog posts and getPostById to fetch a specific post by using its ID, we’ve defined a resolver (method which is to be called when this API is triggered), this resolver can take input (if any), processes your request and provide required response.
Resolvers For Query
def resolver_get_post_by_id(root, info, params):
post_id = params.post_id
authors = {
"1": {
"id": 1,
"name": "Dillip Chowdary",
"email": "dillip_chowdary@techbytes.app"
},
"2": {
"id": 2,
"name": "Narendra Modi",
"email": "modi@techbytes.app"
}
}
posts_details = {
"1":{
"id":1,
"title":"Post_1",
"content":"Post Body",
"author_id":1
},
"2":{
"id":2,
"title":"Post_2",
"content":"Post Body 2",
"author_id":2
}
}
post_details = post[post_id]
post_object = Post(
id=post_details["id"],
title=post_details["title"],
content=post_details["content"],
author=Author(
id=authors[post_details['author_id']]["id"],
name=authors[post_details['author_id']]["name"],
email=authors[post_details['author_id']]["email"]
)
)
return post_object
def resolver_get_posts(root, info):
authors = {
"1": {
"id": 1,
"name": "Dillip Chowdary",
"email": "dillip_chowdary@techbytes.app"
},
"2": {
"id": 2,
"name": "Narendra Modi",
"email": "modi@techbytes.app"
}
}
posts_details = [
{
"id": 1,
"title": "Post_1",
"content": "Post Body",
"author_id": 1
},
{
"id": 2,
"title": "Post_2",
"content": "Post Body 2",
"author_id": 2
}
]
posts = [
Post(
id=post["id"],
title=post["title"],
content=post["content"],
author=Author(
id=authors[post['author_id']]["id"],
name=authors[post['author_id']]["name"],
email=authors[post['author_id']]["email"]
)
)
for post in posts_details
]
return posts
schema = graphene.Schema(query=Query)
Explanation:
- resolver_get_posts: Will return all posts, this method will return List of Post type, it doesn’t expect any input params.
- resolver_get_post_by_id: This method will expect a param post_id, and return the post details, as defined, this method will return Post type
Sample Query
Here is the sample API call seeking for post details, using the getPostById query we’ve defined above, offcourse we can define the query type for creating, Updating or modifying operations but that’s not a good practice, we’re following the similar rule for REST APIs as well.
Use query if we’re requesting the data, and Mutation if you’re creating, modifying or deleting the data.
query {
getPostById(postId: "123") {
id
title
content
author {
name
}
}
}
Response:
The Query and Mutation Types in GraphQL Schema
Imagine an API that responds exactly as we wish, providing just the data we need in a single request. GraphQL makes this dream a reality by offering a flexible and efficient approach to querying and manipulating data.
With its intuitive syntax and powerful features, GraphQL explains how developers design and interact with APIs, offering a more customized and streamlined experience for both developers and users.
In this article, We will learn about, The Query and Mutation Types in GraphQL Schema along with the components like Query and mutations with the implementation of examples and so on.