vega-frontend-monorepo/libs/react-helpers/src/lib/get-nodes.ts
botond 87f7efd960
Chore/migrate to new types (#1439)
* fix: candle type paths

* chore: migrate deal-ticket

* chore: migrate deposits

* fix: format

* chore: migrate environment

* fix: format

* chore: migrate fills

* fix: format

* chore: fix build errors

* fix: format

* fix: lint

* fix: env tests

* fix: add missing query variable

* fix: format

* fix: change vega_url because n11 is down

* fix: deal ticket query name

* chore: extend event type

* fix: incorrect e2e test
2022-09-26 11:35:45 +01:00

26 lines
591 B
TypeScript

import type { Schema } from '@vegaprotocol/types';
export type Node<T> = {
__typename?: string;
node: T;
};
export type Connection<A> = {
__typename?: string;
edges?: Schema.Maybe<Array<Schema.Maybe<A>>>;
};
export function getNodes<
T,
A extends Node<T> = Node<T>,
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;
}, []);
}