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

Commit 56e3069

Browse files
authored
Merge pull request #80 from netlify/addFaunaGraphQL
add working fauna graphql template
2 parents b47e282 + feb0709 commit 56e3069

File tree

6 files changed

+147
-30
lines changed

6 files changed

+147
-30
lines changed

package-lock.json

Lines changed: 11 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const execa = require("execa");
2+
module.exports = {
3+
name: "fauna-graphql-dev",
4+
description: "GraphQL function using Fauna DB [Dev Testing only]",
5+
addons: [
6+
{
7+
addonName: "fauna",
8+
addonDidInstall(fnPath) {
9+
require("fs").chmodSync(fnPath + "/sync-schema.js", 0o777);
10+
execa.sync(fnPath + "/sync-schema.js", undefined, {
11+
env: process.env,
12+
stdio: "inherit"
13+
});
14+
}
15+
}
16+
],
17+
onComplete() {
18+
console.log(`fauna-graphql function created from template!`);
19+
}
20+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const { ApolloServer, gql } = require("apollo-server-lambda");
2+
const { createHttpLink } = require("apollo-link-http");
3+
const fetch = require("node-fetch");
4+
const {
5+
introspectSchema,
6+
makeRemoteExecutableSchema
7+
} = require("graphql-tools");
8+
9+
exports.handler = function(event, context, cb) {
10+
/** required for Fauna GraphQL auth */
11+
if (!process.env.FAUNADB_SERVER_SECRET) {
12+
const msg = `
13+
FAUNADB_SERVER_SECRET missing.
14+
Did you forget to install the fauna addon or forgot to run inside Netlify Dev?
15+
`;
16+
console.error(msg);
17+
return {
18+
statusCode: 500,
19+
body: JSON.stringify({ msg })
20+
};
21+
}
22+
const b64encodedSecret = Buffer.from(
23+
process.env.FAUNADB_SERVER_SECRET + ":" // weird but they
24+
).toString("base64");
25+
const headers = { Authorization: `Basic ${b64encodedSecret}` };
26+
27+
/** standard creation of apollo-server executable schema */
28+
const link = createHttpLink({
29+
uri: "https://graphql.faunadb.net/graphql", // modify as you see fit
30+
fetch,
31+
headers
32+
});
33+
introspectSchema(link).then(schema => {
34+
const executableSchema = makeRemoteExecutableSchema({
35+
schema,
36+
link
37+
});
38+
const server = new ApolloServer({
39+
schema: executableSchema
40+
});
41+
server.createHandler()(event, context, cb);
42+
});
43+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "fauna-graphql-dev",
3+
"version": "1.0.0",
4+
"description": "netlify functions:create - set up for fauna db + apollo graphql",
5+
"main": "fauna-graphql-dev.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"netlify",
11+
"serverless",
12+
"js",
13+
"apollo",
14+
"fauna"
15+
],
16+
"author": "Netlify",
17+
"license": "MIT",
18+
"dependencies": {
19+
"apollo-link-http": "^1.5.14",
20+
"apollo-link-context": "^1.0.17",
21+
"apollo-server-lambda": "^2.4.8",
22+
"graphql": "^14.1.1",
23+
"graphql-tools": "^4.0.4",
24+
"node-fetch": "^2.3.0"
25+
}
26+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type Todo {
2+
title: String!
3+
completed: Boolean!
4+
}
5+
type Query {
6+
allTodos: [Todo!]
7+
todosByCompletedFlag(completed: Boolean!): [Todo!]
8+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env node
2+
3+
/* sync GraphQL schema to your FaunaDB account - use with `netlify dev:exec <path-to-this-file>` */
4+
function createFaunaGraphQL() {
5+
if (!process.env.FAUNADB_SERVER_SECRET) {
6+
console.log("No FAUNADB_SERVER_SECRET in environment, skipping DB setup");
7+
}
8+
console.log("Upload GraphQL Schema!");
9+
10+
var request = require("request");
11+
const fs = require("fs");
12+
const path = require("path");
13+
var dataString = fs
14+
.readFileSync(path.join(__dirname, "schema.graphql"))
15+
.toString(); // name of your schema file
16+
17+
var options = {
18+
url: "https://graphql.faunadb.net/import",
19+
method: "POST",
20+
body: dataString,
21+
auth: {
22+
user: process.env.FAUNADB_SERVER_SECRET,
23+
pass: ""
24+
}
25+
};
26+
27+
request(options, callback);
28+
29+
function callback(error, response, body) {
30+
if (!error && response.statusCode == 200) {
31+
// // for debugging
32+
// console.log("body", body);
33+
} else {
34+
console.error("something wrong happened: ", { error, body });
35+
}
36+
}
37+
}
38+
39+
createFaunaGraphQL();

0 commit comments

Comments
 (0)