Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/extend-id-filter-in.md
Original file line number Diff line number Diff line change
@@ -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] }`)
12 changes: 9 additions & 3 deletions packages/hypergraph/src/entity/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
| {
Expand Down
10 changes: 6 additions & 4 deletions packages/hypergraph/src/utils/translate-filter-to-graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ type GraphqlFilterEntry =
};
}
| {
id: {
is: string;
};
id: EntityIdGraphqlFilter;
}
| { [k: string]: never };

Expand Down Expand Up @@ -95,10 +93,14 @@ export function translateFilterToGraphql<S extends Schema.Schema.AnyNoContext>(

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;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/hypergraph/test/utils/translate-filter-to-graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
Loading