From 170e6cd3a3534ac817f2be72758850472fa60f64 Mon Sep 17 00:00:00 2001 From: Nik Graf Date: Mon, 16 Mar 2026 09:08:10 +0100 Subject: [PATCH] extend id filter to support in --- .changeset/extend-id-filter-in.md | 5 +++++ packages/hypergraph/src/entity/types.ts | 12 +++++++++--- .../src/utils/translate-filter-to-graphql.ts | 10 ++++++---- .../test/utils/translate-filter-to-graphql.test.ts | 12 ++++++++++++ 4 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 .changeset/extend-id-filter-in.md diff --git a/.changeset/extend-id-filter-in.md b/.changeset/extend-id-filter-in.md new file mode 100644 index 00000000..489012ab --- /dev/null +++ b/.changeset/extend-id-filter-in.md @@ -0,0 +1,5 @@ +--- +"@graphprotocol/hypergraph": patch +--- + +Add `in` operator support to entity id filter, allowing filtering by multiple ids (e.g. `id: { in: [id1, id2] }`) diff --git a/packages/hypergraph/src/entity/types.ts b/packages/hypergraph/src/entity/types.ts index ea54c8c1..e7945c30 100644 --- a/packages/hypergraph/src/entity/types.ts +++ b/packages/hypergraph/src/entity/types.ts @@ -70,9 +70,15 @@ export type EntityStringFilter = { contains?: string; }; -export type EntityIdFilter = { - is?: string; -}; +export type EntityIdFilter = + | { + is: string; + in?: never; + } + | { + in: readonly string[]; + is?: never; + }; export type RelationEntityIdFilter = | { diff --git a/packages/hypergraph/src/utils/translate-filter-to-graphql.ts b/packages/hypergraph/src/utils/translate-filter-to-graphql.ts index 30debb58..c4e7b1cf 100644 --- a/packages/hypergraph/src/utils/translate-filter-to-graphql.ts +++ b/packages/hypergraph/src/utils/translate-filter-to-graphql.ts @@ -53,9 +53,7 @@ type GraphqlFilterEntry = }; } | { - id: { - is: string; - }; + id: EntityIdGraphqlFilter; } | { [k: string]: never }; @@ -95,10 +93,14 @@ export function translateFilterToGraphql( if (fieldName === 'id') { const idFilter = fieldFilter as Entity.EntityIdFilter; - if (typeof idFilter.is === 'string') { + if ('is' in idFilter && typeof idFilter.is === 'string') { graphqlFilter.push({ id: { is: idFilter.is }, }); + } else if ('in' in idFilter && Array.isArray(idFilter.in)) { + graphqlFilter.push({ + id: { in: idFilter.in }, + }); } continue; } diff --git a/packages/hypergraph/test/utils/translate-filter-to-graphql.test.ts b/packages/hypergraph/test/utils/translate-filter-to-graphql.test.ts index b5722e31..67412ca7 100644 --- a/packages/hypergraph/test/utils/translate-filter-to-graphql.test.ts +++ b/packages/hypergraph/test/utils/translate-filter-to-graphql.test.ts @@ -189,6 +189,18 @@ describe('translateFilterToGraphql id filters', () => { }); }); + it('should translate id `in` filter correctly', () => { + const filter: TodoFilter = { + id: { in: ['entity-id-1', 'entity-id-2'] }, + }; + + const result = translateFilterToGraphql(filter, Todo); + + expect(result).toEqual({ + id: { in: ['entity-id-1', 'entity-id-2'] }, + }); + }); + it('should combine id filter with other property filters', () => { const filter: TodoFilter = { id: { is: 'entity-id' },