This is a project which demonstrates GraphQL.
It uses Apollo Server with Express, and GraphQL-Request and Apollo Client as GraphQL clients. The application is used to explain queries, mutations, custom object types, authentication, etc.
- Download a copy of pre-built binaries from the official download page. Or by apt install in Ubuntu Linux.
apt install sqlite3
- In Terminal / Command Prompt, create an empty database, namely e.g. country.db.
sqlite3 country.db
- Import table schema from file.
.read create_tables.sql
- Insert data from file.
.read insert_data.sql
A Node.js express server responding to the queries for countries using Apollo GraphQL server.
-
Create package.json.
{ "name": "country-list-server", "private": true, "license": "MIT", "type": "module", "scripts": { "start": "nodemon server.js" }, "dependencies": { "better-sqlite3": "^8.3.0", "@apollo/server": "^4.7.5", "graphql": "^16.7.1", "knex": "^2.4.2" }, "devDependencies": { "nodemon": "^2.0.22" }, "nodemonConfig": { "ext": "graphql,js" } }
-
Install dependencies.
npm install
-
Create server.js.
-
Start server.
npm start
-
Open GraphQL Explorer at
http://localhost:9000/graphql
in browser. -
Get all countries at
http://localhost:9000/countries
. -
Get country by code at
http://localhost:9000/country?code=HK
. -
To get specific fields of all countries, type the following command in Terminal / Command Prompt:
export GRAPHQL_SERVER=http://localhost:9000
curl --request POST \
--header 'content-type: application/json' \
--url "$GRAPHQL_SERVER" \
--data '{"query":"query { countries { countryCode isoCode isoCodeA3 nameEn } }"}' | jq
- To search for a specific country by ISO Code, type the following command in Terminal / Command Prompt:
curl --request POST \
--header 'content-type: application/json' \
--url "$GRAPHQL_SERVER" \
--data '{"query":"query { country(isoCode: \"HK\") { countryCode isoCode isoCodeA3 nameEn } }"}' | jq
Deployed to Vercel.
-
Open landing page at
https://country-list-graphql.vercel.app/
in browser. -
Get all countries at
https://country-list-graphql.vercel.app/countries
. -
Get country by code at
https://country-list-graphql.vercel.app/country?code=HK
. -
To search for a specific country by ISO Code, type the following command in Terminal / Command Prompt:
export GRAPHQL_SERVER=https://country-list-graphql.vercel.app
curl --request POST \
--header 'content-type: application/json' \
--url "$GRAPHQL_SERVER" \
--data '{"query":"query { country(isoCode: \"HK\") { countryCode isoCode isoCodeA3 nameEn } }"}' | jq