Skip to content

Commit 10e9654

Browse files
committed
refactor
1 parent 4dbe973 commit 10e9654

File tree

2 files changed

+130
-88
lines changed

2 files changed

+130
-88
lines changed

src/definitions/field.ts

Lines changed: 111 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
1313

14-
import { buildTypeMetadata } from "../utils/build-type-metadata";
14+
import { buildTypeMetadata, TypeMetadata } from "../utils/build-type-metadata";
1515
import {
1616
FieldDefinitionNode,
1717
GraphQLSchema,
@@ -24,6 +24,7 @@ import { CodegenConfigWithDefaults } from "../config/build-config-with-defaults"
2424
import { indent } from "@graphql-codegen/visitor-plugin-common";
2525
import { buildAnnotations } from "../annotations/build-annotations";
2626
import { findTypeInResolverInterfacesConfig } from "../config/find-type-in-resolver-interfaces-config";
27+
import { shouldGenerateFunctionsInClass } from "./object";
2728

2829
export 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

9081
export 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

148187
function 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
}

src/definitions/object.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,10 @@ export function buildObjectTypeDefinition(
7272
const fieldNodes = typeInResolverInterfacesConfig
7373
? node.fields
7474
: fieldsWithArguments;
75-
const shouldGenerateFunctions = Boolean(
76-
typeInResolverInterfacesConfig ||
77-
node.fields?.some((fieldNode) => fieldNode.arguments?.length),
78-
);
7975

80-
if (node.name.value === "Query" || node.name.value === "Mutation") {
76+
const isTopLevelType =
77+
node.name.value === "Query" || node.name.value === "Mutation";
78+
if (isTopLevelType) {
8179
const individualQueryClasses = node.fields?.map((fieldNode) => {
8280
const className = `${titleCase(fieldNode.name.value)}${node.name.value}`;
8381
return `${annotations}${outputRestrictionAnnotation}open class ${className}${interfaceInheritance} {
@@ -92,6 +90,10 @@ ${getClassMembers({ node, fieldNodes, schema, config })}
9290
);
9391
}
9492

93+
const shouldGenerateFunctions = shouldGenerateFunctionsInClass(
94+
node,
95+
typeInResolverInterfacesConfig,
96+
);
9597
if (shouldGenerateFunctions) {
9698
const atLeastOneFieldHasNoArguments = node.fields?.some(
9799
(fieldNode) => !fieldNode.arguments?.length,
@@ -143,6 +145,18 @@ function getClassMembers({
143145
.join("\n");
144146
}
145147

148+
export function shouldGenerateFunctionsInClass(
149+
node: ObjectTypeDefinitionNode,
150+
typeInResolverInterfacesConfig: ReturnType<
151+
typeof findTypeInResolverInterfacesConfig
152+
>,
153+
) {
154+
return Boolean(
155+
typeInResolverInterfacesConfig ||
156+
node.fields?.some((fieldNode) => fieldNode.arguments?.length),
157+
);
158+
}
159+
146160
function titleCase(str: string) {
147161
return str.charAt(0).toUpperCase() + str.slice(1);
148162
}

0 commit comments

Comments
 (0)