2022-09-26 10:35:45 +00:00
|
|
|
import type { Schema } from '@vegaprotocol/types';
|
|
|
|
|
2022-10-19 09:14:18 +00:00
|
|
|
type Edge<T> = {
|
2022-09-26 10:35:45 +00:00
|
|
|
node: T;
|
|
|
|
};
|
|
|
|
|
2022-10-19 09:14:18 +00:00
|
|
|
type Connection<A> = {
|
2022-09-26 10:35:45 +00:00
|
|
|
edges?: Schema.Maybe<Array<Schema.Maybe<A>>>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function getNodes<
|
|
|
|
T,
|
2022-10-19 09:14:18 +00:00
|
|
|
A extends Edge<T> = Edge<T>,
|
2022-09-26 10:35:45 +00:00
|
|
|
B extends Connection<A> = Connection<A>
|
|
|
|
>(data?: B | null, filterBy?: (item?: T | null) => boolean) {
|
|
|
|
const edges = data?.edges || [];
|
|
|
|
return edges.reduce<T[]>((acc, edge) => {
|
|
|
|
if (edge?.node && (filterBy ? filterBy(edge?.node) : true)) {
|
|
|
|
acc.push(edge.node);
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
}
|