Introduction

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.

Revisiting Apollo Server Basics

We'll start with a quick review of key components without introducing mutations.

  1. Import Modules: Import the necessary modules, including ApolloServer for setting up the server and uuid for generating unique IDs.

  2. Define Schema: Define the GraphQL schema with a Book type and a Query type to fetch book data.

  3. Sample Data: Provide some sample book data to be served by our query.

  4. Define Resolvers: Specify how each field in the schema maps to the data provided.

  5. Initialize and Start Server: Create an instance of ApolloServer and start it.

Introduction to Mutations

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.

  1. AddBook Mutation: Define a mutation to add a new book by specifying a title and author.

  2. DeleteBook Mutation: Define a mutation to delete a book by specifying its ID.

Writing Resolvers for Mutations

Resolvers execute the behavior for a given type in the schema.

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

  2. 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 at bookIndex from the books array and assigns the removed book to the variable deletedBook.

Testing Mutations

To test our mutations, we'll use a Node.js script to make HTTP requests to our GraphQL server.

  1. Import Fetch Module: Import the fetch function, which is available globally in modern Node.js, or use node-fetch for older versions.

  2. Define Queries and Mutations: Define the queries and mutations we want to perform for testing.

  3. 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.
  4. Execute Sample Requests: Run a sequence of requests to query books, add a new book, and delete a book, then observe the changes.

Expected Output

When running the script, you should see logged outputs similar to:

Review and Next Steps

In this lesson, you learned how to:

  1. Set up a basic Apollo Server.
  2. Define a GraphQL schema with mutations.
  3. Write resolver functions for mutations.
  4. 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.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal