Change code to ID

This commit is contained in:
Trevor Blades
2020-03-27 09:10:43 -07:00
parent 84a1a9229a
commit 9e83568a16
2 changed files with 26 additions and 46 deletions

View File

@@ -19,7 +19,6 @@ A public GraphQL API for information about countries, continents, and languages.
3. Each `Country` has an array of `states` populated by their states/provinces, if any. 3. Each `Country` has an array of `states` populated by their states/provinces, if any.
- [Writing queries](#writing-queries) - [Writing queries](#writing-queries)
- [Migration notes (pre-March 2020)](#migration-notes-pre-march-2020)
- [Docs](#docs) - [Docs](#docs)
- [Examples](#examples) - [Examples](#examples)
- [License](#license) - [License](#license)
@@ -28,7 +27,7 @@ A public GraphQL API for information about countries, continents, and languages.
```graphql ```graphql
{ {
country(code: BR) { country(code: "BR") {
name name
native native
capital capital
@@ -64,20 +63,6 @@ The above GraphQL query will produce the following JSON response:
} }
``` ```
### Migration notes (pre-March 2020)
In the first version of this API, `code` arguments were treated as `String`s. Now, they are enums of valid options only. To migrate, simply remove the double quotes from your existing code arguments:
```diff
{
- country(code: "CA") {
+ country(code: CA) {
name
emoji
}
}
```
## Docs ## Docs
Check out [the playground](https://countries.trevorblades.com) to explore the schema and test out some queries. Check out [the playground](https://countries.trevorblades.com) to explore the schema and test out some queries.

View File

@@ -3,26 +3,14 @@ const {ApolloServer, gql} = require('apollo-server');
const {continents, countries, languages} = require('countries-list'); const {continents, countries, languages} = require('countries-list');
const typeDefs = gql` const typeDefs = gql`
enum ContinentCode {
${Object.keys(continents)}
}
enum CountryCode {
${Object.keys(countries)}
}
enum LanguageCode {
${Object.keys(languages)}
}
type Continent { type Continent {
code: ContinentCode! code: ID!
name: String! name: String!
countries: [Country!]! countries: [Country!]!
} }
type Country { type Country {
code: CountryCode! code: ID!
name: String! name: String!
native: String! native: String!
phone: String! phone: String!
@@ -42,7 +30,7 @@ const typeDefs = gql`
} }
type Language { type Language {
code: LanguageCode! code: ID!
name: String name: String
native: String native: String
rtl: Boolean! rtl: Boolean!
@@ -50,11 +38,11 @@ const typeDefs = gql`
type Query { type Query {
continents: [Continent!]! continents: [Continent!]!
continent(code: ContinentCode!): Continent! continent(code: ID!): Continent
countries: [Country!]! countries: [Country!]!
country(code: CountryCode!): Country! country(code: ID!): Country
languages: [Language!]! languages: [Language!]!
language(code: LanguageCode!): Language! language(code: ID!): Language
} }
`; `;
@@ -104,10 +92,13 @@ const resolvers = {
}, },
Query: { Query: {
continent(parent, {code}) { continent(parent, {code}) {
return { const name = continents[code];
code, return (
name: continents[code] name && {
}; code,
name
}
);
}, },
continents() { continents() {
return Object.entries(continents).map(([code, name]) => ({ return Object.entries(continents).map(([code, name]) => ({
@@ -117,10 +108,12 @@ const resolvers = {
}, },
country(parent, {code}) { country(parent, {code}) {
const country = countries[code]; const country = countries[code];
return { return (
...country, country && {
code ...country,
}; code
}
);
}, },
countries() { countries() {
return Object.entries(countries).map(([code, country]) => ({ return Object.entries(countries).map(([code, country]) => ({
@@ -130,10 +123,12 @@ const resolvers = {
}, },
language(parent, {code}) { language(parent, {code}) {
const language = languages[code]; const language = languages[code];
return { return (
...language, language && {
code ...language,
}; code
}
);
}, },
languages() { languages() {
return Object.entries(languages).map(([code, language]) => ({ return Object.entries(languages).map(([code, language]) => ({