Skip to content

Commit 4f1e582

Browse files
committed
initial react app
1 parent a6a474d commit 4f1e582

File tree

12 files changed

+1962
-226
lines changed

12 files changed

+1962
-226
lines changed

README.md

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,50 @@
1-
![Cloudflare GraphQL Gateway](https://www.cloudflare.com/img/products/cloudflare-workers/workers-api-responses.png)
1+
![Cloudflare GraphQL Gateway](/img/workers-react.png)
22

3-
Workers GraphQL Gateway Example
3+
Workers React Example
44
====
5-
Building a GraphQL Gateway allows you to make a single query, have GraphQL and take care of breaking it down to multiple rest calls. This example makes use of [dataloader](https://github.com/facebook/dataloader) to batch and cache the queries so a query with the same key will not be requested twice.
5+
Combining the power of Cloudflare Workers and React will allow you to render the
6+
same React code you would on the browser on Cloudflare Workers.
67

78
### Dependencies
89
- [npm](https://www.npmjs.com/get-npm) or [yarn](https://yarnpkg.com/en/docs/install#debian-stable)
910

1011
### Instructions
1112

12-
- `npm install`
13-
- `npm run build`
13+
- `yarn preview`
1414

15-
To open the Workers preview with the built Worker:
15+
#### Terraform
16+
If you'd like to use terraform to upload your worker scripts, you'll need a
17+
vars file with the following variables
18+
19+
```hcl
20+
# Cloudflare variables
21+
variable "cloudflare_email" {
22+
default = "dmr@bell-labs.com"
23+
}
24+
25+
variable "cloudflare_token" {
26+
default = "00000000000000000000000000"
27+
}
28+
29+
# GCP exmaple variables
30+
variable "project" {
31+
default = "my-project"
32+
}
33+
34+
variable "zone" {
35+
default = "buzzwords.app"
36+
}
37+
38+
variable "bucket" {
39+
default = "buzzwords"
40+
}
41+
```
42+
after adding this file as `vars.tf` (terraform will pickup any `.tf` extension file) do
43+
`terraform init` and `terraform apply`
1644

17-
- `npm run preview`
1845

1946
### About
2047
[Cloudflare Workers](http://developers.cloudflare.com/workers/) allow you to write JavaScript which runs on all of Cloudflare's
2148
150+ global data centers.
2249

23-
[GraphQL](https://graphql.org) provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.
50+
[React](https://reactjs.org) makes it painless to create interactive UIs. Design simple views for each state in your application, and React will efficiently update and render just the right components when your data changes.

deploy.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
provider "cloudflare" {
2+
email = "${var.cloudflare_email}"
3+
token = "${var.cloudflare_token}"
4+
}
5+
6+
provider "google" {
7+
credentials = "${file("storage.json")}"
8+
project = "${var.project}"
9+
}
10+
11+
resource "cloudflare_worker_script" "buzzwords" {
12+
zone = "${var.zone}"
13+
content = "${file("dist/worker.js")}"
14+
}
15+
16+
resource "google_storage_bucket_object" "worker" {
17+
name = "worker.js"
18+
content_encoding = "application/javascript"
19+
content = "${file("dist/worker.js")}"
20+
bucket = "${var.bucket}"
21+
}

img/workers-react.png

5.93 KB
Loading

package.json

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
{
2-
"name": "workers-webpack",
2+
"name": "workers-react-example",
33
"version": "1.0.0",
44
"description": "",
55
"scripts": {
6-
"build": "webpack -p --progress --colors",
7-
"preview": "node ./scripts/open-preview.js"
6+
"build": "webpack -p",
7+
"terraform": "yarn build && terraform apply -auto-approve",
8+
"deploy": "yarn build && node ./scripts/deploy.js",
9+
"preview": "yarn build && node ./scripts/open-preview.js"
810
},
911
"main": "dist/worker.js",
10-
"author": "",
11-
"license": "MIT",
12+
"author": "@sevki",
13+
"license": "BSD",
1214
"devDependencies": {
1315
"node-fetch": "^2.2.0",
1416
"opn": "^5.3.0"
1517
},
1618
"dependencies": {
17-
"dataloader": "^1.4.0",
18-
"graphql": "^0.13.2",
19+
"@babel/core": "^7.1.2",
20+
"@babel/preset-env": "^7.1.0",
21+
"@babel/preset-react": "^7.0.0",
22+
"babel-core": "^6.26.3",
23+
"babel-loader": "^8.0.4",
24+
"babel-preset-env": "^1.7.0",
25+
"react": "^16.6.1",
26+
"react-dom": "^16.6.1",
27+
"react-dom-server": "^0.0.5",
1928
"webpack": "^4.10.2",
2029
"webpack-cli": "^2.1.4"
2130
}

scripts/deploy.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const fs = require("fs");
2+
const util = require("util");
3+
const fetch = require("node-fetch");
4+
const readFile = util.promisify(fs.readFile);
5+
const opn = require("opn");
6+
7+
async function deploy(script) {
8+
let resp = await fetch(
9+
"https://api.cloudflare.com/client/v4/accounts" +
10+
node.env.CLOUDFLARE_ZONE +
11+
"/workers/scripts/" +
12+
node.env.CLOUDFLARE_SCRIPT,
13+
{
14+
method: "PUT",
15+
headers: {
16+
"cache-control": "no-cache",
17+
"content-type": "text/javascript",
18+
"X-Auth-Email": node.env.CLOUDFLARE_EMAIL,
19+
"X-Auth-Key": node.env.CLOUDFLARE_KEY
20+
},
21+
body: script
22+
}
23+
);
24+
25+
let data = await resp.json();
26+
return data;
27+
}
28+
29+
readFile("dist/worker.js", "utf8").then(data => {
30+
deploy(data).then(d => {
31+
console.log(d.errors);
32+
});
33+
});

scripts/open-preview.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function newWorker(script) {
2222
readFile("dist/worker.js", "utf8").then(data => {
2323
newWorker(data).then(id =>
2424
opn(
25-
"https://cloudflareworkers.com/#" + id + ":https://cloudflaregraphql.com",
25+
"https://cloudflareworkers.com/#" + id + ":https://reactjs.org",
2626
{ app: "chromium" }
2727
)
2828
);

src/all.js

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

src/graphql.js

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

0 commit comments

Comments
 (0)