Back to Projects

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

What do you want to know?

The name always comes back. Tick or untick the rest and the query changes to match.

From the API

🇬🇧United Kingdom

Capital
London
Currency
GBP
Languages
English

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.

  1. The query

    The page always sends a POST to the same address. What changes is the query inside it: a list of the fields you ticked.

    POST https://countries.trevorblades.com/graphql

    query CountryFacts {
      country(code: "GB") {
        name
        emoji
        capital
        currency
        languages {
          name
        }
      }
    }
  2. 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" }
          ]
        }
      }
    }
  3. 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:

    1. GET /countries/GBThe country, with every field it has
    2. GET /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?