Use a netlify lambda for the API

This commit is contained in:
Trevor Blades
2020-02-06 12:31:44 -08:00
parent 541614b125
commit b89b2acdd7
9 changed files with 376 additions and 2769 deletions

2
.gitignore vendored
View File

@@ -1,2 +1,2 @@
.env
node_modules
.netlify

View File

@@ -1 +0,0 @@
web: node -r esm index.js

View File

@@ -1,7 +1,51 @@
import provinces from 'provinces';
import {continents, countries, languages} from 'countries-list';
const provinces = require('provinces');
const {ApolloServer, gql} = require('apollo-server-lambda');
const {continents, countries, languages} = require('countries-list');
export default {
const typeDefs = gql`
type Continent {
code: String
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]
}
type State {
code: String
name: String
country: Country
}
type Language {
code: String
name: String
native: String
rtl: Int
}
type Query {
continents: [Continent]
continent(code: String): Continent
countries: [Country]
country(code: String): Country
languages: [Language]
language(code: String): Language
}
`;
const resolvers = {
Country: {
continent({continent}) {
return {
@@ -81,3 +125,15 @@ export default {
}
}
};
const server = new ApolloServer({
typeDefs,
resolvers,
introspection: true,
playground: true,
engine: {
apiKey: process.env.ENGINE_API_KEY
}
});
exports.handler = server.createHandler();

View File

@@ -1,17 +0,0 @@
import resolvers from './resolvers';
import typeDefs from './schema';
import {ApolloServer} from 'apollo-server';
const server = new ApolloServer({
typeDefs,
resolvers,
introspection: true,
playground: true,
engine: {
apiKey: process.env.ENGINE_API_KEY
}
});
server.listen({port: process.env.PORT}).then(({url}) => {
console.log(`🚀 Server ready at ${url}`);
});

2
netlify.toml Normal file
View File

@@ -0,0 +1,2 @@
[build]
functions = "functions"

3000
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,27 +1,21 @@
{
"engines": {
"node": "10"
},
"scripts": {
"pretest": "eslint index.js",
"test": "echo \"Error: no test specified\" && exit",
"start": "nodemon -r dotenv/config -r esm index.js"
"pretest": "eslint functions",
"test": "echo \"Error: no test specified\" && exit"
},
"eslintConfig": {
"extends": "@trevorblades"
},
"dependencies": {
"apollo-server": "^2.7.0",
"apollo-server-lambda": "^2.10.0",
"countries-list": "^2.4.3",
"esm": "^3.2.25",
"graphql": "^14.4.2",
"graphql": "^14.6.0",
"provinces": "^1.11.0"
},
"devDependencies": {
"@trevorblades/eslint-config": "^7.0.1",
"apollo": "^2.16.0",
"dotenv": "^6.1.0",
"eslint": "^6.2.1",
"nodemon": "^1.18.7"
"eslint": "^6.2.1"
}
}

View File

@@ -1,44 +0,0 @@
import {gql} from 'apollo-server';
export default gql`
type Continent {
code: String
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]
}
type State {
code: String
name: String
country: Country
}
type Language {
code: String
name: String
native: String
rtl: Int
}
type Query {
continents: [Continent]
continent(code: String): Continent
countries: [Country]
country(code: String): Country
languages: [Language]
language(code: String): Language
}
`;

1
static/_redirects Normal file
View File

@@ -0,0 +1 @@
/ /.netlify/functions/graphql