Live data · Countries GraphQL API
Country facts from a GraphQL API
Pick a country and the facts you want. This page writes a GraphQL query for exactly those facts, sends it to a free API and draws the card from the answer. Scroll down to see the query and what came back.
Country explorer
From the API
🇬🇧United Kingdom
- Capital
- London
- Currency
- GBP
- Languages
- English
Only the name: tick a fact to ask for more.
The GraphQL API didn't answer.
That happens with live data: the service might be busy, or your connection dropped. Nothing is broken on this page.
Behind the scenes
One query, one answer
Everything on the card came from a single request to the Countries API (opens in a new tab), a free GraphQL API that needs no key. This is the real query and answer from this page, and they change when you change your choices.
The query
The page always sends a
POSTto the same address. What changes is the query inside it: a list of the fields you ticked.POST
https://countries.trevorblades.com/graphqlquery CountryFacts { country(code: "GB") { name emoji capital currency languages { name } } }The answer
JSON in exactly the shape of the query: the same fields, nested the same way, and nothing else.
Example answer
{ "data": { "country": { "name": "United Kingdom", "emoji": "🇬🇧", "capital": "London", "currency": "GBP", "languages": [ { "name": "English" } ] } } }The same with REST
An imaginary REST version of this API would have an address for each thing. Here's what the same card would cost:
GET /countries/GBThe country, with every field it hasGET /countries/GB/languagesIts languages, in full
2 requests, each sending back everything it has. GraphQL: 1 request, 5 fields.
Which is better? It depends. REST or GraphQL?