Crafting Queries and Mutations
At the core of interacting with a GraphQL API are queries (for fetching data) and mutations (for modifying data). Understanding how to construct these operations is fundamental to leveraging GraphQL's power, as discussed in GraphQL Schema and Type System.
Queries: Fetching Data
A GraphQL query is a string that is sent to a server to be interpreted and fulfilled, which then returns JSON back to the client. Queries allow you to specify exactly which fields you want, and the server will return only that data.
Basic Query
Let's say we want to fetch the title of a specific post:
query GetPostTitle {
post(id: "1") {
title
}
}
The server would respond with:
{
"data": {
"post": {
"title": "My First GraphQL Post"
}
}
}
Notice how the response mirrors the shape of the query. This is a key feature of GraphQL. You can explore more about the foundational concepts of data retrieval in AI & Machine Learning Basics, where efficient data handling is also paramount.
Requesting Multiple Fields and Nested Data
You can request multiple fields and even nested data from related objects:
query GetPostDetails {
post(id: "1") {
title
content
author {
name
}
}
}
Arguments
Fields can take arguments to specify which data to fetch. In the example above, post(id: "1")
uses an id
argument to fetch a specific post.
Variables
To make queries dynamic, you can use variables. This is highly recommended as it avoids string interpolation on the client and makes queries reusable.
query GetPostWithVariable($postId: ID!) {
post(id: $postId) {
title
content
}
}
You would then send the variable value alongside the query, typically as a separate JSON object: { "postId": "1" }
.
Mutations: Modifying Data
Mutations are used to change data on the server (create, update, or delete). They are structured similarly to queries but use the mutation
keyword.
Basic Mutation
Here's an example of a mutation to create a new post. Like queries, mutations can also return data, often the object that was just modified.
mutation CreateNewPost($title: String!, $authorId: ID!) {
createPost(title: $title, authorId: $authorId) {
id
title
author {
name
}
}
}
You'd send variables like: { "title": "GraphQL is Awesome", "authorId": "2" }
.
The server's response would include the id
, title
, and author
details of the newly created post.
Input Types for Mutations
For mutations that take many arguments, it's common to use Input Object Types to group them. This makes the mutation cleaner and more readable.
# Schema Definition (SDL)
input CreatePostInput {
title: String!
content: String
authorId: ID!
}
type Mutation {
createPost(input: CreatePostInput!): Post
}
# Mutation Operation
mutation CreatePostWithInput($postInput: CreatePostInput!) {
createPost(input: $postInput) {
id
title
}
}
And variables: { "postInput": { "title": "Using Input Types", "content": "Makes mutations cleaner!", "authorId": "2" } }
.
Mastering queries and mutations is crucial for effective GraphQL development. They provide a flexible and powerful way to interact with your API. Next, we'll explore how GraphQL handles real-time data with Subscriptions.
Next: Real-time with Subscriptions