Skip to content
Tutorials Center
My learning

Topics

Lesson 53 of 55 ยท Practice and Review

Practice Exercises with Solutions

Ten Shopify app exercises with runnable solutions: GraphQL queries, HMAC checks, cursor pagination, rate limits, function logic, scope diffs and billing states.

Reading about Shopify apps is not the same as building one. This page gives you ten small exercises, each on a skill an app developer uses every week. For each one you get a task, a hint, and a runnable solution that you can edit and break.

Be clear about what is real. The platform itself (the Admin API, webhooks, billing, functions) cannot run on this site, so every solution below is a stand-in written in plain JavaScript that reproduces the rule you need to understand: how a query string is assembled, how a signature is checked, how a cursor moves. The logic is the same logic your real app uses; only the network and the data are fake. Examples that mention an API version target 2026-07, so check the current version before you copy anything into production.

How to use the page: read the task, try it yourself in the editor first (the solutions are right there, so cover them with your hand if you must), then compare. If a task feels unfamiliar, the lesson to revisit is named in each section.

flowchart LR
    A["Read the task"] --> B["Write your own answer"]
    B --> C["Run the solution"]
    C --> D{"Same result?"}
    D -->|"yes"| E["Next exercise"]
    D -->|"no"| F["Reread the linked lesson"]
    F --> B

A practice loop: attempt first, compare second.

Exercises 1 and 2: a query and a signature

Exercise 1: build a products query. Write a function that takes { first, search, fields } and returns a GraphQL string for the products connection, with the search text passed as a variable, never pasted into the query. Background: GraphQL for Shopify Developers and Querying Products and Variants.

Exercise 2: verify a webhook. Write verify(rawBody, header, secret) that returns true only when the base64 HMAC-SHA256 of the raw body matches the header, compared in constant time. Then prove that changing one character of the body makes it fail. Background: Verifying Webhook Signatures.

Node.js Solutions 1 and 2: query builder and HMAC check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice what the solution does not do: it never parses the JSON before verifying, because parsing and re-serialising can change bytes, and the signature covers the exact bytes Shopify sent.

Exercises 3 and 4: cursors and rate limits

Exercise 3: walk every page. Given a fake fetchPage(first, after) that returns { nodes, pageInfo: { hasNextPage, endCursor } }, write an async generator that yields every node exactly once. It must stop when hasNextPage is false and must never use page numbers. Background: Pagination with Cursors.

Exercise 4: simulate the leaky bucket. A bucket holds 1000 points and refills 50 points per second. Write a class with tryRun(cost, atSecond) that returns { ok: true, left } or { ok: false, waitSeconds }, where waitSeconds tells the caller how long to wait before the same query would fit. Background: Rate Limits and Query Cost.

Node.js Solutions 3 and 4: pagination and a leaky bucket

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The generator hides the paging loop from its caller, which is the pattern to copy in a real app. The bucket answers "how long do I wait?" instead of just "no", and that is what makes a polite retry possible.

Exercises 5 and 6: function logic and scope changes

Exercise 5: a discount rule. Write a pure function for a buy-X-get-cheapest-free rule: for every group of three units in the cart, the cheapest unit of that group is free (sort lines into single units, highest price first, group by three). Return the discount in cents. Keep money as integers (Money as Integers). Background: Discount Functions in Depth.

Exercise 6: diff two scope lists. Given the scopes a shop granted and the scopes your updated app now requests, return { added, removed, needsConsent }. Removing a scope never needs consent; adding one does. Remember that write_products implies read_products. Background: Managed Installation and Access Scopes.

Node.js Solutions 5 and 6: cheapest free and a scope diff

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Both functions are tiny and pure. That is deliberate: logic with no network and no clock is the easiest logic to test, and it is the shape a real function needs (Testing and Debugging Functions).

Exercises 7 to 10: billing, IDs, retries and errors

Exercise 7: billing states. Map a subscription status (ACTIVE, PENDING, DECLINED, EXPIRED, FROZEN, CANCELLED, or anything unknown) to a plan name, defaulting to free for everything that is not active. Background: Implementing Billing with the Billing API.

Exercise 8: global IDs. Write parseGid(gid) returning { type, id } for gid://shopify/Product/123, and throw a clear error for malformed input.

Exercise 9: idempotent webhooks. Write a processor that ignores a delivery whose X-Shopify-Webhook-Id it has already handled. Background: Reliable Webhook Handlers and Idempotency.

Exercise 10: userErrors. Write unwrap(response) that throws on top-level errors, returns the data when userErrors is empty, and otherwise returns the user errors as readable lines. Background: Mutations and userErrors.

Node.js Solutions 7 to 10: billing, gids, retries, userErrors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Going further

Once all ten pass, combine them. A real feature is rarely one exercise: a low-stock app (see Project: Low-Stock Alerts App) verifies a signature, skips duplicate deliveries, reads inventory through a paginated query and respects the rate limit. Try rewriting exercise 9 so that the seen-set survives a restart (store it in a table with a unique key), or extend exercise 4 so it sleeps for waitSeconds and retries.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does exercise 2 verify the signature before parsing the JSON body?

    The HMAC is computed over the raw bytes. Verify the raw body first, then parse it.
  2. 2. In exercise 3, why does the loop use endCursor instead of a page number?

    Offsets shift when data changes; a cursor is anchored to a specific item.
  3. 3. What should an unknown subscription status map to in exercise 7?

    Failing closed protects your revenue and keeps the app usable on the free tier.
  4. 4. A scope diff shows only removed scopes. Does the merchant have to approve anything?

    Consent is needed when the app asks for more access, not less.

Summary

  • Practise by attempting each task first and comparing with a runnable solution second.
  • Verify webhooks over the raw body, walk connections with cursors, and treat rate limits as a budget you can calculate.
  • Keep function logic pure and money in integers so it is easy to test.
  • Fail closed on unknown billing states, make webhook handlers idempotent and always read userErrors.

Next, review the ideas in conversation form with Shopify App Developer Interview Questions.