graphcool-templates icon indicating copy to clipboard operation
graphcool-templates copied to clipboard

Reject updating a node if a specific condition is true

Open marktani opened this issue 8 years ago • 1 comments

require('isomorphic-fetch')

module.exports = function (event, cb) {
  var endpoint = 'https://api.graph.cool/simple/v1/__PROJECT_ID__'
  var token = 'Bearer __PAT__'

  var postId = event.data.id
  console.log(event.data)
  
  var query = `{
    Post(id: "${postId}") {
      isPublished
    }
  }`

  return fetch(endpoint, {
    method: 'post',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': token
    },
    body: JSON.stringify({ query })
  }).then((response) => {
    return response.json()
  }).then((data) => {
    console.log(data)
    if (data.data.Post.isPublished) {
      console.log('all good')
      return event 
    } else {
      console.log(`Unexpected update for Post ${postId}`)
      return {error: "Post not open for editing!"} 
    }
  })
}

marktani avatar May 24 '17 15:05 marktani

How is this different from a PQ?

kbrandwijk avatar Jul 24 '17 12:07 kbrandwijk