GraphQL Mutations in Ruby
Introduction
Welcome to the first lesson of our "GraphQL Mutations and Advanced GraphQL Server" course, part of the "Comprehensive Intro to GraphQL in Ruby" series. In this lesson, you'll learn how to add mutations, which will allow you to modify data on the server.
Revisiting GraphQL Server Basics
We'll start with a quick review of key components without introducing mutations.
-
Set Up Project.
First, create a new Ruby project and install the necessary gems. You'll need
graphql,sinatra, andsecurerandom(which is part of Ruby's standard library). -
Import Required Libraries.
Import the necessary modules for setting up the GraphQL server.
-
Define GraphQL Types.
Define the GraphQL
Booktype using graphql-ruby's class-based approach. -
Sample Data.
Provide some sample book data to be served by our query.
-
Define Query Type.
Create a query type that defines how to fetch book data.
-
Define Schema.
Create the GraphQL schema that ties everything together.
-
Initialize and Start Server.
Create a Sinatra server with a GraphQL endpoint.
Introduction to Mutations
Mutations in GraphQL work like creating, updating, or deleting data records. Let's define the mutations using graphql-ruby's class-based approach.
-
AddBook Mutation.
Define a mutation class to add a new book by specifying a
titleandauthor.Notice the distinction between
argumentandfieldhere. Theargumentdeclarations define what the client must provide as input (the book'stitleandauthor). Thefielddeclarations define what the mutation returns in its response — in this case, the newly created book'sid,title, andauthor. Think ofargumentas the request andfieldas the response shape. -
DeleteBook Mutation.
Define a mutation class to delete a book by specifying its
id. -
Define Mutation Type.
Create a mutation type that includes all mutations.
-
Update Schema.
Update the schema to include mutations.
Writing Resolvers for Mutations
Resolvers execute the behavior for a given type in the schema. In graphql-ruby, mutation resolvers are defined as resolve methods within mutation classes.
-
Adding a Book.
The resolver method takes the
titleandauthor, creates a new book with a uniqueidusingSecureRandom.uuid, adds it to the list, and returns the new book. -
Deleting a Book.
The resolver method takes the book
id, finds and removes the book from the list usingdelete_at, and returns the deleted book. If the book is not found, it returnsnil.
Testing Mutations
To test our mutations, we'll use a Ruby script to make HTTP requests to our GraphQL server.
-
Import Required Libraries.
Import the necessary libraries for making HTTP requests and handling JSON.
-
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 print the response.
-
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:
Note: The
idvalue above is an example.SecureRandom.uuidgenerates a random UUID each time, so your output will show a different value in the standard UUID format (e.g.,"f47ac10b-58cc-4372-a567-0e02b2c3d479").
Review and Next Steps
In this lesson, you learned how to:
- Set up a basic GraphQL server using
graphql-rubyand Sinatra. - Define a GraphQL schema with mutations.
- Write resolver functions for mutations.
- Test your mutations using a Ruby 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 graphql-ruby.
