@@ -14,6 +14,7 @@ import {
14
14
import { typeNameFromGraphQLType } from '../typescript/types' ;
15
15
import { Property , interfaceDeclaration } from '../typescript/language' ;
16
16
import { isList } from '../utilities/graphql' ;
17
+ import { propertyDeclarations } from '../flow/codeGeneration' ;
17
18
18
19
export function generateSource ( context : LegacyCompilerContext ) {
19
20
const generator = new CodeGenerator < LegacyCompilerContext > ( context ) ;
@@ -39,11 +40,8 @@ function generateTypes(generator: CodeGenerator, context: LegacyCompilerContext)
39
40
// if subscription operations exist create subscriptionResponse interface
40
41
// https://github.com/aws-amplify/amplify-cli/issues/5284
41
42
if ( context . schema . getSubscriptionType ( ) ) {
42
- generator . printOnNewline ( `
43
- export interface SubscriptionResponse<T> {
44
- value: GraphQLResult<T>;
45
- }` ) ;
46
- generator . printNewline ( ) ;
43
+ generateSubscriptionResponseWrapper ( generator ) ;
44
+ generateSubscriptionOperationTypes ( generator , context ) ;
47
45
}
48
46
context . typesUsed . forEach ( type => typeDeclarationForGraphQLType ( generator , type ) ) ;
49
47
@@ -54,6 +52,38 @@ function generateTypes(generator: CodeGenerator, context: LegacyCompilerContext)
54
52
Object . values ( context . fragments ) . forEach ( operation => interfaceDeclarationForFragment ( generator , operation ) ) ;
55
53
}
56
54
55
+ function generateSubscriptionResponseWrapper ( generator : CodeGenerator ) {
56
+ generator . printOnNewline ( `
57
+ export interface SubscriptionResponse<T> {
58
+ value: GraphQLResult<T>;
59
+ }` ) ;
60
+ generator . printNewline ( ) ;
61
+ }
62
+ function generateSubscriptionOperationTypes ( generator : CodeGenerator , context : LegacyCompilerContext ) {
63
+ const typeName = '__SubscriptionContainer' ;
64
+ const properties : Property [ ] = [ ] ;
65
+ Object . values ( context . operations )
66
+ . filter ( operation => operation . operationType === 'subscription' )
67
+ . forEach ( operation => {
68
+ const { operationName, operationType } = operation ;
69
+ const typeName = interfaceNameFromOperation ( { operationName, operationType } ) ;
70
+ if ( operation . fields . length ) {
71
+ properties . push ( {
72
+ fieldName : operation . fields [ 0 ] . responseName ,
73
+ typeName,
74
+ } ) ;
75
+ }
76
+ } ) ;
77
+ if ( properties . length ) {
78
+ generator . printOnNewline ( `export type ${ typeName } = ` ) ;
79
+ generator . pushScope ( { typeName } ) ;
80
+ generator . withinBlock ( ( ) => propertyDeclarations ( generator , properties ) , '{' , '}' ) ;
81
+ generator . popScope ( ) ;
82
+ generator . print ( ';' ) ;
83
+ generator . printNewline ( ) ;
84
+ }
85
+ }
86
+
57
87
function interfaceDeclarationForOperation ( generator : CodeGenerator , { operationName, operationType, fields } : LegacyOperation ) {
58
88
const interfaceName = interfaceNameFromOperation ( { operationName, operationType } ) ;
59
89
fields = fields . map ( field => updateTypeNameField ( field ) ) ;
@@ -102,6 +132,9 @@ function getReturnTypeName(generator: CodeGenerator, op: LegacyOperation): Strin
102
132
return typeNameFromGraphQLType ( generator . context , type ) ;
103
133
} else {
104
134
let returnType = interfaceNameFromOperation ( { operationName, operationType } ) ;
135
+ if ( op . operationType === 'subscription' && op . fields . length ) {
136
+ returnType = `Pick<__SubscriptionContainer, "${ op . fields [ 0 ] . responseName } ">` ;
137
+ }
105
138
if ( isList ( type ) ) {
106
139
returnType = `Array<${ returnType } >` ;
107
140
}
@@ -150,7 +183,9 @@ function generateSubscriptionOperation(generator: CodeGenerator, op: LegacyOpera
150
183
const params = [ 'statement' ] ;
151
184
variableAssignmentToInput ( generator , vars ) ;
152
185
params . push ( 'gqlAPIServiceArguments' ) ;
153
- generator . printOnNewline ( `return API.graphql(graphqlOperation(${ params . join ( ', ' ) } )) as Observable<SubscriptionResponse<${ returnType } >>;` ) ;
186
+ generator . printOnNewline (
187
+ `return API.graphql(graphqlOperation(${ params . join ( ', ' ) } )) as Observable<SubscriptionResponse<${ returnType } >>;` ,
188
+ ) ;
154
189
generator . printOnNewline ( '}' ) ;
155
190
} ) ;
156
191
}
0 commit comments