Skip to content

Commit

Permalink
feat: Column.excludeFieldFromQuery, exclude field but keep fields arr…
Browse files Browse the repository at this point in the history
…ay (#1217)

* Pass in undefined (instead of null) so TypeScript uses the default value in the parameter signature

* Implement excludeFieldFromQuery flag

---------

Co-authored-by: Ronald Van Ryswyk <[email protected]>
  • Loading branch information
Harsgalt86 and Ronald Van Ryswyk committed Nov 25, 2023
1 parent 2ff444b commit 85cc514
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/common/src/interfaces/column.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ export interface Column<T = any> {
/** Default to false, which leads to exclude the column title from the Grid Menu. */
excludeFromGridMenu?: boolean;

/** Defaults to false, which leads to exclude the field from the query (typically a backend service query) */
/** Defaults to false, which leads to exclude the `field` property, but still includes the `fields` property, from the query (typically a backend service query) */
excludeFieldFromQuery?: boolean;

/** Defaults to false, which leads to exclude the `field` (and `fields`) from the query (typically a backend service query) */
excludeFromQuery?: boolean;

/** Defaults to false, which leads to exclude the column from getting a header menu. For example, the checkbox row selection should not have a header menu. */
Expand Down
14 changes: 14 additions & 0 deletions packages/graphql/src/services/__tests__/graphql.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,20 @@ describe('GraphqlService', () => {
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
});

it('should exclude a column field, and expect a query string without it, but still include any fields specified', () => {
const expectation = `query{ users(first:10, offset:0){ totalCount, nodes{ id, field1, field3, field4, field5 }}}`;
const columns = [
{ id: 'field1', field: 'field1', width: 100 },
{ id: 'field2', field: 'field2', fields: ['field3', 'field4', 'field5'], width: 100, excludeFieldFromQuery: true }
];
jest.spyOn(gridStub, 'getColumns').mockReturnValue(columns);

service.init({ datasetName: 'users' }, paginationOptions, gridStub);
const query = service.buildQuery();

expect(removeSpaces(query)).toBe(removeSpaces(expectation));
});

it('should use default pagination "first" option when "paginationOptions" is not provided', () => {
const expectation = `query{ users(first:${DEFAULT_ITEMS_PER_PAGE}, offset:0){ totalCount, nodes{ id, field1 }}}`;
const columns = [{ id: 'field1', field: 'field1', width: 100 }, { id: 'field2', field: 'field2', width: 100, excludeFromQuery: true }];
Expand Down
4 changes: 3 additions & 1 deletion packages/graphql/src/services/graphql.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ export class GraphqlService implements BackendService {
const columnIds: string[] = [];
if (columnDefinitions && Array.isArray(columnDefinitions)) {
for (const column of columnDefinitions) {
columnIds.push(column.field);
if (!column.excludeFieldFromQuery) {
columnIds.push(column.field);
}

// when extra "fields" are provided, also push them to columnIds
if (column.fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ describe('GridOdataService', () => {

it('should use default pagination "$top" option when "paginationOptions" is not provided', () => {
const expectation = `$top=${DEFAULT_ITEMS_PER_PAGE}`;
const columns = [{ id: 'field1', field: 'field1', width: 100 }, { id: 'field2', field: 'field2', width: 100, excludeFromQuery: true }];
const columns = [
{ id: 'field1', field: 'field1', width: 100 },
{ id: 'field2', field: 'field2', width: 100, excludeFromQuery: true },
{ id: 'field3', field: 'field3', fields: ['field4', 'field5', 'field6'], width: 100, excludeFieldFromQuery: true }
];
jest.spyOn(gridStub, 'getColumns').mockReturnValue(columns);

service.init(null as any, undefined, gridStub);
Expand Down

0 comments on commit 85cc514

Please sign in to comment.