Skip to content

Commit a43fb28

Browse files
committed
add cloud function
1 parent 1c73d41 commit a43fb28

File tree

8 files changed

+1162
-35
lines changed

8 files changed

+1162
-35
lines changed

.firebaserc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "rga-mocked-apis"
4+
}
5+
}

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ This exercise is part of the [React GraphQL Academy](http://reactgraphql.academy
1010

1111
## Exercise part 1
1212

13-
[](TODO DEPLOY URL)
13+
Given the following [GraphQL API](https://us-central1-rga-mocked-apis.cloudfunctions.net/graphql) / https://api.reactgraphql.academy/graphql:
1414

15-
- Query a list with all the training titles
16-
- Query how many training are in the system?
17-
- Query a single training by id (try id equals TODO ADD ONE????????) and get its name
15+
- Query a list with all the training and retrieve the title and language for each
16+
- Query a single discount by id (try id equals `dis:422`) and get its name
17+
- Query how many languages are in the system?
1818
- How many types do we have in the system?
1919

2020
## Exercise part 2
@@ -248,6 +248,8 @@ query getDangerousDiscount {
248248

249249
Once implemented, do you see any problems/ vulnerability issues on that query?
250250

251+
🤸🏾Do you want some extra workout? Create an [enumeration](https://graphql.org/learn/schema/#enumeration-types) for the languages. Add field language to the Training object type that uses the language enum.
252+
251253
## Homework
252254

253255
You are going to build a GraphQL API on top of an existing REST API. Steps:

firebase.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"functions": {
3+
"source": "."
4+
},
5+
"hosting": {
6+
"rewrites": [
7+
{
8+
"source": "/graphql",
9+
"function": "graphql"
10+
}
11+
]
12+
}
13+
}

index-dev.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const { createServer } = require("./src/server");
2+
3+
const server = createServer({ playground: true, introspection: true }).listen(
4+
4000,
5+
() => {
6+
const { address, port } = server.address();
7+
console.log(`🚀 Server ready at ${address}:${port}`);
8+
}
9+
);

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const functions = require("firebase-functions");
2+
const { createServer } = require("./src/server");
3+
4+
// Set up the server and export it for the GCF API (it requires an endpoint to be exported; in this case, 'api')
5+
exports.graphql = functions.https.onRequest(
6+
createServer({ playground: true, introspection: true })
7+
);

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version": "1.0.0",
44
"description": "",
55
"scripts": {
6-
"start": "node_modules/.bin/nodemon src/index.js",
6+
"start": "node_modules/.bin/nodemon index-dev.js",
7+
"deploy": "firebase deploy --only functions",
78
"start-babel": "node_modules/.bin/nodemon -- node_modules/.bin/babel-node src/index.js",
89
"test": "echo \"Error: no test specified\" && exit 1"
910
},
@@ -19,6 +20,10 @@
1920
"homepage": "https://github.com/reactgraphqlacademy/rest-to-graphql-workshop#readme",
2021
"dependencies": {
2122
"apollo-server": "^2.11.0",
23+
"apollo-server-express": "^2.11.0",
24+
"express": "^4.17.1",
25+
"firebase-admin": "^8.10.0",
26+
"firebase-functions": "^3.5.0",
2227
"graphql": "^14.6.0",
2328
"graphql-iso-date": "^3.6.1",
2429
"graphql-relay": "^0.6.0",
@@ -28,5 +33,8 @@
2833
"@babel/core": "^7.4.5",
2934
"@babel/node": "^7.4.5",
3035
"nodemon": "^1.19.1"
36+
},
37+
"engines": {
38+
"node": "8"
3139
}
3240
}

src/index.js renamed to src/server.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
const express = require("express");
12
const fetch = require("node-fetch");
2-
const { ApolloServer, gql } = require("apollo-server");
3+
const { ApolloServer, gql } = require("apollo-server-express");
34
const { GraphQLDateTime } = require("graphql-iso-date");
45

56
const typeDefs = gql`
@@ -11,6 +12,7 @@ const typeDefs = gql`
1112
overview: String
1213
discounts: [Discount]
1314
startDate: DateTime
15+
language: Language
1416
}
1517
1618
type Discount {
@@ -23,6 +25,19 @@ const typeDefs = gql`
2325
2426
scalar DateTime
2527
28+
enum Language {
29+
EN
30+
ES
31+
FR
32+
IT
33+
PT
34+
NL
35+
DE
36+
ZH
37+
JA
38+
RU
39+
}
40+
2641
type Query {
2742
trainings: [Training!]
2843
training(id: ID!): Training
@@ -49,19 +64,19 @@ const resolvers = {
4964

5065
function fetchTrainings() {
5166
// More info about the fetch function? https://github.com/bitinn/node-fetch#json
52-
return fetch("https://restapi.reactgraphql.academy/v1/trainings/")
67+
return fetch("https://api.reactgraphql.academy/api/rest/trainings/")
5368
.then(res => res.json())
5469
.catch(error => console.log(error));
5570
}
5671

5772
function fetchTrainingById(id) {
58-
return fetch(`https://restapi.reactgraphql.academy/v1/trainings/${id}`)
73+
return fetch(`https://api.reactgraphql.academy/api/rest/trainings/${id}`)
5974
.then(res => res.json())
6075
.catch(error => console.log(error));
6176
}
6277

6378
function fetchDiscounts() {
64-
return fetch("https://restapi.reactgraphql.academy/v1/discounts/")
79+
return fetch("https://api.reactgraphql.academy/api/rest/discounts/")
6580
.then(res => res.json())
6681
.catch(error => console.log(error));
6782
}
@@ -73,7 +88,7 @@ function fetchTrainingByUrl(url) {
7388
}
7489

7590
function fetchDiscountById(id) {
76-
return fetch(`https://restapi.reactgraphql.academy/v1/discounts/${id}`)
91+
return fetch(`https://api.reactgraphql.academy/api/rest/discounts/${id}`)
7792
.then(res => res.json())
7893
.catch(error => console.log(error));
7994
}
@@ -84,13 +99,15 @@ function fetchDiscountByUrl(url) {
8499
.catch(error => console.log(error));
85100
}
86101

87-
// In the most basic sense, the ApolloServer can be started
88-
// by passing type definitions (typeDefs) and the resolvers
89-
// responsible for fetching the data for those types.
90-
const server = new ApolloServer({ typeDefs, resolvers });
102+
module.exports = {
103+
createServer: options => {
104+
const app = express();
105+
const apollo = new ApolloServer({ typeDefs, resolvers, ...options });
106+
apollo.applyMiddleware({
107+
app,
108+
path: `/`
109+
});
91110

92-
// This `listen` method launches a web-server. Existing apps
93-
// can utilize middleware options, which we'll discuss later.
94-
server.listen().then(({ url }) => {
95-
console.log(`🚀 Server ready at ${url}`);
96-
});
111+
return app;
112+
}
113+
};

0 commit comments

Comments
 (0)