forked from prisma/prisma1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloggedInUser.js
51 lines (43 loc) · 1.26 KB
/
loggedInUser.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
46
47
48
49
50
51
const fromEvent = require('graphcool-lib').fromEvent
const userQuery = `
query UserQuery($userId: ID!) {
User(id: $userId){
id
password
}
}`
const getUser = (api, userId) => {
return api.request(userQuery, { userId })
.then(userQueryResult => {
return userQueryResult.User
})
.catch(error => {
// Log error, but don't expose to caller
console.log(`Error: ${JSON.stringify(error)}`)
return { error: `An unexpected error occured` }
})
}
module.exports = event => {
if (!event.context.auth || !event.context.auth.nodeId) {
console.log(`No auth context`)
return {data: {id: null}}
}
// Retrieve payload from event
const userId = event.context.auth.nodeId
console.log(`Node ID: ${userId}`)
// Create Graphcool API (based on https://github.com/graphcool/graphql-request)
const graphcool = fromEvent(event)
const api = graphcool.api('simple/v1')
return getUser(api, userId)
.then(emailUser => {
if (!emailUser) {
return { error: `No user with id: ${userId}` }
}
return { data: emailUser }
})
.catch(error => {
// Log error, but don't expose to caller
console.log(`Error: ${JSON.stringify(error)}`)
return { error: `An unexpected error occured` }
})
}