Skip to content
Open
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
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export * from './PAGINATE';
export * from './Field';
export * from './fragment-spread';
export * from './inline-fragment';
export * from './select-always';
export * from './slice';
1 change: 1 addition & 0 deletions src/constants/select-always.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const SELECT_ALWAYS = "perch:selectAlways";
1 change: 1 addition & 0 deletions src/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './select-always';
12 changes: 12 additions & 0 deletions src/decorators/select-always.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {SELECT_ALWAYS} from "../constants";

/**
* @description Force-selects columns, even if they are not in the GraphQL query. Handy if you use e.g. an `@AfterLoad()` that depends on a column.
*/
export function SelectAlways() {
return (target, property) => {
const metadata = Reflect.getMetadata(SELECT_ALWAYS, target) || [];
metadata.push(property);
Reflect.defineMetadata(SELECT_ALWAYS, metadata, target);
};
}
13 changes: 12 additions & 1 deletion src/functions/build-query-recursively.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {GraphQLQueryTree, PAGINATE} from "../";
import {GraphQLQueryTree, SELECT_ALWAYS} from "../";
import {RelationMetadata} from "typeorm/metadata/RelationMetadata";
import {EntityMetadata, SelectQueryBuilder} from "typeorm";

Expand Down Expand Up @@ -28,9 +28,20 @@ export function buildQueryRecursively<T>(
.keys(tree.properties.args)
.map((arg: string) => alias + "." + arg);

// Thirdly, we check the special selectAlways decorator data and force select those columns
let selectAlwaysFields = [];
if (typeof metadata.target == "function") {
const configuredFields = Reflect.getMetadata(SELECT_ALWAYS, metadata.target.prototype) || [];
selectAlwaysFields = configuredFields
// only select fields that are actually columns
.filter((propertyName) => Object.keys(metadata.propertiesMap).includes(propertyName))
.map((propertyName) => alias + "." + propertyName)
}

// We select all of above
qb.addSelect(argFields);
qb.addSelect(selectedFields);
qb.addSelect(selectAlwaysFields);

// We add order options
Object.keys(options.order)
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './arguments';
export * from './types';
export * from './constants';
export * from './classes';
export * from './decorators';
export * from './functions';
export * from './interfaces';
export * from './object-types';