forked from Jobcool/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateFirstArticle.js
More file actions
43 lines (36 loc) · 1.08 KB
/
createFirstArticle.js
File metadata and controls
43 lines (36 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const { fromEvent } = require('graphcool-lib')
// The input type for the function is determined by the subscription query
// defined in `createFirstArticle.graphql`.
// A sample payload could look like this:
//
// event = {
// "data": {
// "User": {
// "node": {
// "id": "cj8wscby6nl7u0133zu7c8a62"
// "name": "Sarah"
// }
// }
// },
// // more event data ...
// }
module.exports = event => {
// Retrieve payload from event
const { id, name } = event.data.User.node
// Create Graphcool API (based on https://github.com/graphcool/graphql-request)
const graphcool = fromEvent(event)
const api = graphcool.api('simple/v1')
// Create variables for mutation
const title = `My name is ${name}, and this is my first article!`
const variables = { authorId: id, title }
// Create mutation
const createArticleMutation = `
mutation ($title: String!, $authorId: ID!) {
createArticle(title: $title, authorId: $authorId) {
id
}
}
`
// Send mutation with variables
return api.request(createArticleMutation, variables)
}