Skip to content

Instantly share code, notes, and snippets.

View graphback-keycloak-auth-extend.ts
import { KeycloakCrudService } from '@graphback/keycloak-authz'
class CustomCrudService extends KeycloakCrudService {
// custom implementation
}
const services = createKeycloakRuntimeContext({
models,
schema: model,
db,
View graphback-keycloak-auth-runtime.ts
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 = `
View graphback-keycloak-authz-config.js
const authConfig = {
Task: {
create: {},
read: {},
update: { roles: ["admin"] },
delete: { roles: ["admin"] }
},
Report: {
create: { roles: ["admin"] },
read: {},
View aws_schema.graphql
input CreateNoteInput {
id: ID
content: String!
_version: Int
}
input CreateTaskInput {
id: ID
title: String!
description: String
View client.js
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 Nov 15, 2019
From my post on Keycloak auth in GraphQL
View keycloak-apollo-client-subscriptions-setup.js
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 Nov 15, 2019
From my medium post on Keycloak auth in GraphQL
View keycloak-connect-graphql-subscriptions-resolvers.js
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 Nov 15, 2019
From my medium post on Keycloak auth in GraphQL
View keycloak-connect-graphql-subscriptions-setup.js
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 Nov 15, 2019
From my medium post on Keycloak auth in GraphQL
View keycloak-connect-graphql-middlewares-example.js
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 Nov 15, 2019
From my medium post about Keycloak Auth in GraphQL
View keycloak-connect-graphql-directives-example.js
const typeDefs = gql`
type Article {
id: ID!
title: String!
content: String!
}
type Query {
listArticles: [Article]! @auth
}