Skip to content

Commit

Permalink
fix: update inputs for linking and unlinking bidirectional relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
rtpascual committed Aug 18, 2023
1 parent 99ea1ae commit c52c565
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ export default function CreateOwnerForm(props) {
query: updateDog,
variables: {
input: {
...Dog,
owner: owner,
id: Dog.id,
dogOwnerId: owner.id,
},
},
})
Expand All @@ -355,9 +355,8 @@ export default function CreateOwnerForm(props) {
query: updateOwner,
variables: {
input: {
...ownerToUnlink,
Dog: undefined,
ownerDogId: undefined,
id: ownerToUnlink.id,
ownerDogId: null,
},
},
})
Expand Down Expand Up @@ -15632,8 +15631,11 @@ export default function CreateCompositeDogForm(props) {
query: updateCompositeOwner,
variables: {
input: {
...CompositeOwner,
CompositeDog: compositeDog,
lastName: CompositeOwner.lastName,
firstName: CompositeOwner.firstName,
compositeOwnerCompositeDogName: compositeDog.name,
compositeOwnerCompositeDogDescription:
compositeDog.description,
},
},
})
Expand All @@ -15646,10 +15648,10 @@ export default function CreateCompositeDogForm(props) {
query: updateCompositeDog,
variables: {
input: {
...compositeDogToUnlink,
CompositeOwner: undefined,
compositeDogCompositeOwnerLastName: undefined,
compositeDogCompositeOwnerFirstName: undefined,
name: compositeDogToUnlink.name,
description: compositeDogToUnlink.description,
compositeDogCompositeOwnerLastName: null,
compositeDogCompositeOwnerFirstName: null,
},
},
})
Expand Down Expand Up @@ -18708,7 +18710,6 @@ import {
useTheme,
} from \\"@aws-amplify/ui-react\\";
import { getOverrideProps } from \\"@aws-amplify/ui-react/internal\\";
import { Owner } from \\"../API\\";
import { fetchByPath, validateField } from \\"./utils\\";
import { API } from \\"aws-amplify\\";
import { listOwners } from \\"../graphql/queries\\";
Expand Down Expand Up @@ -19034,8 +19035,8 @@ export default function CreateDogForm(props) {
query: updateOwner,
variables: {
input: {
...Owner,
Dog: dog,
id: owner.id,
ownerDogId: dog.id,
},
},
})
Expand Down Expand Up @@ -19581,8 +19582,8 @@ export default function CreateOwnerForm(props) {
query: updateDog,
variables: {
input: {
...Dog,
owner: owner,
id: Dog.id,
dogOwnerId: owner.id,
},
},
})
Expand All @@ -19594,9 +19595,8 @@ export default function CreateOwnerForm(props) {
query: updateOwner,
variables: {
input: {
...ownerToUnlink,
Dog: undefined,
ownerDogId: undefined,
id: ownerToUnlink.id,
ownerDogId: null,
},
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ function getFieldBiDirectionalWith({

function unlinkModelRecordExpression({
modelName,
primaryKeys,
recordNameToUnlink,
fieldName,
associatedFields,
importCollection,
dataApi,
}: {
modelName: string;
primaryKeys: string[];
recordNameToUnlink: string;
fieldName: string;
associatedFields: string[];
Expand All @@ -100,10 +102,14 @@ function unlinkModelRecordExpression({
}) {
if (dataApi === 'GraphQL') {
const inputs = [
factory.createSpreadAssignment(factory.createIdentifier(recordNameToUnlink)),
factory.createPropertyAssignment(factory.createIdentifier(fieldName), factory.createIdentifier('undefined')),
...primaryKeys.map((primaryKey) =>
factory.createPropertyAssignment(
factory.createIdentifier(primaryKey),
factory.createPropertyAccessChain(factory.createIdentifier(recordNameToUnlink), undefined, primaryKey),
),
),
...associatedFields.map((field) =>
factory.createPropertyAssignment(factory.createIdentifier(field), factory.createIdentifier('undefined')),
factory.createPropertyAssignment(factory.createIdentifier(field), factory.createNull()),
),
];

Expand Down Expand Up @@ -254,23 +260,38 @@ function linkModelRecordExpression({
importedRelatedModelName,
relatedRecordToLink,
fieldBiDirectionalWithName,
fieldName,
currentRecord,
importCollection,
primaryKeys,
associatedPrimaryKeys,
associatedFieldsBiDirectionalWith,
dataApi,
}: {
importedRelatedModelName: string;
relatedRecordToLink: string;
fieldBiDirectionalWithName: string;
fieldName: string;
currentRecord: string;
importCollection: ImportCollection;
primaryKeys: string[];
associatedPrimaryKeys: string[];
associatedFieldsBiDirectionalWith: string[];
dataApi?: DataApiKind;
}) {
if (dataApi === 'GraphQL') {
const inputs = [
factory.createSpreadAssignment(factory.createIdentifier(importedRelatedModelName)),
factory.createPropertyAssignment(
factory.createIdentifier(fieldBiDirectionalWithName),
factory.createIdentifier(currentRecord),
...associatedPrimaryKeys.map((associatedPrimaryKey) =>
factory.createPropertyAssignment(
factory.createIdentifier(associatedPrimaryKey),
factory.createPropertyAccessChain(factory.createIdentifier(fieldName), undefined, associatedPrimaryKey),
),
),
...associatedFieldsBiDirectionalWith.map((associatedField, index) =>
factory.createPropertyAssignment(
factory.createIdentifier(associatedField),
factory.createPropertyAccessChain(factory.createIdentifier(currentRecord), undefined, primaryKeys[index]),
),
),
];

Expand Down Expand Up @@ -351,6 +372,7 @@ export function getBiDirectionalRelationshipStatements({
fieldConfig,
modelName,
savedRecordName,
thisModelPrimaryKeys,
dataApi,
}: {
formActionType: 'create' | 'update';
Expand All @@ -359,6 +381,7 @@ export function getBiDirectionalRelationshipStatements({
fieldConfig: [string, FieldConfigMetadata];
modelName: string;
savedRecordName: string;
thisModelPrimaryKeys: string[];
dataApi?: DataApiKind;
}) {
const getFieldBiDirectionalWithReturnValue = getFieldBiDirectionalWith({
Expand Down Expand Up @@ -445,6 +468,7 @@ export function getBiDirectionalRelationshipStatements({
})
: unlinkModelRecordExpression({
modelName: importedRelatedModelName,
primaryKeys: thisModelPrimaryKeys,
recordNameToUnlink: relatedRecordToUnlink,
fieldName: fieldBiDirectionalWithName,
associatedFields: associatedFieldsBiDirectionalWith,
Expand Down Expand Up @@ -530,8 +554,12 @@ export function getBiDirectionalRelationshipStatements({
importedRelatedModelName,
relatedRecordToLink,
fieldBiDirectionalWithName,
fieldName,
currentRecord,
importCollection,
primaryKeys: thisModelPrimaryKeys,
associatedPrimaryKeys: fieldBiDirectionalWithPrimaryKeys,
associatedFieldsBiDirectionalWith,
dataApi,
}),
factory.createVariableStatement(
Expand Down Expand Up @@ -571,6 +599,7 @@ export function getBiDirectionalRelationshipStatements({
})
: unlinkModelRecordExpression({
modelName: importedThisModelName,
primaryKeys: thisModelPrimaryKeys,
recordNameToUnlink: thisModelRecordToUnlink,
fieldName,
associatedFields,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ export const buildExpression = (
fieldConfig,
modelName,
savedRecordName,
thisModelPrimaryKeys,
dataApi,
}),
);
Expand Down

0 comments on commit c52c565

Please sign in to comment.