Skip to content

Commit

Permalink
Revert "fix: use related fields for has_one and belongs_to properties…
Browse files Browse the repository at this point in the history
… when building GraphQL API payloads"

This reverts commit e12116a.
  • Loading branch information
awinberg-aws committed Aug 11, 2023
1 parent 4fdcb32 commit c7df29a
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 160 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1001,9 +1001,9 @@ describe('amplify form renderer tests', () => {
);

expect(componentText).toContain('postCommentsId');
expect(componentText).toContain('postID');
expect(componentText).toContain('userCommentsId');
expect(componentText).toContain('orgCommentsId');
expect(componentText).not.toContain('postID');
expect(componentText).not.toContain('userCommentsId');
expect(componentText).not.toContain('orgCommentsId');
expect(componentText).toMatchSnapshot();
expect(declaration).toMatchSnapshot();
});
Expand Down
8 changes: 1 addition & 7 deletions packages/codegen-ui-react/lib/amplify-ui-renderers/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,7 @@ export default class FormRenderer extends ReactComponentRenderer<BaseComponentPr
[],
),
),
buildModelFieldObject(
dataSourceType !== 'DataStore',
formMetadata?.fieldConfigs,
this.componentMetadata.dataSchemaMetadata?.models,
undefined,
this.isRenderingGraphQL(),
),
buildModelFieldObject(dataSourceType !== 'DataStore', formMetadata?.fieldConfigs),
...onSubmitValidationRun(hasModelField),
...this.getOnSubmitDSCall(),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ export const addFormAttributes = (
if (formMetadata.formActionType === 'update' && !fieldConfig.isArray && !isControlledComponent(componentType)) {
attributes.push(renderDefaultValueAttribute(renderedVariableName, fieldConfig, componentType));
}
attributes.push(
buildOnChangeStatement(component, formMetadata.fieldConfigs, dataApi, dataSchema?.models, dataApi === 'GraphQL'),
);
attributes.push(buildOnChangeStatement(component, formMetadata.fieldConfigs, dataApi));
attributes.push(buildOnBlurStatement(componentName, fieldConfig));
attributes.push(
factory.createJsxAttribute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
isValidVariableName,
shouldIncludeCancel,
InvalidInputError,
GenericDataModel,
} from '@aws-amplify/codegen-ui';
import {
BindingElement,
Expand Down Expand Up @@ -212,8 +211,6 @@ export const buildOverrideOnChangeStatement = (
fieldName: string,
fieldConfigs: Record<string, FieldConfigMetadata>,
valueNameOverride?: Identifier,
models?: Record<string, GenericDataModel>,
isRenderingGraphQL = false,
): IfStatement => {
const keyPath = fieldName.split('.');
const keyName = keyPath[0];
Expand All @@ -232,15 +229,9 @@ export const buildOverrideOnChangeStatement = (
factory.createIdentifier('onChange'),
factory.createBlock(
[
buildModelFieldObject(
true,
fieldConfigs,
models || {},
{
[keyName]: keyValueExpression,
},
isRenderingGraphQL,
),
buildModelFieldObject(true, fieldConfigs, {
[keyName]: keyValueExpression,
}),
factory.createVariableStatement(
undefined,
factory.createVariableDeclarationList(
Expand Down Expand Up @@ -295,9 +286,7 @@ function getCallbackVarName(fieldType: string): string {
export const buildOnChangeStatement = (
component: StudioComponent | StudioComponentChild,
fieldConfigs: Record<string, FieldConfigMetadata>,
dataApi: DataApiKind | undefined,
models: Record<string, GenericDataModel> = {},
isRenderingGraphQL = false,
dataApi?: DataApiKind,
) => {
const { name: fieldName, componentType: fieldType } = component;
const fieldConfig = fieldConfigs[fieldName];
Expand All @@ -320,9 +309,7 @@ export const buildOnChangeStatement = (
}

if (!shouldWrapInArrayField(fieldConfig)) {
handleChangeStatements.push(
buildOverrideOnChangeStatement(fieldName, fieldConfigs, undefined, models, isRenderingGraphQL),
);
handleChangeStatements.push(buildOverrideOnChangeStatement(fieldName, fieldConfigs));
}

handleChangeStatements.push(getOnChangeValidationBlock(fieldName));
Expand Down Expand Up @@ -528,7 +515,6 @@ export const buildStorageManagerOnChangeStatement = (
component: StudioComponent | StudioComponentChild,
fieldConfigs: Record<string, FieldConfigMetadata>,
handlerName: 'onUploadSuccess' | 'onFileRemove',
isRenderingGraphQL: boolean,
) => {
const { name: fieldName } = component;
const fieldConfig = fieldConfigs[fieldName];
Expand Down Expand Up @@ -595,7 +581,7 @@ export const buildStorageManagerOnChangeStatement = (
NodeFlags.Let,
),
),
buildOverrideOnChangeStatement(fieldName, fieldConfigs, undefined, undefined, isRenderingGraphQL),
buildOverrideOnChangeStatement(fieldName, fieldConfigs),
factory.createReturnStatement(factory.createIdentifier('value')),
],
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/
import { factory, NodeFlags, ObjectLiteralElementLike } from 'typescript';
import { FieldConfigMetadata, GenericDataModel } from '@aws-amplify/codegen-ui';
import { FieldConfigMetadata } from '@aws-amplify/codegen-ui';

/**
* builds modelFields object which is used to validate, onSubmit, onSuccess/onError
Expand All @@ -33,49 +33,30 @@ import { FieldConfigMetadata, GenericDataModel } from '@aws-amplify/codegen-ui';
export const buildModelFieldObject = (
shouldBeConst: boolean,
fieldConfigs: Record<string, FieldConfigMetadata> = {},
models: Record<string, GenericDataModel> = {},
nameOverrides: Record<string, ObjectLiteralElementLike> = {},
isRenderingGraphQL = false,
) => {
const fieldSet = new Set<string>();
const fields = Object.keys(fieldConfigs).reduce<ObjectLiteralElementLike[]>((acc, value) => {
const fieldName = value.split('.')[0];
const { sanitizedFieldName, relationship } = fieldConfigs[value];
const { sanitizedFieldName } = fieldConfigs[value];
const renderedFieldName = sanitizedFieldName || fieldName;
if (!fieldSet.has(renderedFieldName)) {
let assignments: ObjectLiteralElementLike[] = [
factory.createShorthandPropertyAssignment(factory.createIdentifier(fieldName), undefined),
];
let assignment: ObjectLiteralElementLike = factory.createShorthandPropertyAssignment(
factory.createIdentifier(fieldName),
undefined,
);

if (nameOverrides[fieldName]) {
assignments = [nameOverrides[fieldName]];
} else if (
isRenderingGraphQL &&
typeof fieldConfigs[value].dataType === 'object' &&
relationship &&
(relationship.type === 'BELONGS_TO' || relationship.type === 'HAS_ONE')
) {
assignments =
relationship.associatedFields?.map((associatedField, index) => {
return factory.createPropertyAssignment(
factory.createStringLiteral(associatedField),
factory.createPropertyAccessChain(
factory.createIdentifier(fieldName),
undefined,
models[relationship.relatedModelName].primaryKeys[index],
),
);
}) || [];
assignment = nameOverrides[fieldName];
} else if (sanitizedFieldName) {
// if overrides present, ignore sanitizedFieldName
assignments = [
factory.createPropertyAssignment(
factory.createStringLiteral(fieldName),
factory.createIdentifier(sanitizedFieldName),
),
];
assignment = factory.createPropertyAssignment(
factory.createStringLiteral(fieldName),
factory.createIdentifier(sanitizedFieldName),
);
}

acc.push(...assignments);
acc.push(assignment);
fieldSet.add(renderedFieldName);
}
return acc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
import { FieldConfigMetadata, GenericDataModel, LabelDecorator } from '@aws-amplify/codegen-ui';
import { FieldConfigMetadata, LabelDecorator } from '@aws-amplify/codegen-ui';
import { Expression, factory, Identifier, JsxAttribute, JsxChild, NodeFlags, SyntaxKind } from 'typescript';
import {
buildAccessChain,
Expand All @@ -38,16 +38,12 @@ function getOnChangeAttribute({
renderedFieldName,
fieldConfigs,
isLimitedToOneValue,
models,
isRenderingGraphQL,
}: {
setStateName: Identifier;
fieldName: string;
renderedFieldName: string;
fieldConfigs: Record<string, FieldConfigMetadata>;
isLimitedToOneValue?: boolean;
models: Record<string, GenericDataModel> | undefined;
isRenderingGraphQL: boolean;
}): JsxAttribute {
const fieldConfig = fieldConfigs[fieldName];
const { dataType, componentType } = fieldConfig;
Expand Down Expand Up @@ -101,7 +97,7 @@ function getOnChangeAttribute({
NodeFlags.Let,
),
),
buildOverrideOnChangeStatement(fieldName, fieldConfigs, valueName, models, isRenderingGraphQL),
buildOverrideOnChangeStatement(fieldName, fieldConfigs, valueName),
...setStateStatements,
],
true,
Expand Down Expand Up @@ -136,7 +132,6 @@ export const renderArrayFieldComponent = (
labelDecorator?: LabelDecorator,
isRequired?: boolean,
dataApi: DataApiKind = 'DataStore',
models: Record<string, GenericDataModel> = {},
) => {
const fieldConfig = fieldConfigs[fieldName];
const { sanitizedFieldName, dataType, componentType } = fieldConfig;
Expand Down Expand Up @@ -177,17 +172,7 @@ export const renderArrayFieldComponent = (
);
}

props.push(
getOnChangeAttribute({
fieldName,
isLimitedToOneValue,
fieldConfigs,
renderedFieldName,
setStateName,
models,
isRenderingGraphQL: dataApi === 'GraphQL',
}),
);
props.push(getOnChangeAttribute({ fieldName, isLimitedToOneValue, fieldConfigs, renderedFieldName, setStateName }));
let labelAttribute = factory.createJsxAttribute(
factory.createIdentifier('label'),
factory.createJsxExpression(undefined, factory.createStringLiteral(fieldLabel)),
Expand Down
2 changes: 0 additions & 2 deletions packages/codegen-ui-react/lib/react-component-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ export class ReactComponentRenderer<TPropIn> extends ComponentRendererBase<
fieldConfigs,
labelDecorator,
isRequired,
this.importCollection.rendererConfig?.apiConfiguration?.dataApi === 'GraphQL',
);
}

Expand All @@ -119,7 +118,6 @@ export class ReactComponentRenderer<TPropIn> extends ComponentRendererBase<
labelDecorator,
isRequired,
this.importCollection.rendererConfig?.apiConfiguration?.dataApi,
this.componentMetadata.dataSchemaMetadata?.models,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ export const renderStorageFieldComponent = (
fieldConfigs: Record<string, FieldConfigMetadata>,
labelDecorator?: LabelDecorator,
isRequired?: boolean,
isRenderingGraphQL = false,
) => {
const { name: componentName } = component;
const dataTypeName = componentMetadata.formMetadata?.dataType.dataTypeName || '';
Expand Down Expand Up @@ -419,12 +418,8 @@ export const renderStorageFieldComponent = (
);
}

storageManagerAttributes.push(
buildStorageManagerOnChangeStatement(component, fieldConfigs, 'onUploadSuccess', isRenderingGraphQL),
);
storageManagerAttributes.push(
buildStorageManagerOnChangeStatement(component, fieldConfigs, 'onFileRemove', isRenderingGraphQL),
);
storageManagerAttributes.push(buildStorageManagerOnChangeStatement(component, fieldConfigs, 'onUploadSuccess'));
storageManagerAttributes.push(buildStorageManagerOnChangeStatement(component, fieldConfigs, 'onFileRemove'));
storageManagerAttributes.push(
factory.createJsxAttribute(
factory.createIdentifier('processFile'),
Expand Down

0 comments on commit c7df29a

Please sign in to comment.