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

Merged
ashwin merged 1 commits from pm-handle-array-fields into ng-rm-record-types 2024-01-11 10:22:37 +00:00
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

@ -86,10 +86,19 @@ 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];
} }