BlogsShopifyGraphQL Mutations for Data Manipulation

GraphQL Mutations for Data Manipulation

GraphQL Mutations for Data Manipulation

5
posts
2019–2021

This feature thread tracks the development and implementation of GraphQL mutations for creating, updating, and deleting data within Shopify's systems. Initial efforts focused on understanding GraphQL queries for data fetching. This post introduces the concept of mutations as the mechanism for modifying data, detailing the process of generating mutation resolvers, defining input arguments, and handling the `resolve` method to interact with the database. It covers specific examples for creating, updating, and deleting data. This post also details the implementation of a calculated query cost method for rate limiting GraphQL APIs, which adapts to the data clients need and provides a more predictable load on servers compared to traditional request-based models. The cost is calculated based on the types and number of objects requested, with mutations having a higher base cost due to their side effects.

2021

Understanding GraphQL for Beginners–Part Three - Shopify

8/6/2021

This post introduces and details the implementation of GraphQL mutations for data manipulation. It covers the generation of mutation classes using `rails g graphql:mutation`, the structure of mutation queries, defining input arguments (including handling snake_case to camelCase conversion), and implementing the `resolve` method to perform CRUD operations on ActiveRecord objects. Specific examples are provided for `foodCreate`, `nutritionCreate`, `foodUpdate`, `nutritionUpdate`, `foodDelete`, and `nutritionDelete` mutations.

Understanding GraphQL for Beginners–Part One - Shopify

7/2/2021

This post introduces GraphQL as an alternative to RESTful APIs, explaining its benefits for data fetching. It details how GraphQL allows clients to specify the exact data they need, contrasting this with REST's fixed responses. The post uses analogies (burgers, banks) and examples to illustrate the differences and advantages of GraphQL, such as reducing over-fetching and under-fetching, enabling rapid product iterations on the frontend, and consolidating multiple endpoints into a single 'smart' endpoint. It sets the stage for a subsequent post that will detail implementing GraphQL in a Ruby on Rails application.

Understanding GraphQL for Beginners–Part Two - Shopify

7/2/2021

This post details the creation of a GraphQL object (`food`) based on an existing Ruby on Rails model, the creation of a root field (`all_food`) to query all food items, and the implementation of a query to retrieve specific fields (`image`, `place_of_origin`). It also explains how to add a nested field (`nutrition`) to an existing object and how to write a query with an argument (`food` by `id`). The post also includes a 'Try it Yourself' section for implementing a `nutrition` root field and a `find_food` root field based on `place_of_origin`.

Rate Limiting GraphQL APIs by Calculating Query Complexity - Shopify

6/22/2021

This post introduces a calculated query cost method for rate limiting Shopify's GraphQL Admin API. It details how query costs are defined for different GraphQL types (objects, scalars, enums, connections, interfaces, unions, mutations) based on the data requested and potential server load. It also explains how query cost information is returned in API responses and the difference between requested and actual query costs, with a focus on measuring the effectiveness of this model through the correlation between query complexity and execution time.

2019

Unifying Our GraphQL Design Patterns and Best Practices with Tutorials - Shopify

2/13/2019

This post details the unification of Shopify's GraphQL design patterns and best practices by creating an in-depth design tutorial. It addresses the scaling issues encountered due to a lack of formal documentation and guidelines as more developers contributed to GraphQL APIs. The creation of the API Patterns Team is highlighted as a key initiative to focus on horizontal concerns across the company. The tutorial emphasizes a business domain-centric API design philosophy and provides practical applications through a real example.