Shopify2023-10-18Tutorial
Understanding GraphQL for Beginners–Part Three
Summary
Part Three of a beginner GraphQL series introduces mutations for creating, updating, and deleting database objects, extending the earlier focus on queries that fetch data. Using the existing Rails repository with Food and Nutrition ActiveRecord models, it shows how to generate mutation classes with rails g graphql:mutation and register root fields in mutation_type.rb. The tutorial defines input arguments, uses snake_case fields that GraphQL converts to camelCase, and implements a resolve method that creates or locates records and returns typed responses. It demonstrates foodCreate, foodUpdate, and foodDelete in GraphiQL, while exercises ask readers to build corresponding Nutrition mutations. It concludes that mutations use the same endpoint as queries and that each schema field has a resolve method that returns its response.
Context
The tutorial builds on an earlier part that covered GraphQL queries for fetching data and addresses how to create, modify, and delete Food and Nutrition database records in the existing Rails repository.
Approach / What changed
It uses Rails GraphQL mutation generators, mutation root fields, input arguments, typed responses, and resolve methods. Examples implement foodCreate, foodUpdate, and foodDelete, with GraphiQL used to test the mutations and exercises extending the work to Nutrition records.
Takeaways
GraphQL mutations create, modify, or delete database objects and are sent to the same endpoint as query requests.
The Rails generator rails g graphql:mutation nameAction creates mutation scaffolding and adds the corresponding root field to mutation_type.rb.
Mutation resolve methods receive the input, perform the ActiveRecord operation, and return the response type declared by the mutation.
Source and freshness
- Source
- shopify.engineering
- Published
- Added to Yomu
- Last updated
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.