@@ -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+
113131function 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+
164197function renderUnionType ( type : GraphQLUnionType ) : string {
165198 return `${ renderDescription ( type . description ) } export type ${ type . name } = ${ type
166199 . getTypes ( )
0 commit comments