Skip to content

Commit

Permalink
fixup types
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Jul 26, 2024
1 parent 3b8848a commit 96b06a4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
2 changes: 2 additions & 0 deletions packages/graph/src/-private/-edge-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ export function upgradeDefinition(
// TODO probably dont need this assertion if polymorphic
assert(`Expected the inverse model to exist`, getStore(storeWrapper).modelFor(inverseType));
inverseDefinition = null;
} else if (definition.isCollection && !definition.inverseKey) {
inverseDefinition = null;
} else {
inverseKey = /*#__NOINLINE__*/ inverseForRelationship(getStore(storeWrapper), identifier, propertyName);

Expand Down
40 changes: 27 additions & 13 deletions packages/graph/src/-private/debug/assert-polymorphic-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,18 @@ if (DEBUG) {
if (definition.inverseKind !== meta.kind) {
errors.set('type', ` <---- should be '${definition.inverseKind}'`);
}
if (definition.inverseIsAsync !== meta.options.async) {
errors.set('async', ` <---- should be ${definition.inverseIsAsync}`);
if (definition.kind !== 'collection') {
if (definition.inverseIsAsync !== meta.options?.async) {
errors.set('async', ` <---- should be ${definition.inverseIsAsync}`);
}
}
if (definition.inverseIsPolymorphic && definition.inverseIsPolymorphic !== meta.options.polymorphic) {
if (definition.inverseIsPolymorphic && definition.inverseIsPolymorphic !== meta.options?.polymorphic) {
errors.set('polymorphic', ` <---- should be ${definition.inverseIsPolymorphic}`);
}
if (definition.key !== meta.options.inverse) {
if (definition.key !== meta.options?.inverse) {
errors.set('inverse', ` <---- should be '${definition.key}'`);
}
if (definition.type !== meta.options.as) {
if (definition.type !== meta.options?.as) {
errors.set('as', ` <---- should be '${definition.type}'`);
}

Expand All @@ -59,11 +61,11 @@ if (DEBUG) {
name: string;
type: string;
kind: string;
options: {
options?: {
as?: string;
async: boolean;
async?: boolean;
polymorphic?: boolean;
inverse: string | null;
inverse?: string | null;
};
};
type RelationshipSchemaError = 'name' | 'type' | 'kind' | 'as' | 'async' | 'polymorphic' | 'inverse';
Expand All @@ -83,6 +85,19 @@ if (DEBUG) {
}

function printSchema(config: PrintConfig, errors?: Map<RelationshipSchemaError, string>) {
const optionLines = [
config.options?.as ? ` as: '${config.options.as}',${errors?.get('as') || ''}` : '',
config.kind !== 'collection' ? ` async: ${config.options?.async},${errors?.get('async') || ''}` : '',
config.options?.polymorphic || errors?.get('polymorphic')
? ` polymorphic: ${config.options?.polymorphic},${errors?.get('polymorphic') || ''}`
: '',
config.kind !== 'collection' || errors?.get('inverse')
? ` inverse: '${config.options?.inverse}'${errors?.get('inverse') || ''}`
: '',
]
.filter(Boolean)
.join('\n');

return `
\`\`\`
Expand All @@ -92,10 +107,7 @@ if (DEBUG) {
type: '${config.type}',${errors?.get('type') || ''}
kind: '${config.kind}',${errors?.get('kind') || ''}
options: {
as: '${config.options.as}',${errors?.get('as') || ''}
async: ${config.options.async},${errors?.get('async') || ''}
polymorphic: ${config.options.polymorphic},${errors?.get('polymorphic') || ''}
inverse: '${config.options.inverse}'${errors?.get('inverse') || ''}
${optionLines}
}
}
}
Expand Down Expand Up @@ -137,6 +149,7 @@ if (DEBUG) {
isAsync: definition.inverseIsAsync,
isPolymorphic: true,
isCollection: definition.inverseIsCollection,
isPaginated: definition.inverseIsPaginated,
isImplicit: definition.inverseIsImplicit,
inverseKey: definition.key,
inverseType: definition.type,
Expand All @@ -145,6 +158,7 @@ if (DEBUG) {
inverseIsPolymorphic: definition.isPolymorphic,
inverseIsImplicit: definition.isImplicit,
inverseIsCollection: definition.isCollection,
inverseIsPaginated: definition.isPaginated,
resetOnRemoteUpdate: definition.resetOnRemoteUpdate,
};
}
Expand Down Expand Up @@ -230,7 +244,7 @@ if (DEBUG) {
meta = isLegacyField(meta) ? meta : temporaryConvertToLegacy(meta);
assert(
`You should not specify both options.as and options.inverse as null on ${addedIdentifier.type}.${parentDefinition.inverseKey}, as if there is no inverse field there is no abstract type to conform to. You may have intended for this relationship to be polymorphic, or you may have mistakenly set inverse to null.`,
!(meta.options.inverse === null && meta?.options.as?.length)
!(meta.options?.inverse === null && meta.options.as?.length)
);
const errors = validateSchema(parentDefinition, meta);
assert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ function ensureRelationshipIsSetToParent(
if (inverse) {
const { inverseKey, kind } = inverse;

if (kind === 'collection') {
return;
}

const relationshipData = relationships[inverseKey]?.data as RelationshipData | undefined;

if (DEBUG) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ if (DEBUG) {
);
assert(
`The schema for the relationship '${parentDefinition.inverseKey}' on '${addedIdentifier.type}' type does not implement '${parentDefinition.type}' and thus cannot be assigned to the '${parentDefinition.key}' relationship in '${parentIdentifier.type}'. The definition should specify 'as: "${parentDefinition.type}"' in options.`,
meta?.options.as === parentDefinition.type
meta?.options?.as === parentDefinition.type
);
}
};
Expand Down
1 change: 1 addition & 0 deletions packages/store/src/-types/q/ds-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { TypedRecordInstance, TypeFromInstance } from '@warp-drive/core-typ
import type { LegacyAttributeField, LegacyRelationshipSchema } from '@warp-drive/core-types/schema/fields';

export type KeyOrString<T> = keyof T & string extends never ? string : keyof T & string;
export type FieldKind = 'attribute' | 'belongsTo' | 'hasMany' | 'collection';

/**
* Minimum subset of static schema methods and properties on the
Expand Down

0 comments on commit 96b06a4

Please sign in to comment.