@@ -11,7 +11,7 @@ See the License for the specific language governing permissions and
1111limitations under the License.
1212*/
1313
14- import { buildTypeMetadata } from "../utils/build-type-metadata" ;
14+ import { buildTypeMetadata , TypeMetadata } from "../utils/build-type-metadata" ;
1515import {
1616 FieldDefinitionNode ,
1717 GraphQLSchema ,
@@ -24,6 +24,7 @@ import { CodegenConfigWithDefaults } from "../config/build-config-with-defaults"
2424import { indent } from "@graphql-codegen/visitor-plugin-common" ;
2525import { buildAnnotations } from "../annotations/build-annotations" ;
2626import { findTypeInResolverInterfacesConfig } from "../config/find-type-in-resolver-interfaces-config" ;
27+ import { shouldGenerateFunctionsInClass } from "./object" ;
2728
2829export function buildObjectFieldDefinition ( {
2930 node,
@@ -48,43 +49,33 @@ export function buildObjectFieldDefinition({
4849 config ,
4950 ) ;
5051 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 ,
6056 ) ;
61- const defaultImplementation =
62- ! typeInResolverInterfacesConfig && atLeastOneFieldHasNoArguments
63- ? fieldNode . name . value
64- : notImplementedError ;
6557 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 ,
6961 ) ;
7062 const defaultValue = shouldGenerateFunctions
7163 ? defaultFunctionValue
7264 : 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 ,
8472 ) ;
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 ) ? "" : "," } ` ;
8879}
8980
9081export function buildConstructorFieldDefinition ( {
@@ -109,40 +100,88 @@ export function buildConstructorFieldDefinition({
109100 typeInResolverInterfacesConfig ,
110101 ) ;
111102 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+ ) ;
112113 const annotations = buildAnnotations ( {
113114 config,
114115 definitionNode : fieldNode ,
115116 typeMetadata,
116117 } ) ;
118+ const shouldGenerateFunctions = shouldGenerateFunctionsInClass (
119+ node ,
120+ typeInResolverInterfacesConfig ,
121+ ) ;
122+ return `${ annotations } ${ field } ${ shouldGenerateFunctions || isLastFieldInType ( node , fieldNode ) ? "" : "," } ` ;
123+ }
117124
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 ,
121139 ) ;
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 ,
129153 ) ;
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+ }
132161
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+ ) ;
133177 const isCompletableFuture =
134178 typeInResolverInterfacesConfig ?. classMethods === "COMPLETABLE_FUTURE" ;
135179 const completableFutureDefinition = `java.util.concurrent.CompletableFuture<${ typeMetadata . typeName } ${ typeMetadata . isNullable ? "?" : "" } > = ${ defaultImplementation } ` ;
136- const field = indent (
180+ const defaultDefinition = `${ typeMetadata . typeName } ${ defaultDefinitionValue } ` ;
181+ return indent (
137182 `${ functionDefinition } : ${ isCompletableFuture ? completableFutureDefinition : defaultDefinition } ` ,
138183 2 ,
139184 ) ;
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 ? "" : "," } ` ;
146185}
147186
148187function buildFunctionDefinition (
@@ -269,39 +308,28 @@ function shouldModifyFieldWithOverride(
269308 } ) ;
270309}
271310
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 ,
293321 ) ;
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 ,
300333 ) ;
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 ;
307335}
0 commit comments