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.TypeScript1import { ApolloServer } from '@apollo/server'; 2import { startStandaloneServer } from '@apollo/server/standalone'; 3import { v4 as uuidv4 } from 'uuid';
-
Define Schema: Define the GraphQL schema with a
Book
type and aQuery
type to fetch book data.TypeScript1const typeDefs = `#graphql 2 type Book { 3 id: ID! 4 title: String! 5 author: String! 6 } 7 8 type Query { 9 books: [Book] 10 book(id: ID!): Book 11 } 12`;
-
Sample Data: Provide some sample book data to be served by our query.
TypeScript1let books = [ 2 { id: '1', title: 'The Hobbit', author: 'J.R.R. Tolkien' }, 3 { id: '2', title: 'Harry Potter', author: 'J.K. Rowling' }, 4];
-
Define Resolvers: Specify how each field in the schema maps to the data provided.
TypeScript1const resolvers = { 2 Query: { 3 books: () => books, 4 book: (_: any, args: { id: string }) => books.find(book => book.id === args.id), 5 }, 6};
-
Initialize and Start Server: Create an instance of
ApolloServer
and start it.TypeScript1const server = new ApolloServer({ 2 typeDefs, 3 resolvers, 4}); 5 6const { url } = await startStandaloneServer(server, { 7 listen: { port: 4000 }, 8}); 9 10console.log(`🚀 Server ready at ${url}`);
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.
TypeScript1type Mutation { 2 addBook(title: String!, author: String!): Book 3}
-
DeleteBook Mutation: Define a mutation to delete a book by specifying its ID.
TypeScript1type Mutation { 2 deleteBook(id: ID!): Book 3}
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.
TypeScript1addBook: (_: any, { title, author }: { title: string, author: string }) => { 2 const newBook = { id: uuidv4(), title, author }; 3 books.push(newBook); 4 return newBook; 5},
-
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
.TypeScript1deleteBook: (_: any, { id }: { id: string }) => { 2 const bookIndex = books.findIndex(book => book.id === id); 3 if (bookIndex === -1) return null; 4 const [deletedBook] = books.splice(bookIndex, 1); 5 return deletedBook; 6},
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.TypeScript1import fetch from 'node-fetch';
-
Define Queries and Mutations: Define the queries and mutations we want to perform for testing.
TypeScript1const queryBooks = ` 2 query { 3 books { 4 id 5 title 6 author 7 } 8 } 9`; 10 11const addBookMutation = (title: string, author: string) => ` 12 mutation { 13 addBook(title: "${title}", author: "${author}") { 14 id 15 title 16 author 17 } 18 } 19`; 20 21const deleteBookMutation = (id: string) => ` 22 mutation { 23 deleteBook(id: "${id}") { 24 id 25 title 26 author 27 } 28 } 29`;
-
Function to Execute Requests: Create a function to send HTTP requests to the GraphQL server and log the response.
TypeScript1const url = 'http://localhost:4000/'; 2 3const makeRequest = async (query: string) => { 4 try { 5 const response = await fetch(url, { 6 method: 'POST', 7 headers: { 8 'Content-Type': 'application/json', 9 }, 10 body: JSON.stringify({ query }), 11 }); 12 const json = await response.json(); 13 console.log(JSON.stringify(json, null, 2)); 14 } catch (error) { 15 console.error('Error:', error); 16 } 17};
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.
TypeScript1(async () => { 2 console.log("Query all books:"); 3 await makeRequest(queryBooks); 4 5 console.log("Add a new book:"); 6 await makeRequest(addBookMutation("New Book", "New Author")); 7 8 console.log("Query all books after addition:"); 9 await makeRequest(queryBooks); 10 11 console.log("Delete a book:"); 12 await makeRequest(deleteBookMutation("1")); 13 14 console.log("Query all books after deletion:"); 15 await makeRequest(queryBooks); 16})();
When running the script, you should see logged outputs similar to:
JSON1{ 2 "data": { 3 "books": [ 4 { "id": "1", "title": "The Hobbit", "author": "J.R.R. Tolkien" }, 5 { "id": "2", "title": "Harry Potter", "author": "J.K. Rowling" } 6 ] 7 } 8}
JSON1{ 2 "data": { 3 "addBook": { 4 "id": "unique-id", 5 "title": "New Book", 6 "author": "New Author" 7 } 8 } 9}
JSON1{ 2 "data": { 3 "deleteBook": { 4 "id": "1", 5 "title": "The Hobbit", 6 "author": "J.R.R. Tolkien" 7 } 8 } 9}
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.