From 715caad708260be3dda25e38eeb299a00be4d79e Mon Sep 17 00:00:00 2001 From: arieger Date: Tue, 28 Jun 2022 11:38:23 +0200 Subject: [PATCH] feat: request application layers in bundle BREAKING CHANGE: Methods `parseNodes` and `parseFolder` are expecting a mandatory array of layer entities as second argument --- src/graphqlqueries/Layers.ts | 9 ++++ src/parser/SHOGunApplicationUtil.ts | 84 +++++++++++++++++++---------- 2 files changed, 66 insertions(+), 27 deletions(-) create mode 100644 src/graphqlqueries/Layers.ts diff --git a/src/graphqlqueries/Layers.ts b/src/graphqlqueries/Layers.ts new file mode 100644 index 000000000..39522751f --- /dev/null +++ b/src/graphqlqueries/Layers.ts @@ -0,0 +1,9 @@ +export const allLayersByIds = `query($ids: [Int]) { + allLayersByIds(ids: $ids) { + id + clientConfig + name + sourceConfig + type + } +}`; diff --git a/src/parser/SHOGunApplicationUtil.ts b/src/parser/SHOGunApplicationUtil.ts index 42cbec321..52291ffdd 100644 --- a/src/parser/SHOGunApplicationUtil.ts +++ b/src/parser/SHOGunApplicationUtil.ts @@ -36,6 +36,10 @@ import SHOGunAPIClient from '../service/SHOGunAPIClient'; import { getBearerTokenHeader } from '../security/getBearerTokenHeader'; + +import { + allLayersByIds +} from '../graphqlqueries/Layers'; export interface SHOGunApplicationUtilOpts { client?: SHOGunAPIClient; } @@ -95,49 +99,75 @@ class SHOGunApplicationUtil { return; } - const nodes = await this.parseNodes(layerTree.children, projection); + if (!this.client) { + Logger.warn('Cannot parse the layers in layertree because no ' + + 'SHOGunClient has been provided.'); + return; + } - const tree = new OlLayerGroup({ - layers: nodes.reverse(), - visible: layerTree.checked - }); + let applicationLayerIds: number[] = this.getLayerIds(layerTree.children); - return tree; + if (applicationLayerIds.length > 0) { + try { + const { allLayersByIds: layers } = await this.client.graphql().sendQuery({ + query: allLayersByIds, + variables: { + ids: applicationLayerIds + } + }); + if (layerTree.children) { + const nodes = await this.parseNodes(layerTree.children, layers, projection); + + const tree = new OlLayerGroup({ + layers: nodes.reverse(), + visible: layerTree.checked + }); + return tree; + } + } catch (e) { + Logger.warn('Could not parse the layer tree: ' + e); + return new OlLayerGroup(); + } + } + return new OlLayerGroup(); } - async parseNodes(nodes: DefaultLayerTree[], projection?: OlProjectionLike) { - const collection: OlLayerBase[] = []; + getLayerIds(nodes: DefaultLayerTree[], ids?: number[]) { + let layerIds: number[] = ids ?? []; for (const node of nodes) { - if (node.children) { - collection.push(await this.parseFolder(node, projection)); + if (node.children?.length > 0) { + this.getLayerIds(node.children, layerIds); } else { - if (!this.client) { - Logger.warn(`Couldn\'t parse the layer with ID ${node.layerId} because no ` + - 'SHOGunClient has been provided.'); - - continue; - } - - // TODO Fetch via graphlQL (multiple at once) - const layer = await this.client.layer().findOne(node.layerId); - - const olLayer = await this.parseLayer(layer, projection); + layerIds.push(node.layerId); + } + } + return layerIds; + } - olLayer.setVisible(node.checked); + async parseNodes(nodes: DefaultLayerTree[], layers: Layer[], projection?: OlProjectionLike) { + const collection: OlLayerBase[] = []; - collection.push(olLayer); + for (const node of nodes) { + if (node.children?.length > 0) { + collection.push(await this.parseFolder(node, layers, projection)); + } else { + const layerNode = layers.find(l => l.id === node.layerId); + if (layerNode) { + const olLayer = await this.parseLayer(layerNode as S, projection); + olLayer.setVisible(node.checked); + collection.push(olLayer); + } } } - return collection; } - async parseFolder(el: DefaultLayerTree, projection?: OlProjectionLike) { - const layers = await this.parseNodes(el.children, projection); + async parseFolder(el: DefaultLayerTree, layers: Layer[], projection?: OlProjectionLike) { + const layersInFolder = await this.parseNodes(el.children, layers, projection); const folder = new OlLayerGroup({ - layers: layers.reverse(), + layers: layersInFolder.reverse(), visible: el.checked });