Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'graphcool-lib' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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)
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 } }`
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}))
})
}
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
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
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
)
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
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 }))
}
}
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
}
})
}
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")
}
})
}