Skip to content

Instantly share code, notes, and snippets.

@ShopifyEng

ShopifyEng/nutrition_query.graphql Secret

Last active Jul 2, 2021
Embed
What would you like to do?
Understanding GraphQL for Beginners–Part Two - Nutrition GraphQL Object and Query - Try it yourself #1
query {
nutrition {
id
foodId
calories
protein
totalFat
}
}
{
"data": {
"nutrition": [
{
"id": "1",
"foodId": 1,
"calories": "158",
"protein": "6 g",
"totalFat": "0.98 g"
},
{
"id": "2",
"foodId": 2,
"calories": "459",
"protein": "11 g",
"totalFat": "18 g"
},
{
"id": "3",
"foodId": 3,
"calories": "149",
"protein": "5.5 g",
"totalFat": "9.6 g"
}
]
}
}
module Types
class NutritionType < Types::BaseObject
field :id, ID, null: false
field :food_id, Integer, null: true
field :serving_size, String, null: true
field :calories, String, null: true
field :total_fat, String, null: true
field :trans_fat, String, null: true
field :saturated_fat, String, null: true
field :cholesterol, String, null: true
field :sodium, String, null: true
field :potassium, String, null: true
field :total_carbohydrate, String, null: true
field :dietary_fiber, String, null: true
field :sugars, String, null: true
field :protein, String, null: true
field :vitamin_a, String, null: true
field :vitamin_c, String, null: true
field :calcium, String, null: true
field :iron, String, null: true
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
end
end
field :nutrition, [Types::NutritionType], null: false, description: 'Get all the nutrition facts.'
def nutrition
Nutrition.all
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment