Skip to content

Commit

Permalink
feat: add context to default handler and allow custom handler
Browse files Browse the repository at this point in the history
BREAKING CHANGES: default handlers now have a context argument
  • Loading branch information
kbrandwijk committed Jan 12, 2018
1 parent 264fb44 commit e8a4d59
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
22 changes: 12 additions & 10 deletions src/Binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,35 @@ import { delegateToSchema } from 'graphql-tools'
import { makeProxy, makeSubscriptionProxy } from './proxy'
import { QueryMap, BindingOptions, FragmentReplacements, SubscriptionMap, Operation } from './types'

export class Binding {
query: QueryMap
mutation: QueryMap
subscription: SubscriptionMap
export class Binding<TQueryMap extends object = QueryMap, TSubscriptionMap extends object = SubscriptionMap> {
query: TQueryMap
mutation: TQueryMap
subscription: TSubscriptionMap
schema: GraphQLSchema
before: () => void

private fragmentReplacements: FragmentReplacements

constructor({ schema, fragmentReplacements, before }: BindingOptions) {
constructor({ schema, fragmentReplacements, before, handler }: BindingOptions) {
this.fragmentReplacements = fragmentReplacements || {}
this.schema = schema
this.before = before || (() => undefined)

this.query = makeProxy<QueryMap>({
this.query = makeProxy<TQueryMap>({
schema: this.schema,
fragmentReplacements: this.fragmentReplacements,
operation: 'query',
before: this.before
before: this.before,
handler
})
this.mutation = makeProxy<QueryMap>({
this.mutation = makeProxy<TQueryMap>({
schema: this.schema,
fragmentReplacements: this.fragmentReplacements,
operation: 'mutation',
before: this.before
before: this.before,
handler
})
this.subscription = makeSubscriptionProxy<SubscriptionMap>({
this.subscription = makeSubscriptionProxy<TSubscriptionMap>({
schema: this.schema,
fragmentReplacements: this.fragmentReplacements,
before: this.before
Expand Down
6 changes: 4 additions & 2 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class Handler<T extends object> implements ProxyHandler<T> {
get(target: T, rootFieldName: string) {
return (
args?: { [key: string]: any },
context?: { [key: string]: any },
info?: GraphQLResolveInfo | string,
): Promise<ExecutionResult> => {
this.before()
Expand All @@ -28,7 +29,7 @@ export class Handler<T extends object> implements ProxyHandler<T> {
operation,
rootFieldName,
args || {},
{},
context || {},
info,
)
}
Expand All @@ -45,6 +46,7 @@ export class SubscriptionHandler<T extends object> implements ProxyHandler<T> {
get(target: T, rootFieldName: string) {
return async (
args?: { [key: string]: any },
context?: { [key: string]: any },
infoOrQuery?: GraphQLResolveInfo | string,
): Promise<AsyncIterator<any>> => {
this.before()
Expand All @@ -62,7 +64,7 @@ export class SubscriptionHandler<T extends object> implements ProxyHandler<T> {
'subscription',
rootFieldName,
args || {},
{},
context || {},
info,
)

Expand Down
8 changes: 6 additions & 2 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ export function makeProxy<T extends object = any>({
fragmentReplacements,
operation,
before,
handler
}: {
schema: GraphQLSchema
fragmentReplacements: FragmentReplacements
operation: 'query' | 'mutation'
before: () => void
before: () => void,
handler?: { new<T extends object>(schema, fragmentReplacements, operation, before): ProxyHandler<T> }
}): T {
return new Proxy(
{} as T,
new Handler<T>(schema, fragmentReplacements, operation, before),
handler
? new handler<T>(schema, fragmentReplacements, operation, before)
: new Handler<T>(schema, fragmentReplacements, operation, before),
)
}

Expand Down
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ export interface FragmentReplacements {

export interface QueryMap {
[rootField: string]: (
args?: any,
args?: { [key: string]: any },
context?: { [key: string]: any },
info?: GraphQLResolveInfo | string,
) => Promise<any>
}

export interface SubscriptionMap {
[rootField: string]: (
args?: any,
context?: { [key: string]: any },
info?: GraphQLResolveInfo | string,
) => AsyncIterator<any> | Promise<AsyncIterator<any>>
}
Expand All @@ -26,4 +28,5 @@ export interface BindingOptions {
fragmentReplacements?: FragmentReplacements
schema: GraphQLSchema
before?: () => void
handler?: any
}

0 comments on commit e8a4d59

Please sign in to comment.