Welcome to the first lesson of our "GraphQL Mutations and Advanced Apollo Server" course, part of the "Comprehensive Intro to GraphQL in TypeScript" series. In this lesson, you'll learn how to add mutations, which will allow you to modify data on the server.
We'll start with a quick review of key components without introducing mutations.
-
Import Modules: Import the necessary modules, including
ApolloServer
for setting up the server anduuid
for generating unique IDs. -
Define Schema: Define the GraphQL schema with a
Book
type and aQuery
type to fetch book data. -
Sample Data: Provide some sample book data to be served by our query.
-
Define Resolvers: Specify how each field in the schema maps to the data provided.
-
Initialize and Start Server: Create an instance of
ApolloServer
and start it.
In GraphQL, mutations allow clients to modify data on the server, such as creating, updating, or deleting records. Unlike queries, which are read-only and do not affect the server's state, mutations perform write operations. Mutations often correspond to HTTP POST requests and require specific arguments to specify the data to be modified. They return the updated or deleted data, enabling clients to immediately see the result of their operation.
-
AddBook Mutation: Define a mutation to add a new book by specifying a title and author.
-
DeleteBook Mutation: Define a mutation to delete a book by specifying its ID.
Resolvers execute the behavior for a given type in the schema.
-
Adding a Book: Create a resolver function to take the title and author, create a new book with a unique ID, add it to the list, and return the new book.
-
Deleting a Book: Create a resolver function to take the book ID, find and remove the book from the list, and return the deleted book. The
splice
method removes the book atbookIndex
from thebooks
array and assigns the removed book to the variabledeletedBook
.
To test our mutations, we'll use a Node.js script to make HTTP requests to our GraphQL server.
-
Import Fetch Module: Import the
fetch
function, which is available globally in modern Node.js, or usenode-fetch
for older versions. -
Define Queries and Mutations: Define the queries and mutations we want to perform for testing.
-
Function to Execute Requests: Create a function to send HTTP requests to the GraphQL server and log the response.
Here, the
makeRequest
function:- Converts the query or mutation into a JSON payload.
- Sends an HTTP POST request with the payload to the server.
- Parses the JSON response and log it to the console.
- Handles errors gracefully.
-
Execute Sample Requests: Run a sequence of requests to query books, add a new book, and delete a book, then observe the changes.
When running the script, you should see logged outputs similar to:
In this lesson, you learned how to:
- Set up a basic Apollo Server.
- Define a GraphQL schema with mutations.
- Write resolver functions for mutations.
- Test your mutations using a Node.js script.
Next, you'll get hands-on practice with these concepts through a series of exercises. In the upcoming lessons, we will delve deeper into advanced features and best practices in GraphQL and Apollo Server.
