How to use graphcool-lib - 10 common examples

To help you get started, we’ve selected a few graphcool-lib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github prisma-archive / graphcool-templates / auth / google / src / googleAuthentication.ts View on Github external
export default async (event: FunctionEvent) => {
  console.log(event)

  try {
    const graphcool = fromEvent(event)
    const api = graphcool.api('simple/v1')

    const { googleToken } = event.data

    // call google API to obtain user data
    const googleUser = await getGoogleUser(googleToken)
    
    // get graphcool user by google id
    const user: User = await getGraphcoolUser(api, googleUser.sub)
      .then(r => r.User)

    // check if graphcool user exists, and create new one if not
    let userId: string | null = null

    if (!user) {
      userId = await createGraphcoolUser(api, googleUser.sub)
github prisma-archive / graphcool-templates / miscellaneous / tictactoe-gameserver / functions / create-gamestate-for-new-game.js View on Github external
module.exports = (event) => {
  event.context = { graphcool: { pat, projectId } }
  const api = fromEvent(event).api('simple/v1');

  // Flip a coin to see who gets to go first
  const turn = crypto.randomBytes(1)[0] > 127 ? "User" : "Computer"

  // Flip another coin to determine player symbol
  const playerSymbol = crypto.randomBytes(1)[0] > 127 ? "X" : "O"


  // Mutation to create GameState for the new Game
  const mutation = `mutation {
	createGameState(
		gameId: "${event.data.id}",
		turn: ${turn},
		playerSymbol: ${playerSymbol})
	{ id } }`
github prisma-archive / graphcool-templates / miscellaneous / tictactoe-gameserver / functions / computer-makes-move.js View on Github external
module.exports = (event) => {
  event.context = { graphcool: { pat, projectId } }
  const api = fromEvent(event).api('simple/v1');

  const nextMove = takeNoobMove(event.data.GameState.node.board)
  const mutation = `mutation {
	createMove(
		gameId: "${event.data.GameState.node.game.id}",
		position: ${nextMove+1},
		player: Computer)
	{ id } }`

  // Return a Promise to wait for the mutation to complete
  return new Promise((resolve,reject) => {
	api.request(mutation)
  	  .then(data => resolve({ data: event.data }))
      .catch(err => resolve({ error: err}))
  })
}
github prisma-archive / graphcool-templates / outdated / miscellaneous / tictactoe-gameserver / functions / update-gamestate-on-move.js View on Github external
module.exports = (event) => {
  // First, validate position
  if (Array.from(Array(9).keys()).indexOf(event.data.position - 1) == -1) {
    return { error: "Position needs to be 1-9" }
  }

  event.context = { graphcool: { pat, projectId } }
  const api = fromEvent(event).api('simple/v1');

  // Query to get GameState
  const gameStateQuery = `query {
	Game(id: "${event.data.gameId}") { gameState { id playerSymbol board turn } } }`

  // Return a Promise to wait for the mutation to complete
  return new Promise((resolve,reject) => {
	api.request(gameStateQuery)
  	  .then(data => {
      	const { id, playerSymbol, board } = data.Game.gameState

        // Determine which symbol to play with
        const symbol = (event.data.player != "Computer") ? playerSymbol : (playerSymbol == "X" ? "O" : "X")

        // Update board and turn
        board[event.data.position-1] = symbol
github CommerceQL / commerceql / oldsrc / cleanupCarts.ts View on Github external
export default async (event: FunctionEvent<{}>) => {
  console.log(event)

  try {
    const graphcool = fromEvent(event)
    const api = graphcool.api('simple/v1')

    const itemIds = event.data.Cart.node.items.map(i => i.id)
    const cartId = event.data.Cart.node.id

    const deleteItems = itemIds
      .map(
        id =>
          `${id}: deleteCartItem(id: "${id}") {
        id
      }`
      )
      .join('\n')

    const deleteCart = `${cartId}: deleteCart(id: "${cartId}") {
    id
github CommerceQL / commerceql / oldsrc / getBasketMeta.ts View on Github external
export default async (event: FunctionEvent) => {
  console.log(event)

  try {
    const graphcool = fromEvent(event)
    const api = graphcool.api('simple/v1')

    const { basketId } = event.data

    const { Basket } = await getBasket(api, basketId)

    if (!Basket) {
      return { error: `Invalid basketId ${basketId}` }
    }

    const subTotal = calculateSubTotal(Basket.items)
    const uniqueItems = Basket._itemsMeta.count
    const totalItems = Basket.items.reduce(
      (sum, item) => sum + item.quantity,
      0
    )
github CommerceQL / commerceql / oldsrc / addItemToBasket.ts View on Github external
export default async (event: FunctionEvent) => {
  console.log(event)

  try {
    const graphcool = fromEvent(event)
    const api = graphcool.api('simple/v1')

    const { basketId, productId, quantity = 1 } = event.data

    const allBasketItems: Basket = await checkBasketItemExists(
      api,
      basketId,
      productId
    ).then(data => data.allBasketItems)

    if (basketIsEmpty(allBasketItems)) {
      const basketItem: BasketItem = await createBasketItem(
        api,
        basketId,
        productId,
        quantity
github CommerceQL / commerceql / oldsrc / sendShippingEmail.ts View on Github external
export default async (event: FunctionEvent) => {
  if (!process.env['SENDGRID_API_KEY']) {
    console.log('Please provide a valid SENDGRID_API_KEY')
    return { error: 'CommerceQL is not configured to work with SendGrid.' }
  }

  const graphcool = fromEvent(event)
  const api = graphcool.api('simple/v1')

  sgMail.setApiKey(process.env['SENDGRID_API_KEY'])

  const { node } = event.data.Order

  if (node.fulfillmentStatus === 'FULFILLED') {
    sendEmail(node)
      .then(() => {
        return
      })
      .catch(err => ({ error: err.message }))
  }
}
github prisma-archive / graphcool-templates / community / auth / facebook-authentication / facebook-authentication.js View on Github external
module.exports = function(event) {
  const facebookToken = event.data.facebookToken
  const graphcool = fromEvent(event)
  const api = graphcool.api('simple/v1')

  function getFacebookAccountData(facebookToken) {
    return fetch(
      `https://graph.facebook.com/v2.9/me?fields=id%2Cemail&access_token=${facebookToken}`)
      .then(response => response.json())
      .then((parsedResponse) => {
      console.log(parsedResponse)
        if (parsedResponse.error) {
          return Promise.reject(parsedResponse.error.message)
        } else {
          return parsedResponse
        }
      })
  }
github prisma-archive / graphcool-templates / auth / email-password / src / authenticate.js View on Github external
module.exports = function(event) {
  if (!event.context.graphcool.pat) {
    console.log('Please provide a valid root token!')
    return { error: 'Email Authentication not configured correctly.'}
  }

  const email = event.data.email
  const password = event.data.password
  const graphcool = fromEvent(event)
  const api = graphcool.api('simple/v1')

  return getGraphcoolUser(api, email)
    .then((graphcoolUser) => {
      if (graphcoolUser === null) {
        return Promise.reject("Invalid Credentials") //returning same generic error so user can't find out what emails are registered.
      } else {
        return bcrypt.compare(password, graphcoolUser.password)
          .then(passwordCorrect => {
            if (passwordCorrect) {
              return graphcoolUser.id
            } else {
              return Promise.reject("Invalid Credentials")
            }
          })
      }

graphcool-lib

## Install

Unknown
Latest version published 6 years ago

Package Health Score

31 / 100
Full package analysis

Popular graphcool-lib functions

Similar packages