Combined Schema With Query and Mutations
Here is the schema with both query & mutation (overall code)
Suppose we want to design a GraphQL schema for managing blog posts and authors. The schema should include types for authors and posts, with fields for ID, name, email, title, content, and creation date. Additionally, implement a mutation for creating new posts. The schema should support queries for retrieving all posts and retrieving a post by its ID. Use dummy data for authors and posts for testing purposes.
import graphene
# Define the Author object type
class Author(graphene.ObjectType):
id = graphene.Int(required=True)
name = graphene.String(required=True)
email = graphene.String(required=True)
# Define the Post object type
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)
# Define the CreatePost mutation
class CreatePost(graphene.Mutation):
class Arguments:
title = graphene.String(required=True)
content = graphene.String(required=True)
author_id = graphene.Int(required=True)
Output = Post
@staticmethod
def mutate(root, info, title, content, author_id):
# In a real implementation, this function would create the post in the database
# and return the created post object
return Post(
id=1, # Replace with actual post ID
title=title,
content=content,
author=Author(
id=author_id,
name="Author Name", # Replace with actual author name
email="author@example.com" # Replace with actual author email
),
createdAt="2022-03-24" # Replace with actual creation date
)
# Define the Query object type
class Query(graphene.ObjectType):
get_all_posts = graphene.List(Post, required=True) # Get all posts query
get_post_by_id = graphene.Field(Post, required=True, post_id=graphene.Int()) # Get post by ID query
def resolve_get_all_posts(root, info):
# Dummy data for posts
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
}
]
authors = {
1: {
"id": 1,
"name": "Dillip Chowdary",
"email": "dillip_chowdary@techbytes.app"
},
2: {
"id": 2,
"name": "Narendra Modi",
"email": "modi@techbytes.app"
}
}
# Create a list of Post objects with author details
return [
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"]
),
createdAt="2022-03-24" # Replace with actual creation date
)
for post in posts_details
]
def resolve_get_post_by_id(root, info, post_id):
# Dummy data for posts
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
}
}
authors = {
1: {
"id": 1,
"name": "Dillip Chowdary",
"email": "dillip_chowdary@techbytes.app"
},
2: {
"id": 2,
"name": "Narendra Modi",
"email": "modi@techbytes.app"
}
}
post_details = posts_details.get(post_id)
if post_details:
return 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"]
),
createdAt="2022-03-24" # Replace with actual creation date
)
else:
return None
# Define the Mutation object type
class Mutation(graphene.ObjectType):
create_post = CreatePost.Field(required=True) # Define the create post mutation
# Create the schema with Query and Mutation types
schema = graphene.Schema(query=Query, mutation=Mutation)
Explanation: The provided code defines a GraphQL schema with object types for Author
and Post
, along with a Query
object type for retrieving posts and a Mutation
object type for creating posts.
- The
Author
object type represents the author of a post, with fields for ID, name, and email. - The
Post
object type represents a post, with fields for ID, title, content, author (anAuthor
object), and creation date. - The
CreatePost
mutation allows the creation of a new post with the specified title, content and author ID. - The
get_all_posts
query retrieves a list of all posts with their details. - The
get_post_by_id
query retrieves a single post by its ID.
These components together define a GraphQL API for managing posts and authors.
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.