Skip to content
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
45 changes: 45 additions & 0 deletions packages/core/e2e/custom-field-relations.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,51 @@ describe('Custom field relations', () => {

assertCustomFieldIds(updateCustomerAddress.customFields, 'T_3', ['T_2', 'T_4']);
});

// Regression: an OrderAddress (Order.shippingAddress / billingAddress) is an embedded
// value object with no `id`, so its relation custom fields cannot be resolved by a parent
// entity id. Previously this threw "The loader.load() function must be called with a
// value, but got: undefined"; it must now resolve to null / [] instead of crashing.
it('order shippingAddress resolves relation custom fields without throwing', async () => {
await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
await shopClient.query(gql`
mutation {
addItemToOrder(productVariantId: "T_1", quantity: 1) {
... on Order {
id
}
}
}
`);
await shopClient.query(gql`
mutation {
setOrderShippingAddress(
input: {
countryCode: "GB"
streetLine1: "Test Street"
customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
}
) {
... on Order {
id
}
}
}
`);

const { activeOrder } = await shopClient.query(gql`
query {
activeOrder {
shippingAddress {
${customFieldsSelection}
}
}
}
`);

expect(activeOrder.shippingAddress.customFields.single).toBeNull();
expect(activeOrder.shippingAddress.customFields.multi).toEqual([]);
});
});

describe('Collection entity', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { RequestContext } from './request-context';

export interface ResolveRelationConfig {
ctx: RequestContext;
entityId: ID;
entityId: ID | undefined;
entityName: string;
fieldDef: RelationCustomFieldConfig;
}
Expand All @@ -40,6 +40,15 @@ export class CustomFieldRelationResolverService {
async resolveRelation(config: ResolveRelationConfig): Promise<VendureEntity | VendureEntity[] | null> {
const { ctx, entityId, entityName, fieldDef } = config;

// Embedded value objects such as OrderAddress (Order.shippingAddress / billingAddress) have
// no `id`, so `entityId` can be undefined. Passing it to DataLoader.load() would throw
// ("The loader.load() function must be called with a value, but got: undefined"); there is
// nothing to resolve by id, so return an empty result, matching the behaviour before
// relation custom fields were resolved via DataLoader.
if (entityId == null) {
return fieldDef.list ? [] : null;
}

const loader = this.getLoader(ctx, entityName, fieldDef);
const batchResult = await loader.load(entityId);
const finalResult = fieldDef.list ? batchResult : (batchResult[0] ?? null);
Expand Down
Loading