Skip to content

Commit

Permalink
fix: validate references on belongsTo when cpk is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilch committed Apr 25, 2024
1 parent b668fd2 commit 1207492
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ describe('custom references', () => {
}
`;

const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema);
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: true });
expect(visitor.generate()).toMatchSnapshot();
});

Expand All @@ -543,7 +543,7 @@ describe('custom references', () => {
}
`;

const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema);
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: true });
expect(visitor.generate()).toMatchSnapshot();
});

Expand All @@ -567,7 +567,7 @@ describe('custom references', () => {
primary: Primary @belongsTo(references: ["primaryId"])
}
`;
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema);
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: true });
expect(visitor.generate()).toMatchSnapshot();
});

Expand All @@ -588,7 +588,7 @@ describe('custom references', () => {
}
`;

const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema);
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: true });
expect(visitor.generate()).toMatchSnapshot();
});

Expand All @@ -611,7 +611,7 @@ describe('custom references', () => {
}
`;

const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema);
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: true });
expect(visitor.generate()).toMatchSnapshot();
});

Expand All @@ -633,7 +633,7 @@ describe('custom references', () => {
primary: Primary @belongsTo(references: ["primaryTenantId", "primaryInstanceId", "primaryRecordId"])
}
`;
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema);
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: true });
expect(visitor.generate()).toMatchSnapshot();
});

Expand All @@ -653,7 +653,7 @@ describe('custom references', () => {
}
`;

const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema);
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: true });
expect(() => visitor.generate())
.toThrowError(`'fields' and 'references' cannot be used together.`);
});
Expand All @@ -674,7 +674,7 @@ describe('custom references', () => {
}
`;

const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema);
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: true });
expect(() => visitor.generate())
.toThrowError(`'fields' and 'references' cannot be used together.`);
});
Expand All @@ -695,7 +695,7 @@ describe('custom references', () => {
}
`;

const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema);
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: true });
expect(() => visitor.generate())
.toThrowError(`'fields' and 'references' cannot be used together.`);
});
Expand All @@ -716,7 +716,7 @@ describe('custom references', () => {
}
`;

const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema);
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: true });
expect(() => visitor.generate())
.toThrowError(`Error processing @hasOne directive on SqlPrimary.related. @belongsTo directive with references ["primaryId"] was not found in connected model SqlRelated`);
});
Expand All @@ -737,11 +737,33 @@ describe('custom references', () => {
}
`;

const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema);
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: true });
expect(() => visitor.generate())
.toThrowError(`Error processing @belongsTo directive on SqlRelated.primary. @hasOne or @hasMany directive with references ["primaryId"] was not found in connected model SqlPrimary`);
});

test('throws error when missing references on hasMany related model when custom pk is disabled', () => {
const schema = /* GraphQL */ `
type SqlPrimary @refersTo(name: "sql_primary") @model {
id: Int! @primaryKey
content: String
related: [SqlRelated] @hasMany(references: ["primaryId"])
}
type SqlRelated @refersTo(name: "sql_related") @model {
id: Int! @primaryKey
content: String
primaryId: Int! @refersTo(name: "primary_id") @index(name: "primary_id")
primary: SqlPrimary @belongsTo
}
`;

const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: false });
expect(() => visitor.generate())
.toThrowError(`Error processing @hasMany directive on SqlPrimary.related. @belongsTo directive with references ["primaryId"] was not found in connected model SqlRelated`);
});


test('throws error when missing references on hasMany related model', () => {
const schema = /* GraphQL */ `
type SqlPrimary @refersTo(name: "sql_primary") @model {
Expand All @@ -758,7 +780,7 @@ describe('custom references', () => {
}
`;

const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema);
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: true });
expect(() => visitor.generate())
.toThrowError(`Error processing @hasMany directive on SqlPrimary.related. @belongsTo directive with references ["primaryId"] was not found in connected model SqlRelated`);
});
Expand All @@ -779,7 +801,7 @@ describe('custom references', () => {
}
`;

const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema);
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, { respectPrimaryKeyAttributesOnConnectionField: true });
expect(() => visitor.generate())
.toThrowError(`Error processing @belongsTo directive on SqlRelated.primary. @hasOne or @hasMany directive with references ["primaryId"] was not found in connected model SqlPrimary`);
});
Expand Down
10 changes: 7 additions & 3 deletions packages/appsync-modelgen-plugin/src/utils/process-belongs-to.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ export function processBelongsToConnection(
`A 'belongsTo' field should match to a corresponding 'hasMany' or 'hasOne' field`
);
}
const references = connectionDirective.arguments.references || [];
const isUsingReferences = references.length > 0;
if (isUsingReferences) {
// ensure there is a matching hasOne/hasMany field with references
getConnectedFieldV2(field, model, otherSide, connectionDirective.name)
}

const otherSideField = isCustomPKEnabled ? otherSideConnectedFields[0] : getConnectedFieldV2(field, model, otherSide, connectionDirective.name);
const connectionFields = connectionDirective.arguments.fields || [];

const references = connectionDirective.arguments.references || [];

if (connectionFields.length > 0 && references.length > 0) {
throw new Error(fieldsAndReferencesErrorMessage);
}
Expand All @@ -41,7 +46,6 @@ export function processBelongsToConnection(
// but if the field are connected using fields argument in connection directive
// we are reusing the field and it should be preserved in selection set
const otherSideHasMany = otherSideField.isList;
const isUsingReferences = references.length > 0;
// New metada type introduced by custom PK v2 support
let targetNames: string[] = [ ...connectionFields, ...references ];
if (targetNames.length === 0) {
Expand Down

0 comments on commit 1207492

Please sign in to comment.