Skip to content

Commit

Permalink
Return a Set, not array, to prevent duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
sgarner committed Aug 20, 2020
1 parent f391aea commit 7495998
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/RelationMapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('RelationMapper', () => {
const resolveInfoHook = (info: GraphQLResolveInfo): void => {
const relations = new RelationMapper(connection).buildRelationListForQuery(Product, info);

expect(relations).toEqual(['owner', 'store']);
expect([...relations]).toEqual(['owner', 'store']);
};
const result = await graphql(executableSchema, query, {}, { resolveInfoHook });

Expand Down Expand Up @@ -115,7 +115,7 @@ describe('RelationMapper', () => {
const resolveInfoHook = (info: GraphQLResolveInfo): void => {
const relations = new RelationMapper(connection).buildRelationListForQuery(Product, info);

expect(relations).toEqual(['owner', 'store', 'store.owner']);
expect([...relations]).toEqual(['owner', 'store', 'store.owner']);
};
const result = await graphql(executableSchema, query, {}, { resolveInfoHook });

Expand Down Expand Up @@ -184,7 +184,7 @@ describe('RelationMapper', () => {
const resolveInfoHook = (info: GraphQLResolveInfo): void => {
const relations = new RelationMapper(connection).buildRelationListForQuery(Product, info);

expect(relations).toEqual(['owner', 'store', 'store.owner']);
expect([...relations]).toEqual(['owner', 'store', 'store.owner']);
};
const result = await graphql(executableSchema, query, {}, { resolveInfoHook });

Expand Down Expand Up @@ -254,7 +254,7 @@ describe('RelationMapper', () => {
const resolveInfoHook = (info: GraphQLResolveInfo): void => {
const relations = new RelationMapper(connection).buildRelationListForQuery(Product, info);

expect(relations).toEqual([]);
expect([...relations]).toEqual([]);
};
const result = await graphql(executableSchema, query, {}, { resolveInfoHook });

Expand Down
13 changes: 8 additions & 5 deletions src/RelationMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ export class RelationMapper {
* Build the list of matching TypeORM relation property names for an entity, based on the `info` given to a GraphQL
* query resolver.
*/
public buildRelationListForQuery(entity: Function | EntitySchema<any> | string, info: GraphQLResolveInfo): string[] {
public buildRelationListForQuery(
entity: Function | EntitySchema<any> | string,
info: GraphQLResolveInfo,
): Set<string> {
const baseNode = info.fieldNodes.find(fieldNode => fieldNode.name.value === info.fieldName);

if (baseNode == null) {
Expand All @@ -27,8 +30,8 @@ export class RelationMapper {
baseNode: SelectionNode,
fragments?: { [p: string]: FragmentDefinitionNode },
basePropertyPath?: string,
): string[] {
const relationNames: string[] = [];
): Set<string> {
const relationNames = new Set<string>();
const selectionSet = this.getSelectionSetFromNode(baseNode, fragments);

if (selectionSet == null) {
Expand All @@ -51,7 +54,7 @@ export class RelationMapper {
: relationMetadata.propertyName;

// add the relation to the list
relationNames.push(currentPropertyPath);
relationNames.add(currentPropertyPath);
}

/*
Expand All @@ -66,7 +69,7 @@ export class RelationMapper {
fragments,
currentPropertyPath,
);
relationNames.push(...nestedRelations);
nestedRelations.forEach(nestedRelation => relationNames.add(nestedRelation));
});

return relationNames;
Expand Down
6 changes: 3 additions & 3 deletions test/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const resolvers: IResolvers<any, TestResolverContext> = {
const productRelations = new RelationMapper(connection).buildRelationListForQuery(Product, info);

return connection.getRepository(Product).find({
relations: productRelations,
relations: [...productRelations],
});
},
},
Expand Down Expand Up @@ -105,13 +105,13 @@ export const resolvers: IResolvers<any, TestResolverContext> = {
where: {
product: source,
},
relations: imageRelations,
relations: [...imageRelations],
});
const videos = await connection.getRepository(Video).find({
where: {
product: source,
},
relations: videoRelations,
relations: [...videoRelations],
});

return [...images, ...videos];
Expand Down

0 comments on commit 7495998

Please sign in to comment.