mirror of
https://github.com/FranP-code/countries.git
synced 2025-10-13 00:02:15 +00:00
Merge pull request #15 from trevorblades/enums
IDs, Booleans, and nullability
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1 @@
|
||||
node_modules
|
||||
.env
|
||||
|
||||
18
README.md
18
README.md
@@ -1,14 +1,21 @@
|
||||
<img align="right" src="https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/apple/155/earth-globe-americas_1f30e.png" alt="globe" width="120">
|
||||
<p align="center">
|
||||
<img src="./logo.png" alt="globe" width="150">
|
||||
</p>
|
||||
|
||||
# Countries GraphQL API
|
||||
<h1 align="center">Countries GraphQL API</h1>
|
||||
|
||||
<div align="center">
|
||||
|
||||
[](https://github.com/trevorblades/countries/actions)
|
||||
[](CODE_OF_CONDUCT.md)
|
||||
[](https://twitter.com/trevorblades)
|
||||
|
||||
</div>
|
||||
|
||||
A public GraphQL API for information about countries, continents, and languages. This project uses [Countries List](https://annexare.github.io/Countries/) and [`provinces`](https://github.com/substack/provinces) as data sources, so the schema follows the shape of that data, with a few exceptions:
|
||||
|
||||
1. The codes used to key the objects in the original data are available as a `code` property on each item returned from the API.
|
||||
2. The `continent` and `languages` properties are now objects and arrays of objects, respectively.
|
||||
2. The `country.continent` and `country.languages` are now objects and arrays of objects, respectively.
|
||||
3. Each `Country` has an array of `states` populated by their states/provinces, if any.
|
||||
|
||||
## Writing queries
|
||||
@@ -18,6 +25,7 @@ A public GraphQL API for information about countries, continents, and languages.
|
||||
country(code: "BR") {
|
||||
name
|
||||
native
|
||||
capital
|
||||
emoji
|
||||
currency
|
||||
languages {
|
||||
@@ -36,6 +44,7 @@ The above GraphQL query will produce the following JSON response:
|
||||
"country": {
|
||||
"name": "Brazil",
|
||||
"native": "Brasil",
|
||||
"capital": "Brasília",
|
||||
"emoji": "🇧🇷",
|
||||
"currency": "BRL",
|
||||
"languages": [
|
||||
@@ -49,6 +58,8 @@ The above GraphQL query will produce the following JSON response:
|
||||
}
|
||||
```
|
||||
|
||||
## Docs
|
||||
|
||||
Check out [the playground](https://countries.trevorblades.com) to explore the schema and test out some queries.
|
||||
|
||||
## Examples
|
||||
@@ -58,6 +69,7 @@ Check out [the playground](https://countries.trevorblades.com) to explore the sc
|
||||
- [ReasonML](https://medium.com/@idkjs/reasonml-and-graphql-without-graphql-part-1-192c2e9e349c)
|
||||
- [Country quiz app](https://github.com/byrichardpowell/Country-Quiz) (React, TypeScript)
|
||||
- [Python](./examples/python)
|
||||
- [Seed](https://github.com/seed-rs/seed/tree/master/examples/graphql)
|
||||
|
||||
## License
|
||||
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
module.exports = {
|
||||
endpoint: {
|
||||
url: `http://localhost:${process.env.PORT}`
|
||||
}
|
||||
};
|
||||
85
index.js
85
index.js
@@ -4,44 +4,45 @@ const {continents, countries, languages} = require('countries-list');
|
||||
|
||||
const typeDefs = gql`
|
||||
type Continent {
|
||||
code: String
|
||||
name: String
|
||||
countries: [Country]
|
||||
code: ID!
|
||||
name: String!
|
||||
countries: [Country!]!
|
||||
}
|
||||
|
||||
type Country {
|
||||
code: String
|
||||
name: String
|
||||
native: String
|
||||
phone: String
|
||||
continent: Continent
|
||||
currency: String
|
||||
languages: [Language]
|
||||
emoji: String
|
||||
emojiU: String
|
||||
states: [State]
|
||||
code: ID!
|
||||
name: String!
|
||||
native: String!
|
||||
phone: String!
|
||||
continent: Continent!
|
||||
capital: String!
|
||||
currency: String!
|
||||
languages: [Language!]!
|
||||
emoji: String!
|
||||
emojiU: String!
|
||||
states: [State!]!
|
||||
}
|
||||
|
||||
type State {
|
||||
code: String
|
||||
name: String
|
||||
country: Country
|
||||
name: String!
|
||||
country: Country!
|
||||
}
|
||||
|
||||
type Language {
|
||||
code: String
|
||||
code: ID!
|
||||
name: String
|
||||
native: String
|
||||
rtl: Int
|
||||
rtl: Boolean!
|
||||
}
|
||||
|
||||
type Query {
|
||||
continents: [Continent]
|
||||
continent(code: String!): Continent
|
||||
countries: [Country]
|
||||
country(code: String!): Country
|
||||
languages: [Language]
|
||||
language(code: String!): Language
|
||||
continents: [Continent!]!
|
||||
continent(code: ID!): Continent
|
||||
countries: [Country!]!
|
||||
country(code: ID!): Country
|
||||
languages: [Language!]!
|
||||
language(code: ID!): Language
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -84,12 +85,20 @@ const resolvers = {
|
||||
}));
|
||||
}
|
||||
},
|
||||
Language: {
|
||||
rtl(language) {
|
||||
return Boolean(language.rtl);
|
||||
}
|
||||
},
|
||||
Query: {
|
||||
continent(parent, {code}) {
|
||||
return {
|
||||
code,
|
||||
name: continents[code]
|
||||
};
|
||||
const name = continents[code];
|
||||
return (
|
||||
name && {
|
||||
code,
|
||||
name
|
||||
}
|
||||
);
|
||||
},
|
||||
continents() {
|
||||
return Object.entries(continents).map(([code, name]) => ({
|
||||
@@ -99,10 +108,12 @@ const resolvers = {
|
||||
},
|
||||
country(parent, {code}) {
|
||||
const country = countries[code];
|
||||
return {
|
||||
...country,
|
||||
code
|
||||
};
|
||||
return (
|
||||
country && {
|
||||
...country,
|
||||
code
|
||||
}
|
||||
);
|
||||
},
|
||||
countries() {
|
||||
return Object.entries(countries).map(([code, country]) => ({
|
||||
@@ -112,10 +123,12 @@ const resolvers = {
|
||||
},
|
||||
language(parent, {code}) {
|
||||
const language = languages[code];
|
||||
return {
|
||||
...language,
|
||||
code
|
||||
};
|
||||
return (
|
||||
language && {
|
||||
...language,
|
||||
code
|
||||
}
|
||||
);
|
||||
},
|
||||
languages() {
|
||||
return Object.entries(languages).map(([code, language]) => ({
|
||||
@@ -136,6 +149,6 @@ const server = new ApolloServer({
|
||||
}
|
||||
});
|
||||
|
||||
server.listen({port: process.env.PORT}).then(({url}) => {
|
||||
server.listen({port: process.env.PORT || 4000}).then(({url}) => {
|
||||
console.log(`🚀 Server ready at ${url}`);
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"scripts": {
|
||||
"start": "nodemon -r dotenv/config index.js",
|
||||
"pretest": "eslint index.js apollo.config.js",
|
||||
"start": "nodemon index.js",
|
||||
"pretest": "eslint index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit"
|
||||
},
|
||||
"eslintConfig": {
|
||||
|
||||
Reference in New Issue
Block a user