diff --git a/package-lock.json b/package-lock.json index e7ccaec..f310103 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2689,8 +2689,7 @@ "ansi-regex": { "version": "2.1.1", "resolved": false, - "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=", - "optional": true + "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=" }, "aproba": { "version": "1.2.0", @@ -2711,14 +2710,12 @@ "balanced-match": { "version": "1.0.0", "resolved": false, - "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", - "optional": true + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" }, "brace-expansion": { "version": "1.1.11", "resolved": false, "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2733,20 +2730,17 @@ "code-point-at": { "version": "1.1.0", "resolved": false, - "integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=", - "optional": true + "integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=" }, "concat-map": { "version": "0.0.1", "resolved": false, - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", - "optional": true + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" }, "console-control-strings": { "version": "1.1.0", "resolved": false, - "integrity": "sha1-PXz0Rk22RG6mRL9LOVB/mFEAjo4=", - "optional": true + "integrity": "sha1-PXz0Rk22RG6mRL9LOVB/mFEAjo4=" }, "core-util-is": { "version": "1.0.2", @@ -2863,8 +2857,7 @@ "inherits": { "version": "2.0.3", "resolved": false, - "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", - "optional": true + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" }, "ini": { "version": "1.3.5", @@ -2876,7 +2869,6 @@ "version": "1.0.0", "resolved": false, "integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=", - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2891,7 +2883,6 @@ "version": "3.0.4", "resolved": false, "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2899,14 +2890,12 @@ "minimist": { "version": "0.0.8", "resolved": false, - "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", - "optional": true + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=" }, "minipass": { "version": "2.3.5", "resolved": false, "integrity": "sha512-Gi1W4k059gyRbyVUZQ4mEqLm0YIUiGYfvxhF6SIlk3ui1WVxMTGfGdQ2SInh3PDrRTVvPKgULkpJtT4RH10+VA==", - "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -2925,7 +2914,6 @@ "version": "0.5.1", "resolved": false, "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", - "optional": true, "requires": { "minimist": "0.0.8" } @@ -3006,8 +2994,7 @@ "number-is-nan": { "version": "1.0.1", "resolved": false, - "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=", - "optional": true + "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=" }, "object-assign": { "version": "4.1.1", @@ -3019,7 +3006,6 @@ "version": "1.4.0", "resolved": false, "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", - "optional": true, "requires": { "wrappy": "1" } @@ -3105,8 +3091,7 @@ "safe-buffer": { "version": "5.1.2", "resolved": false, - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", - "optional": true + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" }, "safer-buffer": { "version": "2.1.2", @@ -3142,7 +3127,6 @@ "version": "1.0.2", "resolved": false, "integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=", - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -3162,7 +3146,6 @@ "version": "3.0.1", "resolved": false, "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -3206,14 +3189,12 @@ "wrappy": { "version": "1.0.2", "resolved": false, - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", - "optional": true + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" }, "yallist": { "version": "3.0.3", "resolved": false, - "integrity": "sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==", - "optional": true + "integrity": "sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==" } } }, diff --git a/src/functions-templates/js/fauna-graphql-dev/.netlify-function-template.js b/src/functions-templates/js/fauna-graphql-dev/.netlify-function-template.js new file mode 100644 index 0000000..bae3637 --- /dev/null +++ b/src/functions-templates/js/fauna-graphql-dev/.netlify-function-template.js @@ -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!`); + } +}; diff --git a/src/functions-templates/js/fauna-graphql-dev/fauna-graphql-dev.js b/src/functions-templates/js/fauna-graphql-dev/fauna-graphql-dev.js new file mode 100644 index 0000000..5a69c29 --- /dev/null +++ b/src/functions-templates/js/fauna-graphql-dev/fauna-graphql-dev.js @@ -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); + }); +}; diff --git a/src/functions-templates/js/fauna-graphql-dev/package.json b/src/functions-templates/js/fauna-graphql-dev/package.json new file mode 100644 index 0000000..29911c0 --- /dev/null +++ b/src/functions-templates/js/fauna-graphql-dev/package.json @@ -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" + } +} diff --git a/src/functions-templates/js/fauna-graphql-dev/schema.graphql b/src/functions-templates/js/fauna-graphql-dev/schema.graphql new file mode 100644 index 0000000..c90dad5 --- /dev/null +++ b/src/functions-templates/js/fauna-graphql-dev/schema.graphql @@ -0,0 +1,8 @@ +type Todo { + title: String! + completed: Boolean! +} +type Query { + allTodos: [Todo!] + todosByCompletedFlag(completed: Boolean!): [Todo!] +} diff --git a/src/functions-templates/js/fauna-graphql-dev/sync-schema.js b/src/functions-templates/js/fauna-graphql-dev/sync-schema.js new file mode 100644 index 0000000..2a92055 --- /dev/null +++ b/src/functions-templates/js/fauna-graphql-dev/sync-schema.js @@ -0,0 +1,39 @@ +#!/usr/bin/env node + +/* sync GraphQL schema to your FaunaDB account - use with `netlify dev:exec ` */ +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();