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 20f83b1 commit 6ecb5bf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
flattenFieldDirectives,
makeConnectionAttributeName,
} from './process-connections';
import { getConnectedFieldV2 } from './process-connections-v2';
import { getConnectedFieldV2, getConnectedFieldForReferences } from './process-connections-v2';


export function processBelongsToConnection(
Expand All @@ -32,7 +32,7 @@ export function processBelongsToConnection(
const isUsingReferences = references.length > 0;
if (isUsingReferences) {
// ensure there is a matching hasOne/hasMany field with references
getConnectedFieldV2(field, model, otherSide, connectionDirective.name)
getConnectedFieldForReferences(field, model, otherSide, connectionDirective.name)
}

const otherSideField = isCustomPKEnabled ? otherSideConnectedFields[0] : getConnectedFieldV2(field, model, otherSide, connectionDirective.name);
Expand Down

0 comments on commit 6ecb5bf

Please sign in to comment.