Skip to content

Instantly share code, notes, and snippets.

@msroot
Created July 14, 2020 14:15
Show Gist options
  • Save msroot/bad6a8d4cfef4576a1eed24df45a4df0 to your computer and use it in GitHub Desktop.
Save msroot/bad6a8d4cfef4576a1eed24df45a4df0 to your computer and use it in GitHub Desktop.
const { ApolloServer, gql } = require('apollo-server')
const { RESTDataSource } = require('apollo-datasource-rest')
class CountriesAPI extends RESTDataSource {
constructor() {
super()
this.baseURL = 'https://restcountries.eu/rest/v2'
}
async getAllCountries() {
return this.get('/all')
}
}
const typeDefs = gql`
type Country {
name: String!
capital: String!
population: Int!
}
type Query {
allCountries: [Country!]!
}
`
const resolvers = {
Query: {
allCountries: async (parent, args, { dataSources }) => {
return dataSources.countriesAPI.getAllCountries()
},
},
}
const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => {
return {
countriesAPI: new CountriesAPI(),
}
},
})
server.listen().then(({ url }) => console.log(`Server running at ${url}`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment