Skip to content

Commit d717a86

Browse files
committed
fix: fix validate() throwing if cascadeFields included a non-object-type relation field
1 parent e0a6895 commit d717a86

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

spec/schema/ast-validation-modules/ttl.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import {
22
assertValidatorAcceptsAndDoesNotWarn,
33
assertValidatorRejects,
44
assertValidatorWarns,
5+
validate,
56
} from './helpers';
67
import gql from 'graphql-tag';
8+
import { expect } from 'chai';
79

810
describe('timeToLive config', () => {
911
it('accepts simple case', () => {
@@ -445,4 +447,28 @@ describe('timeToLive config', () => {
445447
},
446448
);
447449
});
450+
451+
it('does not throw if cascadeFields are referencing relations that are non-object types', () => {
452+
// previously, we had a bug where this threw instead of reporting an error
453+
// it can quickly happen if there is a syntax error in the file that defines the relation type
454+
assertValidatorRejects(
455+
gql`
456+
type Test @rootEntity {
457+
finishedAt: DateTime
458+
nested: String @relation
459+
}
460+
`,
461+
'Type "String" cannot be used with @relation because it is not a root entity type.',
462+
{
463+
timeToLive: [
464+
{
465+
typeName: 'Test',
466+
dateField: 'finishedAt',
467+
expireAfterDays: 3,
468+
cascadeFields: ['nested'],
469+
},
470+
],
471+
},
472+
);
473+
});
448474
});

src/model/implementation/time-to-live.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ export class TimeToLiveType implements ModelComponent {
133133

134134
// only allow cascade on forward relations. See Field#validateRelation() for details.
135135
if (!field.type.isObjectType) {
136-
throw new Error(`Expected ${field.type.name} to be an object type`);
136+
// isRelation = true if the type is not an object type is already an error, so we
137+
// don't need to report an additional error
138+
return;
137139
}
138140
const inverseField = field.type.fields.find((f) => f.inverseOf === field);
139141
if (!inverseField) {

0 commit comments

Comments
 (0)