forked from prisma/prisma1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostRandomDogImage.js
45 lines (37 loc) · 1011 Bytes
/
postRandomDogImage.js
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
44
45
import 'isomorphic-fetch'
import { fromEvent } from 'graphcool-lib'
const url = 'https://dog.ceo/api/breeds/image/random'
export default async event => {
const graphcool = fromEvent(event)
const api = graphcool.api('simple/v1')
const result = await fetch(url)
.then(response => response.json())
.then(responseData => {
const randomDogImageData = responseData.message
const postRandomDogImage = { url: randomDogImageData }
return { data: postRandomDogImage }
})
const mutation = `
mutation ($imageURL: String!, $authorId: ID!) {
createPost(
authorId: $authorId
imageURL: $imageURL
) {
id
}
}
`
const variables = {
imageURL: result.data.url,
authorId: event.data.authorId
}
try {
await api.request(mutation, variables)
} catch (error) {
console.log(`Mutation failed: ${JSON.stringify(error)}`)
return {
error: error.response.errors[0].functionError
}
}
return result
}