Skip to content

Instantly share code, notes, and snippets.

import { KeycloakCrudService } from '@graphback/keycloak-authz'
class CustomCrudService extends KeycloakCrudService {
// custom implementation
}
const services = createKeycloakRuntimeContext({
models,
schema: model,
db,
import { ApolloServer } from 'apollo-server-express'
import { createKeycloakRuntimeContext } from '@graphback/keycloak-authz'
import { GraphbackRuntime } from 'graphback'
import { KnexDbDataProvider } from '@graphback/runtime-knex'
import { PubSub } from 'graphql-subscriptions'
import * as Knex from 'knex'
import { printSchema } from 'graphql'
// the application model
const model = `
const authConfig = {
Task: {
create: {},
read: {},
update: { roles: ["admin"] },
delete: { roles: ["admin"] }
},
Report: {
create: { roles: ["admin"] },
read: {},
input CreateNoteInput {
id: ID
content: String!
_version: Int
}
input CreateTaskInput {
id: ID
title: String!
description: String
import { ApolloOfflineClient } from 'offix-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { HttpLink } from "apollo-link-http";
const config = {
link: new HttpLink({ uri: 'http://example.com/graphql' })
cache: new InMemoryCache()
};
@darahayes
darahayes / keycloak-apollo-client-subscriptions-setup.js
Created November 15, 2019 12:11
From my post on Keycloak auth in GraphQL
import Keycloak from 'keycloak-js'
import { WebSocketLink } from 'apollo-link-ws'
var keycloak = Keycloak({
url: 'http://keycloak-server/auth',
realm: 'myrealm',
clientId: 'myapp'
})
@darahayes
darahayes / keycloak-connect-graphql-subscriptions-resolvers.js
Created November 15, 2019 12:10
From my medium post on Keycloak auth in GraphQL
const resolvers = {
Subscription: {
commentAdded: {
subscribe: () => pubsub.asyncIterator(COMMENT_ADDED)
},
messageAdded: {
subscribe: auth(() => pubsub.asyncIterator(COMMENT_ADDED))
},
alertAdded: {
subscribe: hasRole('admin')(() => pubsub.asyncIterator(ALERT_ADDED))
@darahayes
darahayes / keycloak-connect-graphql-subscriptions-setup.js
Created November 15, 2019 12:09
From my medium post on Keycloak auth in GraphQL
const keycloakSubscriptionHandler = new KeycloakSubscriptionHandler({ keycloak })
new SubscriptionServer({
execute,
subscribe,
schema: server.schema,
onConnect: async (connectionParams, websocket, connectionContext) => {
const token = await keycloakSubscriptionHandler.onSubscriptionConnect(connectionParams)
return {
kauth: new KeycloakSubscriptionContext(token)
}
@darahayes
darahayes / keycloak-connect-graphql-middlewares-example.js
Created November 15, 2019 12:04
From my medium post on Keycloak auth in GraphQL
const { auth, hasRole } = require('keycloak-connect-graphql')
const resolvers = {
Article: {
analytics: hasRole('editor')(articleAnalyticsResolver)
},
Query: {
listArticles: auth(listArticlesResolver)
},
mutation: {
@darahayes
darahayes / keycloak-connect-graphql-directives-example.js
Created November 15, 2019 12:02
From my medium post about Keycloak Auth in GraphQL
const typeDefs = gql`
type Article {
id: ID!
title: String!
content: String!
}
type Query {
listArticles: [Article]! @auth
}