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.
Console
- 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.
{"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
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.
- GraphQL: Introduction to GraphQL
- GraphQL: Serving over HTTP
- Countries GraphQL API (source code)
- GitHub Docs: Validating webhook deliveries
- Node.js: crypto.timingSafeEqual
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.