Shopify2023-10-18Tutorial
Understanding GraphQL for Beginners–Part Two
Summary
The second part of this beginner tutorial uses a food-focused Ruby on Rails application to show how GraphQL fields expose database data. It reviews the graphql directory, where mutations hold data-changing classes, types define returned structures, and food_app_schema.rb defines available queries. It distinguishes root fields, which are GraphQL entry points that define response structure, from object fields, which represent attributes, then demonstrates generating Food and Nutrition types and adding an all_food root field with a non-null list return type. Through GraphiQL at localhost:3000/graphiql, it runs selective queries, adds nutrition to food, and introduces an id argument for retrieving one record; requests pass through graphql_controller’s execute method and return JSON responses.
Context
The tutorial is the second part of a beginner series and uses a prepared Ruby on Rails repository with Food and Nutrition models, seeded database data, and the GraphQL gems needed to follow along. It focuses on understanding the GraphQL file structure, distinguishing root and object fields, querying food data, and exposing the association between food and nutrition.
Approach / What changed
It creates GraphQL object types from the existing Rails models, defines root fields in query_type.rb, and tests them in the GraphiQL playground. The examples add all_food, nutrition, and an id-based food query, select specific response fields, and add a nullable nutrition field to FoodType so food queries can include serving size and calorie data.
Takeaways
Root fields are entry points to a GraphQL server and define response structure, while object fields represent an object's attributes.
The Food type can expose its associated Nutrition record by adding field :nutrition, Types::NutritionType, null: true.
GraphiQL documents schema queries and executes requests; graphql_controller’s execute method processes them and returns a JSON response.
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.