Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 11 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const execa = require("execa");
module.exports = {
name: "fauna-graphql-dev",
description: "GraphQL function using Fauna DB [Dev Testing only]",
addons: [
{
addonName: "fauna",
addonDidInstall(fnPath) {
require("fs").chmodSync(fnPath + "/sync-schema.js", 0o777);
execa.sync(fnPath + "/sync-schema.js", undefined, {
env: process.env,
stdio: "inherit"
});
}
}
],
onComplete() {
console.log(`fauna-graphql function created from template!`);
}
};
43 changes: 43 additions & 0 deletions src/functions-templates/js/fauna-graphql-dev/fauna-graphql-dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const { ApolloServer, gql } = require("apollo-server-lambda");
const { createHttpLink } = require("apollo-link-http");
const fetch = require("node-fetch");
const {
introspectSchema,
makeRemoteExecutableSchema
} = require("graphql-tools");

exports.handler = function(event, context, cb) {
/** required for Fauna GraphQL auth */
if (!process.env.FAUNADB_SERVER_SECRET) {
const msg = `
FAUNADB_SERVER_SECRET missing.
Did you forget to install the fauna addon or forgot to run inside Netlify Dev?
`;
console.error(msg);
return {
statusCode: 500,
body: JSON.stringify({ msg })
};
}
const b64encodedSecret = Buffer.from(
process.env.FAUNADB_SERVER_SECRET + ":" // weird but they
).toString("base64");
const headers = { Authorization: `Basic ${b64encodedSecret}` };

/** standard creation of apollo-server executable schema */
const link = createHttpLink({
uri: "https://graphql.faunadb.net/graphql", // modify as you see fit
fetch,
headers
});
introspectSchema(link).then(schema => {
const executableSchema = makeRemoteExecutableSchema({
schema,
link
});
const server = new ApolloServer({
schema: executableSchema
});
server.createHandler()(event, context, cb);
});
};
26 changes: 26 additions & 0 deletions src/functions-templates/js/fauna-graphql-dev/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "fauna-graphql-dev",
"version": "1.0.0",
"description": "netlify functions:create - set up for fauna db + apollo graphql",
"main": "fauna-graphql-dev.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"netlify",
"serverless",
"js",
"apollo",
"fauna"
],
"author": "Netlify",
"license": "MIT",
"dependencies": {
"apollo-link-http": "^1.5.14",
"apollo-link-context": "^1.0.17",
"apollo-server-lambda": "^2.4.8",
"graphql": "^14.1.1",
"graphql-tools": "^4.0.4",
"node-fetch": "^2.3.0"
}
}
8 changes: 8 additions & 0 deletions src/functions-templates/js/fauna-graphql-dev/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type Todo {
title: String!
completed: Boolean!
}
type Query {
allTodos: [Todo!]
todosByCompletedFlag(completed: Boolean!): [Todo!]
}
39 changes: 39 additions & 0 deletions src/functions-templates/js/fauna-graphql-dev/sync-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env node

/* sync GraphQL schema to your FaunaDB account - use with `netlify dev:exec <path-to-this-file>` */
function createFaunaGraphQL() {
if (!process.env.FAUNADB_SERVER_SECRET) {
console.log("No FAUNADB_SERVER_SECRET in environment, skipping DB setup");
}
console.log("Upload GraphQL Schema!");

var request = require("request");
const fs = require("fs");
const path = require("path");
var dataString = fs
.readFileSync(path.join(__dirname, "schema.graphql"))
.toString(); // name of your schema file

var options = {
url: "https://graphql.faunadb.net/import",
method: "POST",
body: dataString,
auth: {
user: process.env.FAUNADB_SERVER_SECRET,
pass: ""
}
};

request(options, callback);

function callback(error, response, body) {
if (!error && response.statusCode == 200) {
// // for debugging
// console.log("body", body);
} else {
console.error("something wrong happened: ", { error, body });
}
}
}

createFaunaGraphQL();