Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(postgraphql) Add EventListener object to Context object. (#501) #651

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/postgraphql/http/createPostGraphQLHttpRequestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const origGraphiqlHtml = new Promise((resolve, reject) => {
* @param {GraphQLSchema} graphqlSchema
*/
export default function createPostGraphQLHttpRequestHandler (options) {
const { getGqlSchema, pgPool, pgSettings, pgDefaultRole, jwtSecret, jwtAudiences, jwtRole, additionalGraphQLContextFromRequest } = options
const { getGqlSchema, pgPool, pgSettings, pgDefaultRole, jwtSecret, jwtAudiences, jwtRole, additionalGraphQLContextFromRequest, eventListenerFactory } = options

if (pgDefaultRole && typeof pgSettings === 'function') {
throw new Error('pgDefaultRole cannot be combined with pgSettings(req) - please remove pgDefaultRole and instead always return a `role` key from pgSettings(req).')
Expand Down Expand Up @@ -410,6 +410,8 @@ export default function createPostGraphQLHttpRequestHandler (options) {
jwtAudiences,
jwtRole,
pgDefaultRole,
eventListener:
typeof eventListenerFactory === 'function' ? await eventListenerFactory(req, res) : null,
pgSettings:
typeof pgSettings === 'function' ? await pgSettings(req) : pgSettings,
}, context => {
Expand Down
4 changes: 4 additions & 0 deletions src/postgraphql/withPostGraphQLContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import jwt = require('jsonwebtoken')
import { Pool, Client } from 'pg'
import { ExecutionResult } from 'graphql'
import * as sql from 'pg-sql2'
import EventEmitter = require('events')
import { $$pgClient } from '../postgres/inventory/pgClientFromContext'

/**
Expand Down Expand Up @@ -39,6 +40,7 @@ export default async function withPostGraphQLContext(
jwtRole = ['role'],
pgDefaultRole,
pgSettings,
eventListener,
}: {
pgPool: Pool,
jwtToken?: string,
Expand All @@ -47,6 +49,7 @@ export default async function withPostGraphQLContext(
jwtRole: Array<string>,
pgDefaultRole?: string,
pgSettings?: { [key: string]: mixed },
eventListener: (event: string) => Promise<EventEmitter>,
},
callback: (context: mixed) => Promise<ExecutionResult>,
): Promise<ExecutionResult> {
Expand All @@ -73,6 +76,7 @@ export default async function withPostGraphQLContext(

return await callback({
[$$pgClient]: pgClient,
eventListener,
pgRole,
})
}
Expand Down