Created
November 15, 2019 12:00
-
-
Save darahayes/7efb6c7c586af6e36f7f8a7aeea18f3c to your computer and use it in GitHub Desktop.
Medium Blog Post on Keycloak Authentication and Authorization in GraphQL
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, 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