aerogear / keycloak-connect-graphql

Add Keyloak Authentication and Authorization to your GraphQL server.
Apache License 2.0
157 stars 23 forks source link

feat: subscriptions refinements #17

Closed darahayes closed 5 years ago

darahayes commented 5 years ago

This PR adds some really nice subscriptions refinements.

Take a look at the two examples provided.

In both scenarios, context.kauth in the subscription resolvers will work the exact same as in regular resolvers thanks to the KeycloakSubscriptionContext class. (KeycloakSubscriptionContext and KeycloakContext are now extensions of the KeycloakContextBase which provides the common functionality. This could also be used in future if we needed to support context from something that isn't express).

These improvements in how the context is built means that it is now possible to reuse the existing hasRole and auth resolver middlewares on subscription resolvers. Here's an example:

const { auth } = require('keycloak-connect-graphql')

const resolvers = {
  Subscription: {
    taskAdded: {
      subscribe: auth(() => pubsub.asyncIterator(TOPIC))
    },
    taskDeleted: {
      subscribe: hasRole('admin')(() => pubsub.asyncIterator(TOPIC))
    }
  }
}