Vinnie
11/09/2017, 12:42 PMNo field with id ‘cj9r95arr00220149vgvgme4d’
agartha
11/09/2017, 12:49 PMVinnie
11/09/2017, 12:49 PMGlobal
✖ Could not create serverless function for 'loggedInUser'. Ensure that the code is valid
But that function is standard from template. Any idea of what I might’ve done wrong? 🙂agartha
11/09/2017, 1:05 PMVinnie
11/09/2017, 1:05 PMtype LoggedInUserPayload {
id: ID!
}
extend type Query {
# return user data if request contains valid authentication token
loggedInUser: LoggedInUserPayload
}
import { fromEvent, FunctionEvent } from ‘graphcool-lib’
import { GraphQLClient } from ‘graphql-request’
interface User {
id: string
}
export default async (event: FunctionEvent<{}>) => {
console.log(event)
try {
// no logged in user
if (!event.context.auth || !event.context.auth.nodeId) {
return { data: null }
}
const userId = event.context.auth.nodeId
const graphcool = fromEvent(event)
const api = graphcool.api(‘simple/v1’)
// get user by id
const user: User = await getUser(api, userId)
.then(r => r.User)
// no logged in user
if (!user || !user.id) {
return { data: null }
}
return { data: { id: user.id } }
} catch (e) {
console.log(e)
return { error: ‘An unexpected error occured during authentication.’ }
}
}
async function getUser(api: GraphQLClient, id: string): Promise<{ User }> {
const query = `
query getUser($id: ID!) {
User(id: $id) {
id
}
}
`
const variables = {
id,
}
return api.request<{ User }>(query, variables)
}
agartha
11/09/2017, 1:07 PMVinnie
11/09/2017, 1:09 PM--dry-run
is used…agartha
11/09/2017, 1:18 PMVinnie
11/09/2017, 1:18 PM