-
Notifications
You must be signed in to change notification settings - Fork 13
implement Space.findManyPublic #563
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
57fc695
implement Space.findManyPublic
nikgraf 4496248
implement it an try it out
nikgraf 09b09df
rename findSpaces with findManyPublic
nikgraf c1f40da
add changelog
nikgraf e5c5627
add member filters
nikgraf 1691e93
improve docs
nikgraf 4d799f5
fix type issue
nikgraf 8ba17e9
Update docs/docs/query-public-data.md
nikgraf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@graphprotocol/hypergraph-react": patch | ||
| "@graphprotocol/hypergraph": patch | ||
| --- | ||
|
|
||
| Add Space.findManyPublic plus the usePublicSpaces hook so apps can fetch and render public spaces (with invalid entries surfaced) via one shared SDK call | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { Space } from '@graphprotocol/hypergraph'; | ||
| import { useQuery } from '@tanstack/react-query'; | ||
|
|
||
| type UsePublicSpacesParams = Readonly<{ | ||
| filter?: Space.FindManyPublicParams['filter']; | ||
| enabled?: boolean; | ||
| }>; | ||
|
|
||
| export const usePublicSpaces = (params?: UsePublicSpacesParams) => { | ||
| const { filter, enabled = true } = params ?? {}; | ||
|
|
||
| const result = useQuery({ | ||
| queryKey: ['hypergraph-public-spaces', filter], | ||
| queryFn: () => (filter ? Space.findManyPublic({ filter }) : Space.findManyPublic()), | ||
| enabled, | ||
| }); | ||
|
|
||
| return { | ||
| ...result, | ||
| data: result.data?.data ?? [], | ||
| invalidSpaces: result.data?.invalidSpaces ?? [], | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| import { ContentIds, Graph, SystemIds } from '@graphprotocol/grc-20'; | ||
| import * as Either from 'effect/Either'; | ||
| import * as EffectSchema from 'effect/Schema'; | ||
| import { request } from 'graphql-request'; | ||
|
|
||
| const spaceFields = ` | ||
| id | ||
| page { | ||
| name | ||
| relationsList(filter: { | ||
| typeId: { is: "${ContentIds.AVATAR_PROPERTY}"} | ||
| }) { | ||
| toEntity { | ||
| valuesList(filter: { | ||
| propertyId: { is: "${SystemIds.IMAGE_URL_PROPERTY}"} | ||
| }) { | ||
| propertyId | ||
| string | ||
| } | ||
| } | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| const spacesQueryDocument = ` | ||
| query spaces { | ||
| spaces { | ||
| ${spaceFields} | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| const memberSpacesQueryDocument = ` | ||
| query memberSpaces($accountAddress: String!) { | ||
| spaces(filter: {members: {some: {address: {is: $accountAddress}}}}) { | ||
| ${spaceFields} | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| const editorSpacesQueryDocument = ` | ||
| query editorSpaces($accountAddress: String!) { | ||
| spaces(filter: {editors: {some: {address: {is: $accountAddress}}}}) { | ||
| ${spaceFields} | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| export const PublicSpaceSchema = EffectSchema.Struct({ | ||
| id: EffectSchema.String, | ||
| name: EffectSchema.String, | ||
| avatar: EffectSchema.optional(EffectSchema.String), | ||
| }); | ||
|
|
||
| export type PublicSpace = typeof PublicSpaceSchema.Type; | ||
|
|
||
| type SpacesQueryResult = { | ||
| spaces?: { | ||
| id: string; | ||
| page: { | ||
| name?: string | null; | ||
| relationsList?: { | ||
| toEntity?: { | ||
| valuesList?: { | ||
| propertyId: string; | ||
| string: string | null; | ||
| }[]; | ||
| } | null; | ||
| }[]; | ||
| } | null; | ||
| }[]; | ||
| }; | ||
|
|
||
| type SpacesQueryVariables = { | ||
| accountAddress: string; | ||
| }; | ||
|
|
||
| type SpaceQueryEntry = NonNullable<SpacesQueryResult['spaces']>[number]; | ||
|
|
||
| const decodeSpace = EffectSchema.decodeUnknownEither(PublicSpaceSchema); | ||
|
|
||
| const getAvatarFromSpace = (space: SpaceQueryEntry) => { | ||
| const firstRelation = space.page?.relationsList?.[0]; | ||
| const firstValue = firstRelation?.toEntity?.valuesList?.[0]; | ||
| const avatar = firstValue?.string; | ||
| if (typeof avatar === 'string') { | ||
| return avatar; | ||
| } | ||
| return undefined; | ||
| }; | ||
|
|
||
| export const parseSpacesQueryResult = (queryResult: SpacesQueryResult) => { | ||
| const data: PublicSpace[] = []; | ||
| const invalidSpaces: Record<string, unknown>[] = []; | ||
| const spaces = queryResult.spaces ?? []; | ||
|
|
||
| for (const space of spaces) { | ||
| const rawSpace: Record<string, unknown> = { | ||
| id: space.id, | ||
| name: space.page?.name ?? undefined, | ||
| avatar: getAvatarFromSpace(space), | ||
| }; | ||
|
|
||
| const decodedSpace = decodeSpace(rawSpace); | ||
|
|
||
| if (Either.isRight(decodedSpace)) { | ||
| data.push(decodedSpace.right); | ||
| } else { | ||
| invalidSpaces.push(rawSpace); | ||
| } | ||
| } | ||
|
|
||
| return { data, invalidSpaces }; | ||
| }; | ||
|
|
||
| export type FindManyPublicFilter = | ||
| | Readonly<{ memberAccountAddress: string; editorAccountAddress?: never }> | ||
| | Readonly<{ editorAccountAddress: string; memberAccountAddress?: never }> | ||
| | Readonly<{ memberAccountAddress?: undefined; editorAccountAddress?: undefined }>; | ||
|
|
||
| export type FindManyPublicParams = Readonly<{ | ||
| filter?: FindManyPublicFilter; | ||
| }>; | ||
|
|
||
| export const findManyPublic = async (params?: FindManyPublicParams) => { | ||
| const filter = params?.filter; | ||
| const memberAccountAddress = filter?.memberAccountAddress; | ||
| const editorAccountAddress = filter?.editorAccountAddress; | ||
|
|
||
| if (memberAccountAddress && editorAccountAddress) { | ||
| throw new Error('Provide only one of memberAccountAddress or editorAccountAddress when calling findManyPublic().'); | ||
| } | ||
|
|
||
| const endpoint = `${Graph.TESTNET_API_ORIGIN}/graphql`; | ||
|
|
||
| if (memberAccountAddress) { | ||
| const queryResult = await request<SpacesQueryResult, SpacesQueryVariables>(endpoint, memberSpacesQueryDocument, { | ||
| accountAddress: memberAccountAddress, | ||
| }); | ||
| return parseSpacesQueryResult(queryResult); | ||
| } | ||
|
|
||
| if (editorAccountAddress) { | ||
| const queryResult = await request<SpacesQueryResult, SpacesQueryVariables>(endpoint, editorSpacesQueryDocument, { | ||
| accountAddress: editorAccountAddress, | ||
| }); | ||
| return parseSpacesQueryResult(queryResult); | ||
| } | ||
|
|
||
| const queryResult = await request<SpacesQueryResult>(endpoint, spacesQueryDocument); | ||
| return parseSpacesQueryResult(queryResult); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './find-many-public.js'; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This line contains trailing whitespace. Consider removing it for consistency with standard formatting practices.