Skip to content

Commit

Permalink
fix(core): Fix loading multiple customField relations
Browse files Browse the repository at this point in the history
fixes #2555
  • Loading branch information
jnugh committed Dec 4, 2023
1 parent b5a265f commit 55b5f35
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 6 deletions.
45 changes: 42 additions & 3 deletions packages/core/e2e/fixtures/test-plugins/list-query-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,25 @@ export class CustomFieldRelationTestEntity extends VendureEntity {
parent: Relation<TestEntity>;
}

@Entity()
export class CustomFieldOtherRelationTestEntity extends VendureEntity {
constructor(input: Partial<CustomFieldOtherRelationTestEntity>) {
super(input);
}

@Column()
data: string;

@ManyToOne(() => TestEntity)
parent: Relation<TestEntity>;
}

class TestEntityCustomFields {
@OneToMany(() => CustomFieldRelationTestEntity, child => child.parent)
relation: Relation<CustomFieldRelationTestEntity[]>;

@OneToMany(() => CustomFieldOtherRelationTestEntity, child => child.parent)
otherRelation: Relation<CustomFieldOtherRelationTestEntity[]>;
}

@Entity()
Expand Down Expand Up @@ -143,7 +159,7 @@ export class TestEntityTranslation extends VendureEntity implements Translation<
@ManyToOne(type => TestEntity, base => base.translations)
base: TestEntity;

customFields: {};
customFields: never;
}

@Entity()
Expand Down Expand Up @@ -171,7 +187,12 @@ export class ListQueryResolver {
return this.listQueryBuilder
.build(TestEntity, args.options, {
ctx,
relations: ['orderRelation', 'orderRelation.customer', 'customFields.relation'],
relations: [
'orderRelation',
'orderRelation.customer',
'customFields.relation',
'customFields.otherRelation',
],
customPropertyMap: {
customerLastName: 'orderRelation.customer.lastName',
},
Expand Down Expand Up @@ -222,8 +243,14 @@ const apiExtensions = gql`
data: String!
}
type CustomFieldOtherRelationTestEntity implements Node {
id: ID!
data: String!
}
type TestEntityCustomFields {
relation: [CustomFieldRelationTestEntity!]!
otherRelation: [CustomFieldOtherRelationTestEntity!]!
}
type TestEntity implements Node {
Expand Down Expand Up @@ -272,7 +299,13 @@ const apiExtensions = gql`

@VendurePlugin({
imports: [PluginCommonModule],
entities: [TestEntity, TestEntityPrice, TestEntityTranslation, CustomFieldRelationTestEntity],
entities: [
TestEntity,
TestEntityPrice,
TestEntityTranslation,
CustomFieldRelationTestEntity,
CustomFieldOtherRelationTestEntity,
],
adminApiExtensions: {
schema: apiExtensions,
resolvers: [ListQueryResolver],
Expand Down Expand Up @@ -409,6 +442,12 @@ export class ListQueryPlugin implements OnApplicationBootstrap {
data: nestedContent.data,
}),
);
await this.connection.getRepository(CustomFieldOtherRelationTestEntity).save(
new CustomFieldOtherRelationTestEntity({
parent: testEntity,
data: nestedContent.data,
}),
);
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions packages/core/e2e/list-query-builder.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,27 @@ describe('ListQueryBuilder', () => {
},
]);
});

it('should resolve multiple relations in customFields successfully', async () => {
const { testEntities } = await shopClient.query(GET_LIST_WITH_MULTIPLE_CUSTOM_FIELD_RELATION, {
options: {
filter: {
label: { eq: 'A' },
},
},
});

expect(testEntities.items).toEqual([
{
id: 'T_1',
label: 'A',
customFields: {
relation: [{ id: 'T_1', data: 'A' }],
otherRelation: [{ id: 'T_1', data: 'A' }],
},
},
]);
});
});
});

Expand Down Expand Up @@ -1351,3 +1372,24 @@ const GET_LIST_WITH_CUSTOM_FIELD_RELATION = gql`
}
}
`;

const GET_LIST_WITH_MULTIPLE_CUSTOM_FIELD_RELATION = gql`
query GetTestWithMultipleCustomFieldRelation($options: TestEntityListOptions) {
testEntities(options: $options) {
items {
id
label
customFields {
relation {
id
data
}
otherRelation {
id
data
}
}
}
}
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -493,14 +493,21 @@ export class ListQueryBuilder implements OnApplicationBootstrap {
loadEagerRelations: true,
} as FindManyOptions<T>)
.then(results =>
results.map(r => ({ relation: relationPaths[0] as keyof T, entity: r })),
results.map(r => ({
relations: relationPaths[0].startsWith('customFields.')
? relationPaths
: [relationPaths[0]],
entity: r,
})),
);
}),
).then(all => all.flat());
for (const entry of entitiesIdsWithRelations) {
const finalEntity = entityMap.get(entry.entity.id);
if (finalEntity) {
this.assignDeep(entry.relation, entry.entity, finalEntity);
for (const relation of entry.relations) {
if (finalEntity) {
this.assignDeep(relation, entry.entity, finalEntity);
}
}
}
return Array.from(entityMap.values());
Expand Down

0 comments on commit 55b5f35

Please sign in to comment.