Select one level deep values for array type attributes from GQL query result

This commit is contained in:
Prathamesh Musale 2024-01-11 15:31:50 +05:30
parent 17b21c4750
commit e92bf8e431
2 changed files with 21 additions and 3 deletions

View File

@ -16,7 +16,16 @@ const attributeField = `
... on StringValue { string: value } ... on StringValue { string: value }
... on BytesValue { bytes: value } ... on BytesValue { bytes: value }
... on LinkValue { link: 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 } } } ... on MapValue { map: value { key mapping: value { __typename } } }
} }
} }

View File

@ -36,7 +36,7 @@ export class Util {
const vars: any[] = []; const vars: any[] = [];
Object.keys(obj).forEach(key => { Object.keys(obj).forEach(key => {
const value = this.toGQLValue(obj[key]); const value = this.toGQLValue(obj[key]);
if (value !== undefined) { if (value !== undefined) {
vars.push({ key, value }); vars.push({ key, value });
} }
@ -86,13 +86,22 @@ export class Util {
} }
static fromGQLValue(obj: any) { static fromGQLValue(obj: any) {
// Get first non-null key
const present = Object.keys(obj).find(k => obj[k] !== null); const present = Object.keys(obj).find(k => obj[k] !== null);
if (present === undefined) { if (present === undefined) {
throw new Error('Object has no non-null values'); 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]; return obj[present];
} }
/** /**
* Get record content ID. * Get record content ID.
*/ */