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 (an Author 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.

Similar Reads

Basic Components of GraphQL

GraphQL is a query language for APIs and a runtime for executing those queries by using a type system we define for our data. Unlike traditional REST APIs, where clients have limited control over the structure of the responses, GraphQL allows clients to request only the data they need, in the format they need it....

1. 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....

2. Mutation

A mutation in GraphQL is used for write operations, such as creating, updating, or deleting data on the server. It allows clients to modify data. While a query is used for read operations, a mutation is specifically designed for write operations. This distinction helps in clearly defining the intent of an operation. Mutations can modify data in the server’s database, unlike queries which only fetch data. They are used when you need to change the state of the server or persist new data. It’s considered a good practice to use mutations for write operations, even though queries can technically be used to modify data as well. Using mutations explicitly indicates that the operation is intended to change data....

Combined Schema With Query and Mutations

Here is the schema with both query & mutation (overall code)...

Conclusion

In this post we’ve learnt the basics of GraphQL and the key differences between Query, Mutation and it’s purposes with an sample blog post example.In this post we’ve seen how to define query, mutation, and writing resolvers for them....