From 67b7baf7703625dcb4a49e870ae72ed0958d8423 Mon Sep 17 00:00:00 2001 From: Max Ehrlicher-Schmidt Date: Thu, 12 Nov 2020 19:11:30 +0100 Subject: [PATCH] Add dynamic readonly columns --- .../isPartOfGraphQLFunction.ts | 28 +++++++++++++++++++ src/app/pages/tables/bikes/bikes.component.ts | 11 +++----- 2 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 src/app/helperFunctions/isPartOfGraphQLFunction.ts diff --git a/src/app/helperFunctions/isPartOfGraphQLFunction.ts b/src/app/helperFunctions/isPartOfGraphQLFunction.ts new file mode 100644 index 0000000..28d216f --- /dev/null +++ b/src/app/helperFunctions/isPartOfGraphQLFunction.ts @@ -0,0 +1,28 @@ +import { DocumentNode } from 'graphql'; + +export function isPartOfGraphQLDoc( + variableName: String, + doc: DocumentNode +): boolean { + return isPartOfSelectionSet(variableName, doc.definitions[0]); +} + +function isPartOfSelectionSet( + variableName: String, + selectionObject: any +): boolean { + const variablePath = variableName.split('.'); + const selections = selectionObject.selectionSet?.selections; + if (selections !== undefined) { + const nextSelectionObject = selections.find(selection => selection.name.value === variablePath[0]); + if (nextSelectionObject !== undefined) { + if (variablePath.length === 1) { + return true; + } + return isPartOfSelectionSet(variablePath.slice(1).join(), nextSelectionObject); + } else { + return false; + } + } + return false; +} diff --git a/src/app/pages/tables/bikes/bikes.component.ts b/src/app/pages/tables/bikes/bikes.component.ts index ba6f80c..ee1afa4 100644 --- a/src/app/pages/tables/bikes/bikes.component.ts +++ b/src/app/pages/tables/bikes/bikes.component.ts @@ -4,7 +4,8 @@ import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop'; import { BikesService, CargoBikeResult } from 'src/app/services/bikes.service'; import { flatten } from 'src/app/helperFunctions/flattenObject'; import { deepen } from 'src/app/helperFunctions/deepenObject'; -import { filter, propType } from 'graphql-anywhere'; +import { isPartOfGraphQLDoc } from 'src/app/helperFunctions/isPartOfGraphQLFunction'; +import { filter } from 'graphql-anywhere'; import { CargoBikeFieldsMutableFragmentDoc, CargoBikeUpdateInput, @@ -74,11 +75,6 @@ export class BikesComponent { for (let index in this.data) { this.data[index] = flatten(this.data[index]); } - - console.log(CargoBikeFieldsMutableFragmentDoc); - for (const prop in Object.keys(CargoBikeFieldsMutableFragmentDoc)) { - console.log(prop); - } //sort, so the displayedColumns array is in the same order as the columnInfo this.dataColumns.sort((columnA, columnB) => { @@ -133,6 +129,7 @@ export class BikesComponent { } getType(propertyName: string, row) { + //TODO: get type from introspection query return ( this.columnInfo.find((column) => column.name === propertyName)?.type || (typeof row[propertyName]) @@ -142,7 +139,7 @@ export class BikesComponent { isReadonly(propertyName: string) { return ( this.columnInfo.find((column) => column.name === propertyName) - ?.readonly || false + ?.readonly || !isPartOfGraphQLDoc(propertyName, CargoBikeFieldsMutableFragmentDoc) ); }