Skip to content

Commit d69527e

Browse files
committed
Fix: Select issue
- Add metadata to resolve select issue
1 parent 7f6cefc commit d69527e

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/common/graphql/customExtended.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class ExtendedRepository<T = unknown> extends Repository<T> {
6969
}
7070

7171
const queryCondition = gqlQuery
72-
? getConditionFromGqlQuery<T>(gqlQuery, true)
72+
? getConditionFromGqlQuery.call(this, gqlQuery, true)
7373
: { relations: undefined, select: undefined };
7474

7575
const condition: FindManyOptions<T> = {
@@ -101,7 +101,7 @@ export class ExtendedRepository<T = unknown> extends Repository<T> {
101101
gqlQuery?: string,
102102
): Promise<T> {
103103
const queryCondition = gqlQuery
104-
? getConditionFromGqlQuery<T>(gqlQuery)
104+
? getConditionFromGqlQuery.call(this, gqlQuery)
105105
: { relations: undefined, select: undefined };
106106

107107
const condition: FindOneOptions<T> = {

src/common/graphql/utils/getConditionFromGqlQuery.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { parse, print } from 'graphql';
22
import { set } from 'lodash';
3+
import { Repository } from 'typeorm';
34

45
import { AddKeyValueInObjectProps, GetInfoFromQueryProps } from './types';
56

@@ -32,10 +33,11 @@ const addKeyValuesInObject = <Entity>({
3233
return { relations, select };
3334
};
3435

35-
export const getConditionFromGqlQuery = <Entity>(
36+
export function getConditionFromGqlQuery<Entity>(
37+
this: Repository<Entity>,
3638
query: string,
3739
hasCountType?: boolean,
38-
): GetInfoFromQueryProps<Entity> => {
40+
): GetInfoFromQueryProps<Entity> {
3941
const ast = parse(query);
4042
const operationJson = print(ast);
4143

@@ -52,12 +54,21 @@ export const getConditionFromGqlQuery = <Entity>(
5254
const stack = [];
5355

5456
const regex = /[\s\{]/g;
57+
let lastMetadata = this.metadata;
5558

5659
return splitted.reduce(
5760
(acc, line) => {
5861
const replacedLine = line.replace(regex, '');
62+
5963
if (line.includes('{')) {
6064
stack.push(replacedLine);
65+
const isFirstLineDataType = hasCountType && replacedLine === DATA;
66+
67+
if (!isFirstLineDataType) {
68+
lastMetadata = lastMetadata.relations.find(
69+
(v) => v.propertyName === replacedLine,
70+
).inverseEntityMetadata;
71+
}
6172

6273
return addKeyValuesInObject({
6374
stack,
@@ -67,13 +78,33 @@ export const getConditionFromGqlQuery = <Entity>(
6778
hasCountType,
6879
});
6980
} else if (line.includes('}')) {
81+
const hasDataTypeInStack =
82+
hasCountType && stack.length && stack[0] === DATA;
83+
84+
lastMetadata =
85+
stack.length < (hasDataTypeInStack ? 3 : 2)
86+
? this.metadata
87+
: lastMetadata.relations.find(
88+
(v) => v.propertyName === stack[stack.length - 2],
89+
).inverseEntityMetadata;
90+
7091
stack.pop();
7192

7293
return acc;
7394
}
7495

96+
const addedStack = [...stack, replacedLine];
97+
98+
if (
99+
![...lastMetadata.columns, ...lastMetadata.relations]
100+
.map((v) => v.propertyName)
101+
.includes(addedStack[addedStack.length - 1])
102+
) {
103+
return acc;
104+
}
105+
75106
return addKeyValuesInObject({
76-
stack: [...stack, replacedLine],
107+
stack: addedStack,
77108
relations: acc.relations,
78109
select: acc.select,
79110
hasCountType,
@@ -84,4 +115,4 @@ export const getConditionFromGqlQuery = <Entity>(
84115
select: {},
85116
},
86117
);
87-
};
118+
}

0 commit comments

Comments
 (0)