@@ -11,7 +11,7 @@ See the License for the specific language governing permissions and
11
11
limitations under the License.
12
12
*/
13
13
14
- import { buildTypeMetadata } from "../utils/build-type-metadata" ;
14
+ import { buildTypeMetadata , TypeMetadata } from "../utils/build-type-metadata" ;
15
15
import {
16
16
FieldDefinitionNode ,
17
17
GraphQLSchema ,
@@ -24,6 +24,7 @@ import { CodegenConfigWithDefaults } from "../config/build-config-with-defaults"
24
24
import { indent } from "@graphql-codegen/visitor-plugin-common" ;
25
25
import { buildAnnotations } from "../annotations/build-annotations" ;
26
26
import { findTypeInResolverInterfacesConfig } from "../config/find-type-in-resolver-interfaces-config" ;
27
+ import { shouldGenerateFunctionsInClass } from "./object" ;
27
28
28
29
export function buildObjectFieldDefinition ( {
29
30
node,
@@ -48,43 +49,33 @@ export function buildObjectFieldDefinition({
48
49
config ,
49
50
) ;
50
51
const typeMetadata = buildTypeMetadata ( fieldNode . type , schema , config ) ;
51
- const annotations = buildAnnotations ( {
52
- config,
53
- definitionNode : fieldNode ,
54
- typeMetadata,
55
- } ) ;
56
-
57
- const notImplementedError = `throw NotImplementedError("${ node . name . value } .${ fieldNode . name . value } must be implemented.")` ;
58
- const atLeastOneFieldHasNoArguments = node . fields ?. some (
59
- ( fieldNode ) => ! fieldNode . arguments ?. length ,
52
+ const defaultImplementation = getDefaultImplementation (
53
+ node ,
54
+ fieldNode ,
55
+ typeInResolverInterfacesConfig ,
60
56
) ;
61
- const defaultImplementation =
62
- ! typeInResolverInterfacesConfig && atLeastOneFieldHasNoArguments
63
- ? fieldNode . name . value
64
- : notImplementedError ;
65
57
const defaultFunctionValue = `${ typeMetadata . isNullable ? "?" : "" } = ${ defaultImplementation } ` ;
66
- const shouldGenerateFunctions = Boolean (
67
- typeInResolverInterfacesConfig ||
68
- node . fields ?. some ( ( fieldNode ) => fieldNode . arguments ?. length ) ,
58
+ const shouldGenerateFunctions = shouldGenerateFunctionsInClass (
59
+ node ,
60
+ typeInResolverInterfacesConfig ,
69
61
) ;
70
62
const defaultValue = shouldGenerateFunctions
71
63
? defaultFunctionValue
72
64
: typeMetadata . defaultValue ;
73
- const defaultDefinition = `${ typeMetadata . typeName } ${ defaultValue } ` ;
74
-
75
- const isCompletableFuture =
76
- typeInResolverInterfacesConfig ?. classMethods === "COMPLETABLE_FUTURE" ;
77
- const completableFutureDefinition = `java.util.concurrent.CompletableFuture<${ typeMetadata . typeName } ${ typeMetadata . isNullable ? "?" : "" } > = ${ defaultImplementation } ` ;
78
- const field = indent (
79
- `${ functionDefinition } : ${ isCompletableFuture ? completableFutureDefinition : defaultDefinition } ` ,
80
- 2 ,
81
- ) ;
82
- const fieldIndex = node . fields ?. findIndex (
83
- ( field ) => field . name . value === fieldNode . name . value ,
65
+ const field = buildField (
66
+ node ,
67
+ fieldNode ,
68
+ functionDefinition ,
69
+ defaultValue ,
70
+ typeInResolverInterfacesConfig ,
71
+ typeMetadata ,
84
72
) ;
85
- const isLastFieldInType =
86
- node . fields && fieldIndex === node . fields . length - 1 ;
87
- return `${ annotations } ${ field } ${ shouldGenerateFunctions || isLastFieldInType ? "" : "," } ` ;
73
+ const annotations = buildAnnotations ( {
74
+ config,
75
+ definitionNode : fieldNode ,
76
+ typeMetadata,
77
+ } ) ;
78
+ return `${ annotations } ${ field } ${ shouldGenerateFunctions || isLastFieldInType ( node , fieldNode ) ? "" : "," } ` ;
88
79
}
89
80
90
81
export function buildConstructorFieldDefinition ( {
@@ -109,40 +100,88 @@ export function buildConstructorFieldDefinition({
109
100
typeInResolverInterfacesConfig ,
110
101
) ;
111
102
const typeMetadata = buildTypeMetadata ( fieldNode . type , schema , config ) ;
103
+ const defaultDefinitionValue = typeMetadata . defaultValue ;
104
+
105
+ const field = buildField (
106
+ node ,
107
+ fieldNode ,
108
+ functionDefinition ,
109
+ defaultDefinitionValue ,
110
+ typeInResolverInterfacesConfig ,
111
+ typeMetadata ,
112
+ ) ;
112
113
const annotations = buildAnnotations ( {
113
114
config,
114
115
definitionNode : fieldNode ,
115
116
typeMetadata,
116
117
} ) ;
118
+ const shouldGenerateFunctions = shouldGenerateFunctionsInClass (
119
+ node ,
120
+ typeInResolverInterfacesConfig ,
121
+ ) ;
122
+ return `${ annotations } ${ field } ${ shouldGenerateFunctions || isLastFieldInType ( node , fieldNode ) ? "" : "," } ` ;
123
+ }
117
124
118
- const notImplementedError = `throw NotImplementedError("${ node . name . value } .${ fieldNode . name . value } must be implemented.")` ;
119
- const atLeastOneFieldHasNoArguments = node . fields ?. some (
120
- ( fieldNode ) => ! fieldNode . arguments ?. length ,
125
+ export function buildInterfaceFieldDefinition ( {
126
+ node,
127
+ fieldNode,
128
+ schema,
129
+ config,
130
+ } : {
131
+ node : InterfaceTypeDefinitionNode ;
132
+ fieldNode : FieldDefinitionNode ;
133
+ schema : GraphQLSchema ;
134
+ config : CodegenConfigWithDefaults ;
135
+ } ) {
136
+ const typeInResolverInterfacesConfig = findTypeInResolverInterfacesConfig (
137
+ node ,
138
+ config ,
121
139
) ;
122
- const defaultImplementation =
123
- ! typeInResolverInterfacesConfig && atLeastOneFieldHasNoArguments
124
- ? fieldNode . name . value
125
- : notImplementedError ;
126
- const shouldGenerateFunctions = Boolean (
127
- typeInResolverInterfacesConfig ||
128
- node . fields ?. some ( ( fieldNode ) => fieldNode . arguments ?. length ) ,
140
+ const functionDefinition = buildFunctionDefinition (
141
+ node ,
142
+ fieldNode ,
143
+ schema ,
144
+ typeInResolverInterfacesConfig ,
145
+ config ,
146
+ ) ;
147
+ const typeMetadata = buildTypeMetadata ( fieldNode . type , schema , config ) ;
148
+ const fieldText = indent (
149
+ `${ functionDefinition } : ${ typeMetadata . typeName } ${
150
+ typeMetadata . isNullable ? "?" : ""
151
+ } `,
152
+ 2 ,
129
153
) ;
130
- const defaultValue = typeMetadata . defaultValue ;
131
- const defaultDefinition = `${ typeMetadata . typeName } ${ defaultValue } ` ;
154
+ const annotations = buildAnnotations ( {
155
+ config,
156
+ definitionNode : fieldNode ,
157
+ typeMetadata,
158
+ } ) ;
159
+ return `${ annotations } ${ fieldText } ` ;
160
+ }
132
161
162
+ function buildField (
163
+ node : ObjectTypeDefinitionNode ,
164
+ fieldNode : FieldDefinitionNode ,
165
+ functionDefinition : string ,
166
+ defaultDefinitionValue : string ,
167
+ typeInResolverInterfacesConfig : ReturnType <
168
+ typeof findTypeInResolverInterfacesConfig
169
+ > ,
170
+ typeMetadata : TypeMetadata ,
171
+ ) {
172
+ const defaultImplementation = getDefaultImplementation (
173
+ node ,
174
+ fieldNode ,
175
+ typeInResolverInterfacesConfig ,
176
+ ) ;
133
177
const isCompletableFuture =
134
178
typeInResolverInterfacesConfig ?. classMethods === "COMPLETABLE_FUTURE" ;
135
179
const completableFutureDefinition = `java.util.concurrent.CompletableFuture<${ typeMetadata . typeName } ${ typeMetadata . isNullable ? "?" : "" } > = ${ defaultImplementation } ` ;
136
- const field = indent (
180
+ const defaultDefinition = `${ typeMetadata . typeName } ${ defaultDefinitionValue } ` ;
181
+ return indent (
137
182
`${ functionDefinition } : ${ isCompletableFuture ? completableFutureDefinition : defaultDefinition } ` ,
138
183
2 ,
139
184
) ;
140
- const fieldIndex = node . fields ?. findIndex (
141
- ( field ) => field . name . value === fieldNode . name . value ,
142
- ) ;
143
- const isLastFieldInType =
144
- node . fields && fieldIndex === node . fields . length - 1 ;
145
- return `${ annotations } ${ field } ${ shouldGenerateFunctions || isLastFieldInType ? "" : "," } ` ;
146
185
}
147
186
148
187
function buildFunctionDefinition (
@@ -269,39 +308,28 @@ function shouldModifyFieldWithOverride(
269
308
} ) ;
270
309
}
271
310
272
- export function buildInterfaceFieldDefinition ( {
273
- node,
274
- fieldNode,
275
- schema,
276
- config,
277
- } : {
278
- node : InterfaceTypeDefinitionNode ;
279
- fieldNode : FieldDefinitionNode ;
280
- schema : GraphQLSchema ;
281
- config : CodegenConfigWithDefaults ;
282
- } ) {
283
- const typeInResolverInterfacesConfig = findTypeInResolverInterfacesConfig (
284
- node ,
285
- config ,
286
- ) ;
287
- const functionDefinition = buildFunctionDefinition (
288
- node ,
289
- fieldNode ,
290
- schema ,
291
- typeInResolverInterfacesConfig ,
292
- config ,
311
+ function getDefaultImplementation (
312
+ node : ObjectTypeDefinitionNode ,
313
+ fieldNode : FieldDefinitionNode ,
314
+ typeInResolverInterfacesConfig : ReturnType <
315
+ typeof findTypeInResolverInterfacesConfig
316
+ > ,
317
+ ) {
318
+ const notImplementedError = `throw NotImplementedError("${ node . name . value } .${ fieldNode . name . value } must be implemented.")` ;
319
+ const atLeastOneFieldHasNoArguments = node . fields ?. some (
320
+ ( fieldNode ) => ! fieldNode . arguments ?. length ,
293
321
) ;
294
- const typeMetadata = buildTypeMetadata ( fieldNode . type , schema , config ) ;
295
- const fieldText = indent (
296
- `${ functionDefinition } : ${ typeMetadata . typeName } ${
297
- typeMetadata . isNullable ? "?" : ""
298
- } `,
299
- 2 ,
322
+ return ! typeInResolverInterfacesConfig && atLeastOneFieldHasNoArguments
323
+ ? fieldNode . name . value
324
+ : notImplementedError ;
325
+ }
326
+
327
+ function isLastFieldInType (
328
+ node : ObjectTypeDefinitionNode ,
329
+ fieldNode : FieldDefinitionNode ,
330
+ ) {
331
+ const fieldIndex = node . fields ?. findIndex (
332
+ ( field ) => field . name . value === fieldNode . name . value ,
300
333
) ;
301
- const annotations = buildAnnotations ( {
302
- config,
303
- definitionNode : fieldNode ,
304
- typeMetadata,
305
- } ) ;
306
- return `${ annotations } ${ fieldText } ` ;
334
+ return node . fields && fieldIndex === node . fields . length - 1 ;
307
335
}
0 commit comments