Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix validate() throwing if cascadeFields included a non-object-t… #341

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading