Skip to content

Commit

Permalink
feat(subscriptions): added delegateSubscription method to base bindin…
Browse files Browse the repository at this point in the history
…g class for static bindings (#10)
  • Loading branch information
kbrandwijk authored Jan 4, 2018
1 parent 346e249 commit 7bd5fcc
Showing 1 changed file with 47 additions and 20 deletions.
67 changes: 47 additions & 20 deletions src/Binding.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { $$asyncIterator } from 'iterall'
import { buildInfo } from './info'
import { GraphQLResolveInfo, graphql, GraphQLSchema } from 'graphql'
import { delegateToSchema } from 'graphql-tools'
import { makeProxy, makeSubscriptionProxy } from './proxy'
import {
QueryMap,
BindingOptions,
FragmentReplacements,
SubscriptionMap,
Operation,
} from './types'
import { QueryMap, BindingOptions, FragmentReplacements, SubscriptionMap, Operation } from './types'

export class Binding {
query: QueryMap
Expand All @@ -28,32 +23,27 @@ export class Binding {
schema: this.schema,
fragmentReplacements: this.fragmentReplacements,
operation: 'query',
before: this.before,
before: this.before
})
this.mutation = makeProxy<QueryMap>({
schema: this.schema,
fragmentReplacements: this.fragmentReplacements,
operation: 'mutation',
before: this.before,
before: this.before
})
this.subscription = makeSubscriptionProxy<SubscriptionMap>({
schema: this.schema,
fragmentReplacements: this.fragmentReplacements,
before: this.before,
before: this.before
})
}

async request<T = any>(
query: string,
variables?: { [key: string]: any },
): Promise<T> {
public async request<T = any>(query: string, variables?: { [key: string]: any }): Promise<T> {
this.before()
return graphql(this.schema, query, null, null, variables).then(
r => r.data![Object.keys(r.data!)[0]],
)
return graphql(this.schema, query, null, null, variables).then(r => r.data![Object.keys(r.data!)[0]])
}

async delegate(
public async delegate(
operation: Operation,
fieldName: string,
args: {
Expand All @@ -62,7 +52,7 @@ export class Binding {
context: {
[key: string]: any
},
info?: GraphQLResolveInfo | string,
info?: GraphQLResolveInfo | string
) {
this.before()

Expand All @@ -75,7 +65,44 @@ export class Binding {
fieldName,
args,
context,
info,
info
)
}

public async delegateSubscription(
fieldName: string,
args?: { [key: string]: any },
infoOrQuery?: GraphQLResolveInfo | string
): Promise<AsyncIterator<any>> {
this.before()

const info = buildInfo(fieldName, 'subscription', this.schema, infoOrQuery)

const iterator = await delegateToSchema(
this.schema,
this.fragmentReplacements,
'subscription',
fieldName,
args || {},
{},
info
)

return {
async next() {
const { value } = await iterator.next()
const data = { [info.fieldName]: value.data[fieldName] }
return { value: data, done: false }
},
return() {
return Promise.resolve({ value: undefined, done: true })
},
throw(error) {
return Promise.reject(error)
},
[$$asyncIterator]() {
return this
}
}
}
}

0 comments on commit 7bd5fcc

Please sign in to comment.