Skip to content
This repository was archived by the owner on May 10, 2018. It is now read-only.

Commit e78a198

Browse files
committed
feat: added static bindings for subscriptions
Closes #25
1 parent cd4faed commit e78a198

File tree

8 files changed

+6782
-71
lines changed

8 files changed

+6782
-71
lines changed

src/generators/binding-js.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from 'graphql'
2020

2121
import { Generator } from '../types'
22+
import { renderMainMethodFields, renderMainSubscriptionMethodFields } from './graphcool-js'
2223

2324
export const generator: Generator = {
2425
Main: renderMainMethod,
@@ -44,6 +45,10 @@ ${renderMainMethodFields('query', queryType.getFields())}
4445
4546
this.mutation = {
4647
${renderMainMethodFields('mutation', mutationType.getFields())}
48+
}`: ''}${subscriptionType ? `
49+
50+
this.subscription = {
51+
${renderMainSubscriptionMethodFields('mutation', subscriptionType.getFields())}
4752
}`: ''}
4853
}
4954
@@ -53,11 +58,3 @@ ${renderMainMethodFields('mutation', mutationType.getFields())}
5358
}`
5459
}
5560

56-
function renderMainMethodFields(operation: string, fields: GraphQLFieldMap<any, any>): string {
57-
return Object.keys(fields).map(f => {
58-
const field = fields[f]
59-
return ` ${field.name}(args, info) {
60-
return self.delegate('${operation}', '${field.name}', args, {}, info)
61-
}`
62-
}).join(',\n')
63-
}

src/generators/binding-ts.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
} from 'graphql'
2020

2121
import { Generator } from '../types'
22-
import { generator as gcgenerator, renderMainMethodFields } from './graphcool-ts'
22+
import { generator as gcgenerator, renderMainMethodFields, renderMainSubscriptionMethodFields } from './graphcool-ts'
2323

2424
export const generator: Generator = {
2525
...gcgenerator,
@@ -45,6 +45,10 @@ ${renderMainMethodFields('query', queryType.getFields())}
4545
4646
mutation: Mutation = {
4747
${renderMainMethodFields('mutation', mutationType.getFields())}
48+
}`: ''}${subscriptionType ? `
49+
50+
subscription: Subscription = {
51+
${renderMainSubscriptionMethodFields(subscriptionType.getFields())}
4852
}`: ''}
4953
}`
5054
}

src/generators/graphcool-js.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ ${renderMainMethodFields('query', queryType.getFields())}
5151
5252
this.mutation = {
5353
${renderMainMethodFields('mutation', mutationType.getFields())}
54+
}`: ''}${subscriptionType ? `
55+
56+
this.subscription = {
57+
${renderMainSubscriptionMethodFields('mutation', subscriptionType.getFields())}
5458
}`: ''}
5559
}
5660
@@ -83,11 +87,20 @@ export function renderExistsFields(fields: GraphQLFieldMap<any, any>) : string {
8387
.join(',\n')
8488
}
8589

86-
function renderMainMethodFields(operation: string, fields: GraphQLFieldMap<any, any>): string {
90+
export function renderMainMethodFields(operation: string, fields: GraphQLFieldMap<any, any>): string {
8791
return Object.keys(fields).map(f => {
8892
const field = fields[f]
8993
return ` ${field.name}(args, info) {
9094
return self.delegate('${operation}', '${field.name}', args, {}, info)
9195
}`
9296
}).join(',\n')
9397
}
98+
99+
export function renderMainSubscriptionMethodFields(operation: string, fields: GraphQLFieldMap<any, any>): string {
100+
return Object.keys(fields).map(f => {
101+
const field = fields[f]
102+
return ` ${field.name}(args, infoOrQuery) {
103+
return self.delegateSubscription('${field.name}', args, infoOrQuery)
104+
}`
105+
}).join(',\n')
106+
}

src/generators/graphcool-ts.ts

+34-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const generator: Generator = {
2828
GraphQLEnumType: renderEnumType,
2929
GraphQLInterfaceType: renderObjectType,
3030
RootType: renderRootType,
31+
SubscriptionType: renderSubscriptionType,
3132
SchemaType: renderSchemaInterface,
3233
Main: renderMainMethod,
3334
Header: renderHeader
@@ -63,7 +64,7 @@ function renderMainMethod(
6364
exists = {
6465
${renderExistsFields(queryType.getFields())}
6566
}
66-
67+
6768
query: Query = {
6869
${renderMainMethodFields('query', queryType.getFields())}
6970
}${
@@ -72,6 +73,14 @@ ${renderMainMethodFields('query', queryType.getFields())}
7273
7374
mutation: Mutation = {
7475
${renderMainMethodFields('mutation', mutationType.getFields())}
76+
}`
77+
: ''
78+
}${
79+
subscriptionType
80+
? `
81+
82+
subscription: Subscription = {
83+
${renderMainSubscriptionMethodFields(subscriptionType.getFields())}
7584
}`
7685
: ''
7786
}
@@ -110,6 +119,15 @@ export function renderMainMethodFields(operation: string, fields: GraphQLFieldMa
110119
.join(',\n')
111120
}
112121

122+
export function renderMainSubscriptionMethodFields(fields: GraphQLFieldMap<any, any>): string {
123+
return Object.keys(fields)
124+
.map(f => {
125+
const field = fields[f]
126+
return ` ${field.name}: (args, infoOrQuery): Promise<AsyncIterator<${renderFieldType(field.type)}>> => super.delegateSubscription('${field.name}', args, infoOrQuery)`
127+
})
128+
.join(',\n')
129+
}
130+
113131
function renderScalarType(type: GraphQLScalarType): string {
114132
if (type.name === 'ID') {
115133
return renderIDType(type)
@@ -161,6 +179,21 @@ function renderRootType(type: GraphQLObjectType): string {
161179
return renderTypeWrapper(type.name, type.description, fieldDefinition)
162180
}
163181

182+
function renderSubscriptionType(type: GraphQLObjectType): string {
183+
const fieldDefinition = Object.keys(type.getFields())
184+
.map(f => {
185+
const field = type.getFields()[f]
186+
return ` ${field.name}: (args: {${field.args.length > 0 ? ' ' : ''}${field.args
187+
.map(f => `${renderFieldName(f)}: ${renderFieldType(f.type)}`)
188+
.join(', ')}${
189+
field.args.length > 0 ? ' ' : ''
190+
}}, infoOrQuery?: GraphQLResolveInfo | string) => Promise<AsyncIterator<${renderFieldType(field.type)}>>`
191+
})
192+
.join('\n')
193+
194+
return renderTypeWrapper(type.name, type.description, fieldDefinition)
195+
}
196+
164197
function renderUnionType(type: GraphQLUnionType): string {
165198
return `${renderDescription(type.description)}export type ${type.name} = ${type
166199
.getTypes()

src/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ ${Object.keys(generators).map(k => `'${k}`).join(', ')}`)
4545
if (generator.RootType) {
4646
generatedClass.push(generator.RootType(ast.getQueryType()))
4747
if (ast.getMutationType()) { generatedClass.push(generator.RootType(ast.getMutationType()!)) }
48-
if (ast.getSubscriptionType()) { generatedClass.push(generator.RootType(ast.getSubscriptionType()!)) }
48+
}
49+
50+
// Special case 5: subscription type
51+
if (generator.SubscriptionType) {
52+
if (ast.getSubscriptionType()) { generatedClass.push(generator.SubscriptionType(ast.getSubscriptionType()!)) }
4953
}
5054

5155
// Special case 3: the main method

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface Generator {
55
Header: (schema: string) => string,
66
SchemaType?: (queryType: GraphQLObjectType, mutationType?: GraphQLObjectType | null, subscriptionType?: GraphQLObjectType | null) => string,
77
RootType?: (type: GraphQLObjectType) => string,
8+
SubscriptionType?: (type: GraphQLObjectType) => string,
89
GraphQLUnionType?: (type: GraphQLUnionType) => string,
910
GraphQLInputObjectType?: (type: GraphQLInputObjectType) => string,
1011
GraphQLObjectType?: (type: GraphQLObjectType) => string,

0 commit comments

Comments
 (0)