Last active
May 22, 2020 13:16
-
-
Save darahayes/f4b70718a39f4eec6292032b3c0e01fe to your computer and use it in GitHub Desktop.
This file contains 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
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 = ` | |
""" | |
@model | |
""" | |
type Task { | |
id: ID! | |
title: String! | |
description: String! | |
}` | |
// the auth rules for the application | |
const authConfig = { | |
Task: { | |
create: {}, | |
read: {}, | |
update: { roles: ["admin"] }, | |
delete: { roles: ["admin"] } | |
} | |
} | |
// set up the Knex database client | |
const db = Knex({...}) | |
// standard Graphback runtime setup | |
const pubSub = new PubSub() | |
const runtimeEngine = new GraphbackRuntime(model, graphbackConfig) | |
const models = runtimeEngine.getDataSourceModels() | |
// create runtime services integrated with Keycloak | |
// Note that the knex database client and the KnexDBDataProvider class are passed | |
// Other data providers can be passed too | |
// Under the hood this creates KeycloakCrudService instances | |
const services = createKeycloakRuntimeContext({ | |
models, | |
schema: model, | |
db, | |
pubSub, | |
authConfig, | |
dataProvider: KnexDBDataProvider | |
}) | |
const { schema, resolvers } = runtimeEngine.buildRuntime(services); | |
const server = new ApolloServer({ | |
schema, | |
resolvers, | |
context: (req) => { | |
return { | |
kauth: new KeycloakContext({ req }) | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment