Created
July 27, 2019 03:55
-
-
Save aolufisayo/f14923f06b13cc6574ec37ee11abcf9f to your computer and use it in GitHub Desktop.
a sample custom resolver for hasura
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ApolloServer } = require('apollo-server'); | |
const gql = require('graphql-tag'); | |
const jwt = require('jsonwebtoken'); | |
const fetch = require('node-fetch'); | |
const typeDefs = gql` | |
type auth0_profile { | |
email: String | |
picture: String | |
} | |
type Query { | |
auth0: auth0_profile | |
} | |
`; | |
function getProfileInfo(user_id){ | |
const headers = {'Authorization': 'Bearer '+process.env.AUTH0_MANAGEMENT_API_TOKEN}; | |
console.log(headers); | |
return fetch('https://' + process.env.AUTH0_DOMAIN + '/api/v2/users/'+user_id,{ headers: headers}) | |
.then(response => response.json()) | |
} | |
const resolvers = { | |
Query: { | |
auth0: (parent, args, context) => { | |
// read the authorization header sent from the client | |
const authHeaders = context.headers.authorization; | |
const token = authHeaders.replace('Bearer ', ''); | |
// decode the token to find the user_id | |
try { | |
const decoded = jwt.decode(token); | |
const user_id = decoded.sub; | |
// make a rest api call to auth0 | |
return getProfileInfo(user_id).then( function(resp) { | |
console.log(resp); | |
if (!resp) { | |
return null; | |
} | |
return {email: resp.email, picture: resp.picture}; | |
}); | |
} catch(e) { | |
console.log(e); | |
return null; | |
} | |
} | |
}, | |
}; | |
const context = ({req}) => { | |
return {headers: req.headers}; | |
}; | |
const schema = new ApolloServer({ typeDefs, resolvers, context}); | |
schema.listen({ port: process.env.PORT}).then(({ url }) => { | |
console.log(`schema ready at ${url}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment