From e92bf8e431758651a9e7655fda537525b10b51a3 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Thu, 11 Jan 2024 15:31:50 +0530 Subject: [PATCH] Select one level deep values for array type attributes from GQL query result --- src/registry-client.ts | 11 ++++++++++- src/util.ts | 13 +++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/registry-client.ts b/src/registry-client.ts index 5772348..ecff7f6 100644 --- a/src/registry-client.ts +++ b/src/registry-client.ts @@ -16,7 +16,16 @@ const attributeField = ` ... on StringValue { string: value } ... on BytesValue { bytes: value } ... on LinkValue { link: value } - ... on ArrayValue { array: value { __typename } } + ... on ArrayValue { + array: value { + ... on BooleanValue { bool: value } + ... on IntValue { int: value } + ... on FloatValue { float: value } + ... on StringValue { string: value } + ... on BytesValue { bytes: value } + ... on LinkValue { link: value } + } + } ... on MapValue { map: value { key mapping: value { __typename } } } } } diff --git a/src/util.ts b/src/util.ts index c42b444..222742d 100644 --- a/src/util.ts +++ b/src/util.ts @@ -36,7 +36,7 @@ export class Util { const vars: any[] = []; Object.keys(obj).forEach(key => { const value = this.toGQLValue(obj[key]); - + if (value !== undefined) { vars.push({ key, value }); } @@ -86,13 +86,22 @@ export class Util { } static fromGQLValue(obj: any) { + // Get first non-null key const present = Object.keys(obj).find(k => obj[k] !== null); if (present === undefined) { throw new Error('Object has no non-null values'); } + + // Create an array if array type attribute + if (present === 'array') { + return obj[present].map((e: any) => { + return this.fromGQLValue(e); + }); + } + return obj[present]; } - + /** * Get record content ID. */ -- 2.45.2