Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Commit 70ebded

Browse files
author
sw-yx
committed
add graphql rest wrapper and remove chuck norris api
1 parent 80ab040 commit 70ebded

File tree

6 files changed

+129
-13
lines changed

6 files changed

+129
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
name: "apollo-graphql-rest",
3+
description:
4+
"GraphQL function to wrap REST API using apollo-server-lambda and apollo-datasource-rest!"
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const { ApolloServer, gql } = require("apollo-server-lambda");
2+
const RandomUser = require("./random-user.js");
3+
// example from: https://medium.com/yld-engineering-blog/easier-graphql-wrappers-for-your-rest-apis-1410b0b5446d
4+
5+
const typeDefs = gql`
6+
"""
7+
Example Description for Name Type
8+
9+
It's multiline and you can use **markdown**! [more docs](https://www.apollographql.com/docs/apollo-server/essentials/schema#documentation)!
10+
"""
11+
type Name {
12+
"Description for first"
13+
title: String
14+
"Description for title"
15+
first: String
16+
"Description for last"
17+
last: String
18+
}
19+
type Location {
20+
street: String
21+
city: String
22+
state: String
23+
postcode: String
24+
}
25+
type Picture {
26+
large: String
27+
medium: String
28+
thumbnail: String
29+
}
30+
type User {
31+
gender: String
32+
name: Name
33+
location: Location
34+
email: String
35+
phone: String
36+
cell: String
37+
picture: Picture
38+
nat: String
39+
}
40+
type Query {
41+
"""
42+
Example Description for getUser
43+
44+
It's multiline and you can use **markdown**!
45+
"""
46+
getUser(gender: String): User
47+
getUsers(people: Int, gender: String): [User]
48+
}
49+
`;
50+
const resolvers = {
51+
Query: {
52+
getUser: async (_, { gender }, { dataSources }) =>
53+
dataSources.RandomUser.getUser(gender),
54+
getUsers: async (_, { people, gender }, { dataSources }) =>
55+
dataSources.RandomUser.getUsers(people, gender)
56+
}
57+
};
58+
59+
const server = new ApolloServer({
60+
typeDefs,
61+
resolvers,
62+
dataSources: () => ({
63+
RandomUser: new RandomUser()
64+
})
65+
});
66+
67+
exports.handler = server.createHandler();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "apollo-graphql-rest",
3+
"version": "1.0.0",
4+
"description": "netlify functions:create - GraphQL function to wrap REST API using apollo-server-lambda and apollo-datasource-rest!",
5+
"main": "apollo-graphql-rest.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"netlify",
11+
"serverless",
12+
"js",
13+
"apollo"
14+
],
15+
"author": "Netlify",
16+
"license": "MIT",
17+
"dependencies": {
18+
"apollo-server-lambda": "^2.4.8",
19+
"apollo-datasource-rest": "^0.3.2",
20+
"graphql": "^14.1.1"
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { RESTDataSource } = require("apollo-datasource-rest");
2+
3+
class RandomUser extends RESTDataSource {
4+
constructor() {
5+
super();
6+
this.baseURL = "https://randomuser.me/api";
7+
}
8+
9+
async getUser(gender = "all") {
10+
const user = await this.get(`/?gender=${gender}`);
11+
return user.results[0];
12+
}
13+
14+
async getUsers(people = 10, gender = "all") {
15+
const user = await this.get(`/?results=${people}&gender=${gender}`);
16+
return user.results;
17+
}
18+
}
19+
20+
module.exports = RandomUser;

src/functions-templates/js/graphql-gateway/example-sibling-function-graphql-2.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,39 @@ const books = [
2626
},
2727
{
2828
id: 2,
29-
title: "The Chamber of Secrets",
30-
year: 1998,
29+
title: "Pet Sematary",
30+
year: 1983,
3131
authorName: "Stephen King"
3232
},
3333
{
3434
id: 3,
35-
title: "The Prisoner of Azkaban",
36-
year: 1999,
35+
title: "Going Postal",
36+
year: 2004,
3737
authorName: "Terry Pratchett"
3838
},
3939
{
4040
id: 4,
41-
title: "The Goblet of Fire",
42-
year: 2000,
41+
title: "Small Gods",
42+
year: 1992,
4343
authorName: "Terry Pratchett"
4444
},
4545
{
4646
id: 5,
47-
title: "The Order of the Phoenix",
48-
year: 2003,
47+
title: "Night Watch",
48+
year: 2002,
4949
authorName: "Terry Pratchett"
5050
},
5151
{
5252
id: 6,
53-
title: "The Half-Blood Prince",
54-
year: 2005,
53+
title: "The Shining",
54+
year: 1977,
5555
authorName: "Stephen King"
5656
},
5757
{
5858
id: 7,
5959
title: "The Deathly Hallows",
6060
year: 2007,
61-
authorName: "Stephen King"
61+
authorName: "JK Rowling"
6262
}
6363
];
6464

src/functions-templates/js/node-fetch/node-fetch.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const fetch = require("node-fetch");
22
exports.handler = async function(event, context) {
33
try {
4-
const response = await fetch("https://api.chucknorris.io/jokes/random");
4+
const response = await fetch("https://icanhazdadjoke.com", {
5+
headers: { Accept: "application/json" }
6+
});
57
if (!response.ok) {
68
// NOT res.status >= 200 && res.status < 300
79
return { statusCode: response.status, body: response.statusText };
@@ -10,7 +12,7 @@ exports.handler = async function(event, context) {
1012

1113
return {
1214
statusCode: 200,
13-
body: JSON.stringify({ msg: data.value })
15+
body: JSON.stringify({ msg: data.joke })
1416
};
1517
} catch (err) {
1618
console.log(err); // output to netlify function log

0 commit comments

Comments
 (0)