Skip to content

Instantly share code, notes, and snippets.

@jthegedus
jthegedus / CF4F-express|functionsES6|index-02.js
Last active October 9, 2017 13:51
Cloud Functions for Firebase - Express example 2/3 - using middleware
const functions = require("firebase-functions")
const cors = require("cors")
const express = require("express")
/* Express with CORS */
const app2 = express()
app2.use(cors({ origin: true }))
app2.get("*", (request, response) => {
response.send("Hello from Express on Firebase with CORS!")
})
@jthegedus
jthegedus / CF4F-graphql|firebaseFunctions|package.json
Last active October 9, 2017 14:37
Cloud Functions for Firebase - GraphQL Server - /functionsES6/package.json
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"dependencies": {
"apollo-server-express": "^1.1.3",
"body-parser": "^1.18.2",
"express": "^4.16.1",
"firebase-admin": "^5.4.1",
"firebase-functions": "^0.7.0",
"graphql": "^0.11.7",
@jthegedus
jthegedus / CF4F-graphql|firebaseFunctions|index.js
Last active October 9, 2017 14:37
Cloud Functions for Firebase - GraphQL Server - /functionsES6/index.js
import { https } from "firebase-functions"
import setupGraphQLServer from "./graphql/server"
/* CF for Firebase with graphql-server-express */
const graphQLServer = setupGraphQLServer()
// https://us-central1-<project-name>.cloudfunctions.net/api
export const api = https.onRequest(graphQLServer)
@jthegedus
jthegedus / CF4F-graphql|firebaseFunctions|graphql|data|schema.js
Last active October 9, 2017 14:38
Cloud Functions for Firebase - GraphQL Server - /functionsES6/graphql/data/schema.js
import { makeExecutableSchema } from "graphql-tools"
import resolvers from "./resolvers"
const schema = `
type Author {
id: Int! # the ! means that every author object _must_ have an id
firstName: String
lastName: String
posts: [Post] # the list of Posts by this author
@jthegedus
jthegedus / CF4F-graphql|firebaseFunctions|graphql|data|resolver.js
Last active October 9, 2017 14:39
Cloud Functions for Firebase - GraphQL Server - /functionsES6/graphql/data/resolvers.js
const authors = [
{ id: 1, firstName: "Tom", lastName: "Coleman" },
{ id: 2, firstName: "Sashko", lastName: "Stubailo" }
]
const posts = [
{ id: 1, authorId: 1, title: "Introduction to GraphQL", votes: 2 },
{ id: 2, authorId: 2, title: "GraphQL Rocks", votes: 3 },
{ id: 3, authorId: 2, title: "Advanced GraphQL", votes: 1 }
]
@jthegedus
jthegedus / CF4F-ES6-2|function01.js
Last active November 29, 2017 11:46
ES6+ in Cloud Functions for Firebase - preset-2015
// input - using CommonJS module syntax
const functions = require('firebase-functions')
module.exports.helloWorld = functions.https.onRequest((req, res) => {
let world = `from Babelified Cloud Functions!`;
res.status(200).send(`Hello ${world}`);
})
// Babel output - with Babel REPL settings:
// * preset-es2015
@jthegedus
jthegedus / CF4F-ES6-2|function02.js
Last active November 29, 2017 11:46
ES6+ in Cloud Functions for Firebase - preset-2015 with ES module syntax
// input - using ES module syntax
import * as functions from 'firebase-functions';
export let helloWorld = functions.https.onRequest((req, res) => {
let world = `from Babelified Cloud Functions!`;
res.status(200).send(`Hello ${world}`);
})
// Babel output - with Babel REPL settings:
// * prettify
@jthegedus
jthegedus / CF4F-ES6-2|input.js
Created November 29, 2017 13:23
ES6+ in Cloud Functions for Firebase - input for the last 3 examples
// input - async/awaiy with CommonJS module syntax
const functions = require('firebase-functions');
// returns a string after 5 seconds
const message = () => {
return new Promise(resolve => {
setTimeout(() => {
resolve(`from Babelified Cloud Functions!`)
}, 5000)
})
@jthegedus
jthegedus / CF4F-ES6-2|function03.js
Last active November 29, 2017 13:24
ES6+ in Cloud Functions for Firebase - preset-2015 with async/await (not getting compiled)
// Babel output - with Babel REPL settings:
// * preset-es2015
"use strict";
var functions = require("firebase-functions");
var message = function message() {
return new Promise(function(resolve) {
setTimeout(function() {
resolve("from Babelified Cloud Functions!");
@jthegedus
jthegedus / CF4F-ES6-2|function04.js
Last active November 29, 2017 13:24
ES6+ in Cloud Functions for Firebase - preset-2015,2016,2017 to compile async/await
// Babel output - with Babel REPL settings:
// * prettify
// * preset-es2015 (es6->es5)
// * preset-es2016 (es7->es6)
// * preset-es2017 (es8->es7)
"use strict";
function _asyncToGenerator(fn) {
return function() {