graphcool-templates
graphcool-templates copied to clipboard
Reject updating a node if a specific condition is true
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!"}
}
})
}
How is this different from a PQ?