Skip to content

Commit

Permalink
fix: fix validate() throwing if cascadeFields included a non-object-t…
Browse files Browse the repository at this point in the history
…ype relation field
  • Loading branch information
Yogu committed Sep 16, 2024
1 parent e0a6895 commit d717a86
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
26 changes: 26 additions & 0 deletions spec/schema/ast-validation-modules/ttl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import {
assertValidatorAcceptsAndDoesNotWarn,
assertValidatorRejects,
assertValidatorWarns,
validate,
} from './helpers';
import gql from 'graphql-tag';
import { expect } from 'chai';

describe('timeToLive config', () => {
it('accepts simple case', () => {
Expand Down Expand Up @@ -445,4 +447,28 @@ describe('timeToLive config', () => {
},
);
});

it('does not throw if cascadeFields are referencing relations that are non-object types', () => {
// previously, we had a bug where this threw instead of reporting an error
// it can quickly happen if there is a syntax error in the file that defines the relation type
assertValidatorRejects(
gql`
type Test @rootEntity {
finishedAt: DateTime
nested: String @relation
}
`,
'Type "String" cannot be used with @relation because it is not a root entity type.',
{
timeToLive: [
{
typeName: 'Test',
dateField: 'finishedAt',
expireAfterDays: 3,
cascadeFields: ['nested'],
},
],
},
);
});
});
4 changes: 3 additions & 1 deletion src/model/implementation/time-to-live.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ export class TimeToLiveType implements ModelComponent {

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

0 comments on commit d717a86

Please sign in to comment.