Skip to content

Instantly share code, notes, and snippets.

@darahayes
Created November 15, 2019 12:00
Show Gist options
  • Select an option

  • Save darahayes/7efb6c7c586af6e36f7f8a7aeea18f3c to your computer and use it in GitHub Desktop.

Select an option

Save darahayes/7efb6c7c586af6e36f7f8a7aeea18f3c to your computer and use it in GitHub Desktop.
Medium Blog Post on Keycloak Authentication and Authorization in GraphQL
const { ApolloServer, gql } = require('apollo-server-express')
const Keycloak = require('keycloak-connect')
const { KeycloakContext, KeycloakTypeDefs, KeycloakSchemaDirectives } = require('keycloak-connect-graphql')
const { typeDefs, resolvers } = require('./schema')
const app = express()
const keycloak = new Keycloak()
app.use(graphqlPath, keycloak.middleware())
const server = new ApolloServer({
typeDefs: [KeycloakTypeDefs, typeDefs], // 1. Add the Keycloak Type Defs
schemaDirectives: KeycloakSchemaDirectives, // 2. Add the KeycloakSchemaDirectives
resolvers,
context: ({ req }) => {
return {
kauth: new KeycloakContext({ req }) // 3. add the KeycloakContext to `kauth`
}
}
})
server.applyMiddleware({ app })
app.listen({ 4000 }, () =>
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment