-
Notifications
You must be signed in to change notification settings - Fork 13
Nik/query config #569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nik/query config #569
Changes from 8 commits
64f4e00
082f964
98a9db5
8dafe44
73d8ee2
3b38b8b
482f7e2
d6ab918
4b1f4bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,18 @@ | ||||||
| --- | ||||||
| "@graphprotocol/hypergraph": patch | ||||||
| "@graphprotocol/hypergraph-react": patch | ||||||
| --- | ||||||
|
|
||||||
| Allow relation includes to override nested relation and value space filters by adding _config: { relationSpaces, valueSpaces } to any include branch; GraphQL fragments now honor those overrides when building queries. | ||||||
|
|
||||||
| ``` | ||||||
| include: { | ||||||
| friends: { | ||||||
| _config: { | ||||||
| relationSpaces: ['space-a', 'space-b'], | ||||||
| valueSpaces: 'all', | ||||||
| }, | ||||||
| }, | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
|
Comment on lines
+17
to
+18
|
||||||
| ``` | |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -1,6 +1,6 @@ | ||||
| import { useEntities, useEntity, usePublicSpaces } from '@graphprotocol/hypergraph-react'; | ||||
| import { createLazyFileRoute } from '@tanstack/react-router'; | ||||
| import { Podcast, Topic } from '@/schema'; | ||||
| import { Person, Podcast, Topic } from '@/schema'; | ||||
|
|
||||
| export const Route = createLazyFileRoute('/podcasts')({ | ||||
| component: RouteComponent, | ||||
|
|
@@ -22,6 +22,25 @@ function RouteComponent() { | |||
| // }, 1000); | ||||
| // }, []); | ||||
|
|
||||
| const { | ||||
| data: person, | ||||
| invalidEntity: personInvalidEntity, | ||||
| invalidRelationEntities: personInvalidRelationEntities, | ||||
| } = useEntity(Person, { | ||||
| id: '9800a4e8-8437-4310-9af6-ac91644f7c26', | ||||
| mode: 'public', | ||||
| space: '95a4a1cc-bfcc-4038-b7a1-02c513d27700', | ||||
| include: { | ||||
| skills: { | ||||
| _config: { | ||||
| relationSpaces: ['95a4a1cc-bfcc-4038-b7a1-02c513d27700'], | ||||
| valueSpaces: ['021265e2-d839-47c3-8d03-0ee3dfb29ffc', '95a4a1cc-bfcc-4038-b7a1-02c513d27700'], | ||||
| }, | ||||
| }, | ||||
| }, | ||||
| }); | ||||
| console.log({ person, personInvalidEntity, personInvalidRelationEntities }); | ||||
|
||||
| console.log({ person, personInvalidEntity, personInvalidRelationEntities }); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -62,6 +62,36 @@ const { data, isPending, isError } = useEntities(Event, { | |||||
|
|
||||||
| For deeper relations you can use the `include` parameter multiple levels deep. Currently two levels of relations are supported for public data. | ||||||
|
|
||||||
| #### Controlling include scopes with `_config` | ||||||
|
|
||||||
| Each branch within `include` can optionally carry a `_config` object that lets you override which spaces Hypergraph will inspect for the relation edges and the related entity values. When you omit `_config`, the query automatically reuses the `space`/`spaces` selection you passed to `useEntities`, `useEntity`, `Entity.findOnePublic`, `Entity.findManyPublic` and `Entity.searchManyPublic` helpers. | ||||||
|
|
||||||
| ```ts | ||||||
| const { data: project } = useEntity(Project, { | ||||||
| id: '9f130661-8c3f-4db7-9bdc-3ce69631c5ef', | ||||||
| mode: 'public', | ||||||
| space: '3f32353d-3b27-4a13-b71a-746f06e1f7db', | ||||||
| include: { | ||||||
| contributors: { | ||||||
| _config: { | ||||||
| relationSpaces: ['3f32353d-3b27-4a13-b71a-746f06e1f7db', '95a4a1cc-bfcc-4038-b7a1-02c513d27700'], | ||||||
| valueSpaces: 'all', | ||||||
| }, | ||||||
| organizations: { | ||||||
| _config: { | ||||||
| valueSpaces: ['95a4a1cc-bfcc-4038-b7a1-02c513d27700'], | ||||||
| }, | ||||||
| }, | ||||||
| }, | ||||||
| }, | ||||||
| }); | ||||||
| ``` | ||||||
|
|
||||||
| - `relationSpaces` controls which spaces are searched for the relation edges themselves (`relations`/`backlinks`). Pass an array to whitelist specific spaces, `'all'` to drop the filter entirely, or `[]` if you intentionally want the branch to match nothing. | ||||||
| - `valueSpaces` applies the same override to the `valuesList` lookups for the related entities. This lets you fetch relation edges from one space while trusting the canonical values that live in another. | ||||||
|
|
||||||
| Overrides cascade to nested branches, so you can attach `_config` anywhere within the two supported include levels. Mix and match the settings per branch to stitch together data that spans multiple public spaces without issuing separate queries. | ||||||
|
||||||
| Overrides cascade to nested branches, so you can attach `_config` anywhere within the two supported include levels. Mix and match the settings per branch to stitch together data that spans multiple public spaces without issuing separate queries. | |
| Each nested branch can have its own `_config` settings, so you can attach `_config` anywhere within the two supported include levels. Mix and match the settings per branch to stitch together data that spans multiple public spaces without issuing separate queries. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,8 +2,17 @@ import type * as Schema from 'effect/Schema'; | |||||
|
|
||||||
| type SchemaKey<S extends Schema.Schema.AnyNoContext> = Extract<keyof Schema.Schema.Type<S>, string>; | ||||||
|
|
||||||
| export type RelationSpacesOverride = 'all' | readonly string[]; | ||||||
|
|
||||||
| export type RelationIncludeConfig = { | ||||||
| relationSpaces?: RelationSpacesOverride; | ||||||
| valueSpaces?: RelationSpacesOverride; | ||||||
| }; | ||||||
|
|
||||||
| export type RelationIncludeBranch = { | ||||||
| [key: string]: RelationIncludeBranch | boolean | undefined; | ||||||
| _config?: RelationIncludeConfig; | ||||||
| } & { | ||||||
| [key: string]: RelationIncludeBranch | RelationIncludeConfig | boolean | undefined; | ||||||
|
||||||
| [key: string]: RelationIncludeBranch | RelationIncludeConfig | boolean | undefined; | |
| [K in Exclude<string, '_config'>]?: RelationIncludeBranch | RelationIncludeConfig | boolean; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,24 +2,58 @@ import type { RelationTypeIdInfo } from './get-relation-type-ids.js'; | |
|
|
||
| type SpaceSelectionMode = 'single' | 'many' | 'all'; | ||
|
|
||
| const buildValuesListFilter = (spaceSelectionMode: SpaceSelectionMode) => { | ||
| if (spaceSelectionMode === 'single') { | ||
| return '(filter: {spaceId: {is: $spaceId}})'; | ||
| const formatGraphQLStringArray = (values: readonly string[]) => | ||
| `[${values.map((value) => JSON.stringify(value)).join(', ')}]`; | ||
|
|
||
| const buildValuesListFilter = ( | ||
| spaceSelectionMode: SpaceSelectionMode, | ||
| override?: RelationTypeIdInfo['valueSpaces'], | ||
| ) => { | ||
| if (!override) { | ||
| if (spaceSelectionMode === 'single') { | ||
| return '(filter: {spaceId: {is: $spaceId}})'; | ||
| } | ||
| if (spaceSelectionMode === 'many') { | ||
| return '(filter: {spaceId: {in: $spaceIds}})'; | ||
| } | ||
| return ''; | ||
| } | ||
| if (spaceSelectionMode === 'many') { | ||
| return '(filter: {spaceId: {in: $spaceIds}})'; | ||
|
|
||
| if (override === 'all') { | ||
| return ''; | ||
| } | ||
|
|
||
| if (override.length === 0) { | ||
| // Explicit empty overrides should produce a match-nothing filter. | ||
| return '(filter: {spaceId: {in: []}})'; | ||
| } | ||
|
Comment on lines
+26
to
29
|
||
| return ''; | ||
|
|
||
| return `(filter: {spaceId: {in: ${formatGraphQLStringArray(override)}}})`; | ||
| }; | ||
|
|
||
| const buildRelationSpaceFilter = (spaceSelectionMode: SpaceSelectionMode) => { | ||
| if (spaceSelectionMode === 'single') { | ||
| return 'spaceId: {is: $spaceId}, '; | ||
| const buildRelationSpaceFilter = ( | ||
| spaceSelectionMode: SpaceSelectionMode, | ||
| override?: RelationTypeIdInfo['relationSpaces'], | ||
| ) => { | ||
| if (!override) { | ||
| if (spaceSelectionMode === 'single') { | ||
| return 'spaceId: {is: $spaceId}, '; | ||
| } | ||
| if (spaceSelectionMode === 'many') { | ||
| return 'spaceId: {in: $spaceIds}, '; | ||
| } | ||
| return ''; | ||
| } | ||
|
|
||
| if (override === 'all') { | ||
| return ''; | ||
| } | ||
| if (spaceSelectionMode === 'many') { | ||
| return 'spaceId: {in: $spaceIds}, '; | ||
|
|
||
| if (override.length === 0) { | ||
| return 'spaceId: {in: []}, '; | ||
| } | ||
| return ''; | ||
|
|
||
| return `spaceId: {in: ${formatGraphQLStringArray(override)}}, `; | ||
| }; | ||
|
|
||
| export const getRelationAlias = (typeId: string) => `relations_${typeId.replace(/-/g, '_')}`; | ||
|
|
@@ -31,8 +65,8 @@ const buildRelationsListFragment = (info: RelationTypeIdInfo, level: 1 | 2, spac | |
| const connectionField = listField === 'backlinks' ? 'backlinks' : 'relations'; | ||
| const toEntityField = listField === 'backlinks' ? 'fromEntity' : 'toEntity'; | ||
| const toEntitySelectionHeader = toEntityField === 'toEntity' ? 'toEntity' : `toEntity: ${toEntityField}`; | ||
| const valuesListFilter = buildValuesListFilter(spaceSelectionMode); | ||
| const relationSpaceFilter = buildRelationSpaceFilter(spaceSelectionMode); | ||
| const valuesListFilter = buildValuesListFilter(spaceSelectionMode, info.valueSpaces); | ||
| const relationSpaceFilter = buildRelationSpaceFilter(spaceSelectionMode, info.relationSpaces); | ||
|
|
||
| if (!info.includeNodes && !info.includeTotalCount) { | ||
| return ''; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove trailing whitespace at the end of the file.