Skip to content

GraphQL and Webhooks: Query Exactly the Fields You Need and Verify Signed Events

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

REST is not the only way APIs talk. GraphQL turns the conversation around: instead of many URLs that each return a fixed shape, there is one endpoint and the client writes a query that names exactly the fields it wants. Webhooks turn it around in a different way: instead of your app asking "anything new?" every minute, the other service calls your server the moment something happens, such as a payment succeeding. This lesson uses a free public GraphQL API about countries to query the six Gulf states from your browser, shows a GraphQL error with real output, and then builds the signature check that every webhook receiver needs, both in Node.js and in the browser.

Quick answer

GraphQL is a query language for APIs: the client POSTs JSON with a query string, and optional variables, to one endpoint and gets back data in the same shape as the query. Errors usually come back in an errors array with status 200, so always check it. A webhook is an HTTP POST that another service sends to your URL when an event happens. Verify its HMAC signature over the raw body with a timing-safe comparison, answer 2xx quickly, and ignore duplicate event ids.

40 min read and practice · Content checked on 30 November 2026

In this lesson you will

  • Write GraphQL queries with variables and send them with fetch
  • Handle GraphQL errors and know when REST is the better fit
  • Receive webhooks safely: signatures, retries and duplicate events

GraphQL: one endpoint, the fields you ask for

A GraphQL server publishes a schema: the types it knows, such as Country, and the fields on each, such as name, capital and languages. A query lists the fields you want and nests related objects, so one request can return a country and its languages without a second call. The client sends a POST to a single URL with a JSON body {"query": "..."} and receives {"data": ...} mirroring the query. No special library is needed; fetch is enough.

Values that change, such as a list of country codes, belong in variables, not glued into the query string. The live example declares $codes: [String!]!, meaning a required list of strings, and passes the six Gulf codes separately. This keeps queries readable and cacheable and protects against injection, the same reason you use parameters in SQL. Only the three requested fields came back for each country; the server did not send currency names, emojis or anything else the schema offers.

Try it: Query the six Gulf states with variables
Code
ResultUpdates as you type
Console
  1. Console messages, errors, form data and clicked links appear here.

This printed six lines, from United Arab Emirates: Abu Dhabi (AED) and Bahrain: Manama (BHD) to Saudi Arabia: Riyadh (SAR). The server sorted them by code, not in the order we sent. Add native to the field list and the Arabic names appear.

Example: A GraphQL query for the UAE, sent with curl
Real output: curl -s -X POST https://countries.trevorblades.com/ -H "Content-Type: application/json" -d '{"query":"{ country(code: \"AE\") { name native capital currency languages { name } } }"}'
{"data":{"country":{"native":"دولة الإمارات العربية المتحدة","capital":"Abu Dhabi","currency":"AED","languages":[{"name":"Arabic"}],"name":"United Arab Emirates"}}}

One request returned the country and its nested languages. The keys came back in a different order from the query; JSON objects have no guaranteed order, so read fields by name. The native name arrived in Arabic as plain UTF-8.

Sign in free to read the full lesson

You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.

Unlocks after sign-in:

  • 2 more sections with live code examples
  • Common mistakes to avoid
  • Hands-on coding exercise with automatic checks
  • Practice quiz and the graded lesson test
Sign in or create a free account

Free forever. No card, no paywall.

Questions people ask

Is GraphQL replacing REST?

No. Both are widely used and many companies run both. GraphQL suits apps that combine lots of related data for different screens; REST stays simpler for small APIs, public APIs and anything that benefits from plain HTTP caching.

Do I need Apollo or another library to use GraphQL?

No. Every example here uses plain fetch with a JSON body. Client libraries add caching, typed results and developer tools, which help in larger apps, but they send the same POST underneath.

How can I test webhooks on my laptop?

The provider cannot reach localhost, so use the provider’s CLI that forwards events, as Stripe offers, or a tunnel such as Cloudflare Tunnel. You can also replay a saved event body with curl and a signature made by openssl, as in this lesson.

What is the difference between a webhook and polling?

Polling means your app asks the API "anything new?" on a timer, which wastes requests and adds delay. A webhook means the service calls you when something changes. Many apps use webhooks and still poll occasionally to catch anything missed.

Can I use the countries GraphQL API in my own project?

It is a free, open source demo API maintained by an independent developer, great for learning and side projects. For a business app, copy the data into your own database or use a service with a support agreement, because a free demo can change or go offline.

Sources

Facts in this lesson were checked against these official pages on 30 November 2026.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.