Skip to content

Commit e7320a9

Browse files
author
Dane Pilcher
authored
fix: return reference behavior to main for all but introspection (#824)
1 parent e1b2035 commit e7320a9

File tree

8 files changed

+472
-173
lines changed

8 files changed

+472
-173
lines changed

packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-dart-visitor.test.ts.snap

Lines changed: 164 additions & 64 deletions
Large diffs are not rendered by default.

packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-java-visitor.test.ts.snap

Lines changed: 192 additions & 58 deletions
Large diffs are not rendered by default.

packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap

Lines changed: 92 additions & 32 deletions
Large diffs are not rendered by default.

packages/appsync-modelgen-plugin/src/utils/process-belongs-to.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function processBelongsToConnection(
1515
modelMap: CodeGenModelMap,
1616
connectionDirective: CodeGenDirective,
1717
isCustomPKEnabled: boolean = false,
18+
respectReferences: boolean = false, // remove when enabled references for all targets
1819
): CodeGenFieldConnection | undefined {
1920
if (field.isList) {
2021
throw new Error(
@@ -28,7 +29,7 @@ export function processBelongsToConnection(
2829
`A 'belongsTo' field should match to a corresponding 'hasMany' or 'hasOne' field`
2930
);
3031
}
31-
const otherSideField = isCustomPKEnabled ? otherSideConnectedFields[0] : getConnectedFieldV2(field, model, otherSide, connectionDirective.name);
32+
const otherSideField = isCustomPKEnabled ? otherSideConnectedFields[0] : getConnectedFieldV2(field, model, otherSide, connectionDirective.name, false, respectReferences);
3233
const connectionFields = connectionDirective.arguments.fields || [];
3334

3435
const references = connectionDirective.arguments.references || [];
@@ -41,9 +42,9 @@ export function processBelongsToConnection(
4142
// but if the field are connected using fields argument in connection directive
4243
// we are reusing the field and it should be preserved in selection set
4344
const otherSideHasMany = otherSideField.isList;
44-
const isUsingReferences = references.length > 0;
45+
const isUsingReferences = respectReferences && references.length > 0;
4546
// New metada type introduced by custom PK v2 support
46-
let targetNames: string[] = [ ...connectionFields, ...references ];
47+
let targetNames = isUsingReferences ? [ ...connectionFields, ...references ] : [ ...connectionFields ];
4748
if (targetNames.length === 0) {
4849
if (otherSideHasMany) {
4950
targetNames = isCustomPKEnabled

packages/appsync-modelgen-plugin/src/utils/process-connections-v2.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export function getConnectedFieldV2(
1313
model: CodeGenModel,
1414
connectedModel: CodeGenModel,
1515
directiveName: string,
16-
shouldUseModelNameFieldInHasManyAndBelongsTo: boolean = false
16+
shouldUseModelNameFieldInHasManyAndBelongsTo: boolean = false,
17+
respectReferences: boolean = false,
1718
): CodeGenField {
1819
const connectionInfo = getDirective(field)(directiveName);
1920
if (!connectionInfo) {
@@ -23,7 +24,7 @@ export function getConnectedFieldV2(
2324
const references = connectionInfo.arguments.references;
2425
if (connectionInfo.name === 'belongsTo') {
2526
let connectedFieldsBelongsTo = getBelongsToConnectedFields(model, connectedModel);
26-
if (references) {
27+
if (respectReferences && references) {
2728
const connectedField = connectedFieldsBelongsTo.find((field) => {
2829
return field.directives.some((dir) => {
2930
return (dir.name === 'hasOne' || dir.name === 'hasMany')
@@ -49,7 +50,7 @@ export function getConnectedFieldV2(
4950
}
5051
if (references || connectionFields || directiveName === 'hasOne') {
5152
let connectionDirective;
52-
if (references) {
53+
if (respectReferences && references) {
5354
if (connectionInfo) {
5455
connectionDirective = flattenFieldDirectives(connectedModel).find((dir) => {
5556
return dir.arguments.references
@@ -161,17 +162,18 @@ export function processConnectionsV2(
161162
shouldUseModelNameFieldInHasManyAndBelongsTo: boolean = false,
162163
isCustomPKEnabled: boolean = false,
163164
shouldUseFieldsInAssociatedWithInHasOne: boolean = false,
165+
respectReferences: boolean = false, // remove when enabled references for all targets
164166
): CodeGenFieldConnection | undefined {
165167
const connectionDirective = field.directives.find(d => d.name === 'hasOne' || d.name === 'hasMany' || d.name === 'belongsTo');
166168

167169
if (connectionDirective) {
168170
switch (connectionDirective.name) {
169171
case 'hasOne':
170-
return processHasOneConnection(field, model, modelMap, connectionDirective, isCustomPKEnabled, shouldUseFieldsInAssociatedWithInHasOne);
172+
return processHasOneConnection(field, model, modelMap, connectionDirective, isCustomPKEnabled, shouldUseFieldsInAssociatedWithInHasOne, respectReferences);
171173
case 'belongsTo':
172-
return processBelongsToConnection(field, model, modelMap, connectionDirective, isCustomPKEnabled);
174+
return processBelongsToConnection(field, model, modelMap, connectionDirective, isCustomPKEnabled, respectReferences);
173175
case 'hasMany':
174-
return processHasManyConnection(field, model, modelMap, connectionDirective, shouldUseModelNameFieldInHasManyAndBelongsTo, isCustomPKEnabled);
176+
return processHasManyConnection(field, model, modelMap, connectionDirective, shouldUseModelNameFieldInHasManyAndBelongsTo, isCustomPKEnabled, respectReferences);
175177
default:
176178
break;
177179
}

packages/appsync-modelgen-plugin/src/utils/process-has-many.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export function processHasManyConnection(
1919
connectionDirective: CodeGenDirective,
2020
shouldUseModelNameFieldInHasManyAndBelongsTo: boolean,
2121
isCustomPKEnabled: boolean = false,
22+
respectReferences: boolean = false, // remove when enabled references for all targets
2223
): CodeGenFieldConnection | undefined {
2324
if (!field.isList) {
2425
throw new Error("A field with hasMany must be a list type");
@@ -31,9 +32,9 @@ export function processHasManyConnection(
3132
throw new Error(fieldsAndReferencesErrorMessage);
3233
}
3334

34-
if (references.length > 0) {
35+
if (respectReferences && references.length > 0) {
3536
// ensure there is a matching belongsTo field with references
36-
getConnectedFieldV2(field, model, otherSide, connectionDirective.name, shouldUseModelNameFieldInHasManyAndBelongsTo)
37+
getConnectedFieldV2(field, model, otherSide, connectionDirective.name, shouldUseModelNameFieldInHasManyAndBelongsTo, respectReferences)
3738
const associatedWithFields = references.map((reference: string) => otherSide.fields.find((field) => reference === field.name))
3839
return {
3940
kind: CodeGenConnectionType.HAS_MANY,
@@ -46,7 +47,7 @@ export function processHasManyConnection(
4647

4748
const otherSideFields = isCustomPKEnabled
4849
? getConnectedFieldsForHasMany(field, model, otherSide, shouldUseModelNameFieldInHasManyAndBelongsTo)
49-
: [getConnectedFieldV2(field, model, otherSide, connectionDirective.name, shouldUseModelNameFieldInHasManyAndBelongsTo)];
50+
: [getConnectedFieldV2(field, model, otherSide, connectionDirective.name, shouldUseModelNameFieldInHasManyAndBelongsTo, respectReferences)];
5051
const otherSideField = otherSideFields[0];
5152

5253
// if a type is connected using name, then graphql-connection-transformer adds a field to

packages/appsync-modelgen-plugin/src/utils/process-has-one.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export function processHasOneConnection(
1414
modelMap: CodeGenModelMap,
1515
connectionDirective: CodeGenDirective,
1616
isCustomPKEnabled: boolean = false,
17-
shouldUseFieldsInAssociatedWithInHasOne:boolean = false
17+
shouldUseFieldsInAssociatedWithInHasOne:boolean = false,
18+
respectReferences: boolean = false, // remove when enabled references for all targets
1819
): CodeGenFieldConnection | undefined {
1920
const otherSide = modelMap[field.type];
2021
// Find other side belongsTo field when in bi direction connection
@@ -31,9 +32,9 @@ export function processHasOneConnection(
3132
}
3233

3334
let associatedWithFields;
34-
if (references.length > 0) {
35+
if (respectReferences && references.length > 0) {
3536
// ensure there is a matching belongsTo field with references
36-
getConnectedFieldV2(field, model, otherSide, connectionDirective.name);
37+
getConnectedFieldV2(field, model, otherSide, connectionDirective.name, false, respectReferences);
3738
associatedWithFields = references.map((reference: string) => otherSide.fields.find((field) => reference === field.name))
3839
return {
3940
kind: CodeGenConnectionType.HAS_ONE,
@@ -45,7 +46,7 @@ export function processHasOneConnection(
4546
} else if (isCustomPKEnabled) {
4647
associatedWithFields = getConnectedFieldsForHasOne(otherSideBelongsToField, otherSide, shouldUseFieldsInAssociatedWithInHasOne);
4748
} else {
48-
const otherSideField = getConnectedFieldV2(field, model, otherSide, connectionDirective.name);
49+
const otherSideField = getConnectedFieldV2(field, model, otherSide, connectionDirective.name, false, respectReferences);
4950
associatedWithFields = [otherSideField];
5051
}
5152

packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,8 @@ export class AppSyncModelVisitor<
10871087
this.modelMap,
10881088
shouldUseModelNameFieldInHasManyAndBelongsTo,
10891089
isCustomPKEnabled,
1090-
shouldUseFieldsInAssociatedWithInHasOne
1090+
shouldUseFieldsInAssociatedWithInHasOne,
1091+
this.config.target === 'introspection',
10911092
);
10921093
if (connectionInfo) {
10931094
if (connectionInfo.kind === CodeGenConnectionType.HAS_MANY) {
@@ -1181,8 +1182,7 @@ export class AppSyncModelVisitor<
11811182
connectionInfo.kind !== CodeGenConnectionType.HAS_MANY &&
11821183
connectionInfo.kind !== CodeGenConnectionType.HAS_ONE &&
11831184
connectionInfo.targetNames &&
1184-
connectionInfo.targetName !== 'id' &&
1185-
!connectionInfo.isUsingReferences
1185+
connectionInfo.targetName !== 'id'
11861186
) {
11871187
// Need to remove the field that is targetName
11881188
connectionInfo.targetNames.forEach(targetName => removeFieldFromModel(model, targetName));

0 commit comments

Comments
 (0)