Skip to content

Commit 39167ed

Browse files
HCK-9658: Adjust arguments modeling and FE script generation (#53)
* HCK-9658: re-configure properties for List argument type and required option, updated FE logic accordingly * rename argument parameters according to the agreement
1 parent eb921e2 commit 39167ed

File tree

6 files changed

+1047
-131
lines changed

6 files changed

+1047
-131
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Adds required indicator.
3+
*
4+
* @param {Object} param0
5+
* @param {string} param0.type - The type name statement.
6+
* @param {boolean} param0.required - Indicates if the field is required.
7+
* @returns {string} - The type name with required indicator.
8+
*/
9+
function addRequired({ type, required }) {
10+
if (required) {
11+
return `${type}!`;
12+
}
13+
return type;
14+
}
15+
16+
module.exports = {
17+
addRequired,
18+
};

forward_engineering/mappers/arguments.js

+30-13
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,45 @@ const { getDirectivesUsageStatement } = require('./directiveUsageStatements');
66
const { getArgumentDefaultValue } = require('./argumentDefaultValue');
77
const { joinInlineStatements } = require('../helpers/feStatementJoinHelper');
88
const { formatFEStatement } = require('../helpers/feStatementFormatHelper');
9+
const { addRequired } = require('../helpers/addRequiredHelper');
910

1011
/**
1112
* Gets the type of the argument with the required keyword.
1213
* @param {Object} args - arguments object.
13-
* @param {Argument} args.argument - The argument to map.
14+
* @param {Argument} args.graphqlArgument - The argument to map.
1415
* @param {IdToNameMap} [args.idToNameMap] - The ID to name map of all available types in model.
1516
* @returns {string} returns the type of the argument with the required keyword
1617
*/
17-
const getArgumentType = ({ argument, idToNameMap = {} }) => {
18-
const argumentType = idToNameMap[argument.type] || argument.type;
19-
return argument.required ? argument.required.replace('<Type>', argumentType) : argumentType;
18+
const getArgumentType = ({ graphqlArgument, idToNameMap = {} }) => {
19+
let argumentType = idToNameMap[graphqlArgument.type] || graphqlArgument.type;
20+
21+
if (argumentType === 'List') {
22+
const firstListItem = graphqlArgument.listItems?.[0] || {};
23+
const listItemType = idToNameMap[firstListItem.type] || firstListItem.type || '';
24+
25+
if (!listItemType) {
26+
argumentType = '[]';
27+
} else {
28+
argumentType = `[${addRequired({ type: listItemType, required: firstListItem.required })}]`;
29+
}
30+
}
31+
32+
return addRequired({ type: argumentType, required: graphqlArgument.required });
2033
};
2134

2235
/**
2336
* Maps an argument to a string with all configured properties.
2437
* @param {Object} args - arguments object.
25-
* @param {Argument} args.argument - The argument to map.
38+
* @param {Argument} args.graphqlArgument - The argument to map.
2639
* @param {IdToNameMap} [args.idToNameMap] - The ID to name map of all available types in model.
2740
* @returns {FEStatement} returns the argument as a FEStatement
2841
*/
29-
const mapArgument = ({ argument, idToNameMap = {} }) => {
30-
const argumentName = `${argument.name}:`;
31-
const argumentType = getArgumentType({ argument, idToNameMap });
32-
const directivesStatement = getDirectivesUsageStatement({ directives: argument.directives });
33-
const defaultValue = argument.default
34-
? `= ${getArgumentDefaultValue({ type: argument.type, defaultValue: argument.default })}`
42+
const mapArgument = ({ graphqlArgument, idToNameMap = {} }) => {
43+
const argumentName = `${graphqlArgument.name}:`;
44+
const argumentType = getArgumentType({ graphqlArgument, idToNameMap });
45+
const directivesStatement = getDirectivesUsageStatement({ directives: graphqlArgument.directives });
46+
const defaultValue = graphqlArgument.default
47+
? `= ${getArgumentDefaultValue({ type: graphqlArgument.type, defaultValue: graphqlArgument.default })}`
3548
: '';
3649

3750
const statement = joinInlineStatements({
@@ -40,7 +53,7 @@ const mapArgument = ({ argument, idToNameMap = {} }) => {
4053

4154
return {
4255
statement,
43-
description: argument.description,
56+
description: graphqlArgument.description || '',
4457
};
4558
};
4659

@@ -58,7 +71,11 @@ const getArguments = ({ graphqlArguments, idToNameMap = {} }) => {
5871
const hasDescription = graphqlArguments.some(argument => argument.description);
5972
const argumentStatements = graphqlArguments
6073
.filter(argument => argument.name && argument.type)
61-
.map(argument => mapArgument({ argument, idToNameMap }));
74+
.map(graphqlArgument => mapArgument({ graphqlArgument, idToNameMap }));
75+
76+
if (argumentStatements.length === 0) {
77+
return '';
78+
}
6279

6380
if (!hasDescription) {
6481
// For current state of code if arguments don't have any description we return them as a single line

forward_engineering/mappers/fields.js

+5-19
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { getDefinitionNameFromReferencePath } = require('../helpers/referencesHel
77
const { getArguments } = require('./arguments');
88
const { getDirectivesUsageStatement } = require('./directiveUsageStatements');
99
const { getFieldDefaultValueStatement } = require('./fieldDefaultValue');
10+
const { addRequired } = require('../helpers/addRequiredHelper');
1011

1112
/**
1213
* @typedef {Object.<string, FieldData>} FieldsData
@@ -75,18 +76,18 @@ function mapField({ name, fieldData, required, definitionsIdToNameMap, addArgume
7576
function getFieldType({ field, required }) {
7677
if (field.$ref) {
7778
const definitionName = getDefinitionNameFromReferencePath({ referencePath: field.$ref });
78-
return addRequiredField({ field: definitionName, required });
79+
return addRequired({ type: definitionName, required });
7980
}
8081

8182
if (field.type === 'List') {
8283
const arrayItem = getFieldFromArrayItems({ items: field.items });
83-
return addRequiredField({
84-
field: `[${getFieldType({ field: arrayItem, required: arrayItem.required })}]`,
84+
return addRequired({
85+
type: `[${getFieldType({ field: arrayItem, required: arrayItem.required })}]`,
8586
required,
8687
});
8788
}
8889

89-
return addRequiredField({ field: field.type, required });
90+
return addRequired({ type: field.type, required });
9091
}
9192

9293
/**
@@ -103,21 +104,6 @@ function getFieldFromArrayItems({ items }) {
103104
return items;
104105
}
105106

106-
/**
107-
* Adds required field indicator.
108-
*
109-
* @param {Object} param0
110-
* @param {string} param0.field - The field type statement.
111-
* @param {boolean} param0.required - Indicates if the field is required.
112-
* @returns {string} - The field with required indicator.
113-
*/
114-
function addRequiredField({ field, required }) {
115-
if (required) {
116-
return `${field}!`;
117-
}
118-
return field;
119-
}
120-
121107
module.exports = {
122108
getObjectTypeFields: params => getFields({ ...params, addArguments: true, addDefaultValue: false }),
123109
getInterfaceTypeFields: params => getFields({ ...params, addArguments: true, addDefaultValue: false }),

forward_engineering/types/types.d.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ export type ArrayItem = FieldData & {
4949
};
5050

5151
// Field arguments
52-
type ArgumentRequirements = '<Type>' | '<Type>!' | '[<Type>]' | '[<Type>]!' | '[<Type>!]' | '[<Type>!]!';
52+
type ArgumentListItem = {
53+
type?: string;
54+
required?: boolean;
55+
}
5356

5457
export type Argument = {
5558
id: string;
@@ -58,7 +61,8 @@ export type Argument = {
5861
default?: string;
5962
description?: string;
6063
directives?: DirectivePropertyData[];
61-
required?: ArgumentRequirements;
64+
required?: boolean;
65+
listItems?: ArgumentListItem[];
6266
};
6367

6468
// Directives
@@ -175,4 +179,4 @@ export type ValidationResponseItem = {
175179
context?: string; // The context of the entity, typically additional information.
176180
}
177181

178-
export type ValidateScriptCallback = (error: Error | null, validationErrors?: ValidationResponseItem[]) => void;
182+
export type ValidateScriptCallback = (error: Error | null, validationErrors?: ValidationResponseItem[]) => void;

0 commit comments

Comments
 (0)