Skip to content

Commit aebd693

Browse files
dependabot[bot]acao
authored andcommitted
feature: set up swapi for netlify build
- update dependencies only as needed - add handler using aws-express-serverless (not ideal but it works!) - move code around to share with a local, normal express server - add build step for netlify-lambda script - add netlify deploy previews - upgrade GraphiQL version
1 parent 0dd89e8 commit aebd693

File tree

15 files changed

+3386
-1940
lines changed

15 files changed

+3386
-1940
lines changed

.babelrc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"presets": [
3-
"env"
4-
],
3+
["env", { "targets": { "node": "current" }}]
4+
],
55
"plugins": [
6-
"transform-flow-strip-types",
7-
"transform-object-rest-spread",
8-
"transform-runtime",
6+
"transform-flow-strip-types",
7+
"syntax-async-functions",
8+
"babel-plugin-syntax-object-rest-spread"
99
]
1010
}

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/lib
2+
/netlify-lambda-lib
23
/public/schema.js
3-
/node_modules
4-
/npm-debug.log
4+
node_modules
5+
npm-debug.log
56
/coverage
67
/cache/data.json

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ A wrapper around [SWAPI](http://swapi.co) built using GraphQL converting it into
66
Uses:
77

88
* [graphql-js](https://github.com/graphql/graphql-js) - a JavaScript GraphQL runtime.
9-
* [DataLoader](https://github.com/facebook/dataloader) - for coalescing and caching fetches.
9+
* [DataLoader](https://github.com/graphql/dataloader) - for coalescing and caching fetches.
1010
* [express-graphql](https://github.com/graphql/express-graphql) - to provide HTTP access to GraphQL.
11+
* [aws-serverless-express](https://github.com/awslabs/aws-serverless-express) - to use `express-graphql` on aws lambda.
1112
* [GraphiQL](https://github.com/graphql/graphiql) - for easy exploration of this GraphQL server.
1213

1314
Try it out at: http://graphql.org/swapi-graphql
@@ -20,24 +21,24 @@ Try it out at: http://graphql.org/swapi-graphql
2021
Install dependencies with
2122

2223
```sh
23-
npm install
24+
yarn
2425
```
2526

2627
## SWAPI Wrapper
2728

2829
The SWAPI wrapper is in `./swapi`. It can be tested with:
2930

3031
```sh
31-
npm test
32+
yarn test
3233
```
3334

3435
## Local Server
3536

3637
A local express server is in `./server`. It can be run with:
3738

3839
```sh
39-
npm run build # Only if you changed something
40-
npm start
40+
yarn build # Only if you changed something
41+
yarn start
4142
```
4243

4344
A GraphiQL instance will be opened at http://localhost:8080/ (or similar; the actual port number will be printed to the console) to explore the API.

handler/index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the license found in the
6+
* LICENSE-examples file in the root directory of this source tree.
7+
*
8+
*/
9+
10+
const awsServerlessExpress = require('aws-serverless-express');
11+
12+
const app = require('../lib/service');
13+
14+
// NOTE: If you get ERR_CONTENT_DECODING_FAILED in your browser, this is likely
15+
// due to a compressed response (e.g. gzip) which has not been handled correctly
16+
// by aws-serverless-express and/or API Gateway. Add the necessary MIME types to
17+
// binaryMimeTypes below, then redeploy (`npm run package-deploy`)
18+
const binaryMimeTypes = [
19+
'application/javascript',
20+
'application/json',
21+
'font/eot',
22+
'font/opentype',
23+
'font/otf',
24+
'image/jpeg',
25+
'image/png',
26+
'image/svg+xml',
27+
'text/css',
28+
'text/html',
29+
'text/javascript',
30+
];
31+
32+
const server = awsServerlessExpress.createServer(app, null, binaryMimeTypes);
33+
34+
exports.handler = (event, context) =>
35+
awsServerlessExpress.proxy(server, event, context);

netlify.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build]
2+
functions = "netlify-lambda-lib"

package.json

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@
2020
"options": {
2121
"mocha": "--require babel-register --require scripts/mocha-bootload src/**/__tests__/*.js"
2222
},
23-
"babel": {
24-
"optional": [
25-
"runtime",
26-
"es7.asyncFunctions"
27-
]
28-
},
2923
"browserify": {
3024
"transform": [
3125
"babelify"
@@ -35,57 +29,56 @@
3529
"react": "global:React"
3630
},
3731
"scripts": {
38-
"postinstall": "npm run download && npm run build",
39-
"test": "npm run lint && npm run check && npm run testonly",
40-
"start": "node lib/server/main.js",
32+
"postinstall": "yarn run download && yarn run build",
33+
"test": "yarn run lint && yarn run check && yarn run testonly",
34+
"start": "node lib/server",
4135
"watch": "babel scripts/watch.js | node",
42-
"testonly": "mocha $npm_package_options_mocha",
43-
"lint": "eslint src",
44-
"lintfix": "eslint --fix src",
36+
"testonly": "mocha $yarn_package_options_mocha",
37+
"lint": "eslint src handler",
38+
"lintfix": "eslint --fix src handler",
4539
"check": "flow check",
4640
"cover": "babel-node node_modules/.bin/isparta cover --root src --report html node_modules/.bin/_mocha -- $npm_package_options_mocha",
47-
"coveralls": "babel-node node_modules/.bin/isparta cover --root src --report lcovonly node_modules/.bin/_mocha -- $npm_package_options_mocha && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
48-
"build": "babel src --optional runtime --ignore __tests__,public --out-dir lib/",
41+
"coveralls": "babel-node node_modules/.bin/isparta cover --root src --report lcovonly node_modules/.bin/_mocha -- $yarn_package_options_mocha && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
42+
"build": "rimraf lib && babel src --ignore __tests__,public --out-dir lib/ && yarn build-lambda",
43+
"build-lambda": "NODE_ENV=development netlify-lambda build handler",
4944
"download": "babel-node scripts/download.js cache/data.json",
50-
"build-public": "browserify --standalone Schema -t babelify --outfile public/schema.js src/schema-proxy.js",
5145
"serve-public": "babel-node scripts/serve-public",
52-
"deploy": "yarn run build-public && scripts/deploy-public",
5346
"prettier": "prettier --write 'src/**/*.js'",
5447
"print-schema": "babel-node scripts/print-schema.js",
5548
"store-schema": "babel-node scripts/store-schema.js"
5649
},
5750
"dependencies": {
51+
"aws-serverless-express": "^3.3.6",
5852
"babel-runtime": "^6.26.0",
59-
"cors": "^2.8.4",
53+
"cors": "^2.8.5",
6054
"dataloader": "1.4.0",
61-
"express": "^4.16.3",
62-
"express-graphql": "^0.6.12",
63-
"graphql": "0.13.2",
64-
"graphql-relay": "0.5.5"
55+
"express": "^4.17.1",
56+
"express-graphql": "^0.9.0",
57+
"graphql": "14.5.8",
58+
"graphql-relay": "0.6.0"
6559
},
6660
"devDependencies": {
6761
"babel-cli": "^6.26.0",
68-
"babel-core": "^6.26.0",
69-
"babel-eslint": "^8.2.2",
62+
"babel-core": "^6.26.3",
63+
"babel-eslint": "^10.0.3",
7064
"babel-plugin-syntax-async-functions": "6.13.0",
65+
"babel-plugin-syntax-object-rest-spread": "^6.13.0",
7166
"babel-plugin-transform-flow-strip-types": "^6.22.0",
7267
"babel-plugin-transform-object-rest-spread": "^6.26.0",
7368
"babel-plugin-transform-runtime": "^6.23.0",
74-
"babel-preset-env": "^1.6.1",
69+
"babel-preset-env": "^1.7.0",
7570
"babel-register": "^6.26.0",
76-
"babelify": "^8.0.0",
77-
"browserify": "^15.0.0",
78-
"browserify-shim": "^3.8.10",
79-
"chai": "^4.1.2",
80-
"coveralls": "^3.0.0",
81-
"eslint": "^4.19.1",
82-
"eslint-plugin-babel": "5.0.0",
83-
"eslint-plugin-prettier": "^2.6.0",
71+
"chai": "^4.2.0",
72+
"coveralls": "^3.0.4",
73+
"eslint": "^5.16.0",
74+
"eslint-plugin-babel": "5.3.0",
75+
"eslint-plugin-prettier": "^3.1.0",
8476
"flow-bin": "^0.69.0",
8577
"isomorphic-fetch": "2.2.1",
86-
"isparta": "^4.0.0",
87-
"mocha": "^5.0.5",
88-
"prettier": "^1.11.1",
89-
"sane": "^2.5.0"
78+
"isparta": "^4.1.1",
79+
"mocha": "^6.1.4",
80+
"netlify-lambda": "^1.6.3",
81+
"prettier": "^1.18.2",
82+
"sane": "^4.1.0"
9083
}
9184
}

public/index.html

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
}
2424
</style>
2525
<link rel="icon" href="favicon.ico">
26-
<link href="//cdn.jsdelivr.net/npm/graphiql@0.11.11/graphiql.css" rel="stylesheet" />
26+
<link type="text/css" href="//unpkg.com/graphiql@0.17.0/graphiql.css" rel="stylesheet" />
2727
</head>
2828
<body>
2929
<div id="splash">
@@ -32,8 +32,7 @@
3232
<script src="//cdn.jsdelivr.net/es6-promise/4.0.5/es6-promise.auto.min.js"></script>
3333
<script src="//cdn.jsdelivr.net/react/15.4.2/react.min.js"></script>
3434
<script src="//cdn.jsdelivr.net/react/15.4.2/react-dom.min.js"></script>
35-
<script src="//cdn.jsdelivr.net/npm/[email protected]/graphiql.min.js"></script>
36-
<script src="schema.js"></script>
35+
<script src="//unpkg.com/[email protected]/graphiql.min.js"></script>
3736
<script>
3837
// Parse the search string to get url parameters.
3938
var search = window.location.search;
@@ -81,10 +80,26 @@
8180
history.replaceState(null, null, newSearch);
8281
}
8382

84-
function graphQLFetcher(graphQLParams) {
85-
const { query, variables, operationName } = graphQLParams;
86-
return Schema.execute(query, variables, operationName);
87-
}
83+
function graphQLFetcher(graphQLParams) {
84+
// This example expects a GraphQL server at the path /graphql.
85+
// Change this to point wherever you host your GraphQL server.
86+
return fetch(parameters.fetchURL || '/.netlify/functions/index', {
87+
method: 'post',
88+
headers: {
89+
'Accept': 'application/json',
90+
'Content-Type': 'application/json'
91+
},
92+
body: JSON.stringify(graphQLParams),
93+
}).then(function (response) {
94+
return response.text();
95+
}).then(function (responseBody) {
96+
try {
97+
return JSON.parse(responseBody);
98+
} catch (error) {
99+
return responseBody;
100+
}
101+
});
102+
}
88103

89104
// Render <GraphiQL /> into the body.
90105
ReactDOM.render(

scripts/serve-public.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import express from 'express';
1111

1212
const app = express();
13-
app.use(express.static('./public'))
13+
app.use(express.static('./public'));
1414

1515
// Listen for incoming HTTP requests
1616
const listener = app.listen(() => {

src/schema-proxy.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)