From 4a68f6f85ba232608bf28b8a394e0ef24804303d Mon Sep 17 00:00:00 2001 From: Juan Garcia Date: Mon, 16 Dec 2024 11:18:50 +0100 Subject: [PATCH] Implement queries for feed/graphs/namespaces/... --- packages/client/src/actions/admins.ts | 27 ++++++- packages/client/src/actions/feed.ts | 57 ++++++++++++++- packages/client/src/actions/graph.ts | 57 ++++++++++++++- packages/client/src/actions/namespace.ts | 56 ++++++++++++++- packages/graphql/src/admins.ts | 26 +++++++ packages/graphql/src/feed.ts | 33 ++++++++- packages/graphql/src/fragments/index.ts | 1 - packages/graphql/src/fragments/namespace.ts | 27 ------- packages/graphql/src/fragments/post.ts | 6 +- packages/graphql/src/fragments/primitives.ts | 74 +++++++++++++++++++- packages/graphql/src/fragments/username.ts | 4 +- packages/graphql/src/graph.ts | 33 ++++++++- packages/graphql/src/namespace.ts | 33 ++++++++- 13 files changed, 386 insertions(+), 48 deletions(-) delete mode 100644 packages/graphql/src/fragments/namespace.ts diff --git a/packages/client/src/actions/admins.ts b/packages/client/src/actions/admins.ts index 094fabfe7..97860ecfb 100644 --- a/packages/client/src/actions/admins.ts +++ b/packages/client/src/actions/admins.ts @@ -1,13 +1,16 @@ import type { AddAdminsRequest, AddAdminsResult, + Admin, + AdminsForRequest, RemoveAdminsRequest, RemoveAdminsResult, } from '@lens-protocol/graphql'; -import { AddAdminsMutation, RemoveAdminsMutation } from '@lens-protocol/graphql'; +import { AddAdminsMutation, AdminsForQuery, RemoveAdminsMutation } from '@lens-protocol/graphql'; import type { ResultAsync } from '@lens-protocol/types'; -import type { SessionClient } from '../clients'; +import type { Paginated } from '@lens-protocol/graphql'; +import type { AnyClient, SessionClient } from '../clients'; import type { UnauthenticatedError, UnexpectedError } from '../errors'; /** @@ -51,3 +54,23 @@ export function removeAdmins( ): ResultAsync { return client.mutation(RemoveAdminsMutation, { request }); } + +/** + * Fetch admins for. + * + * ```ts + * const result = await fetchAdminsFor(anyClient, { + * address: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'), + * }); + * ``` + * + * @param client - Any Lens client. + * @param request - The query request. + * @returns The list of admins or empty if it does not exist. + */ +export function fetchAdminsFor( + client: AnyClient, + request: AdminsForRequest, +): ResultAsync | null, UnexpectedError> { + return client.query(AdminsForQuery, { request }); +} diff --git a/packages/client/src/actions/feed.ts b/packages/client/src/actions/feed.ts index ee7533cf9..3ed4a2de8 100644 --- a/packages/client/src/actions/feed.ts +++ b/packages/client/src/actions/feed.ts @@ -1,8 +1,15 @@ -import type { CreateFeedRequest, CreateFeedResult } from '@lens-protocol/graphql'; -import { CreateFeedMutation } from '@lens-protocol/graphql'; +import type { + CreateFeedRequest, + CreateFeedResult, + Feed, + FeedRequest, + FeedsRequest, + Paginated, +} from '@lens-protocol/graphql'; +import { CreateFeedMutation, FeedQuery, FeedsQuery } from '@lens-protocol/graphql'; import type { ResultAsync } from '@lens-protocol/types'; -import type { SessionClient } from '../clients'; +import type { AnyClient, SessionClient } from '../clients'; import type { UnauthenticatedError, UnexpectedError } from '../errors'; /** @@ -22,3 +29,47 @@ export function createFeed( ): ResultAsync { return client.mutation(CreateFeedMutation, { request }); } + +/** + * Fetch a Feed. + * + * ```ts + * const result = await fetchFeed(anyClient, { + * feed: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'), + * }); + * ``` + * + * @param client - Any Lens client. + * @param request - The Feed query request. + * @returns The Feed or `null` if it does not exist. + */ +export function fetchFeed( + client: AnyClient, + request: FeedRequest, +): ResultAsync { + return client.query(FeedQuery, { request }); +} + +/** + * Fetch Feeds. + * + * ```ts + * const result = await fetchFeeds(anyClient, { + * filter: { + * managedBy: { + * address: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5') + * } + * }, + * }); + * ``` + * + * @param client - Any Lens client. + * @param request - The Feeds query request. + * @returns The list of Feeds or empty list if none exist. + */ +export function fetchFeeds( + client: AnyClient, + request: FeedsRequest, +): ResultAsync | null, UnexpectedError> { + return client.query(FeedsQuery, { request }); +} diff --git a/packages/client/src/actions/graph.ts b/packages/client/src/actions/graph.ts index 75ac6145e..15bf114f2 100644 --- a/packages/client/src/actions/graph.ts +++ b/packages/client/src/actions/graph.ts @@ -1,8 +1,15 @@ -import type { CreateGraphRequest, CreateGraphResult } from '@lens-protocol/graphql'; -import { CreateGraphMutation } from '@lens-protocol/graphql'; +import type { + CreateGraphRequest, + CreateGraphResult, + Graph, + GraphRequest, + GraphsRequest, + Paginated, +} from '@lens-protocol/graphql'; +import { CreateGraphMutation, GraphQuery, GraphsQuery } from '@lens-protocol/graphql'; import type { ResultAsync } from '@lens-protocol/types'; -import type { SessionClient } from '../clients'; +import type { AnyClient, SessionClient } from '../clients'; import type { UnauthenticatedError, UnexpectedError } from '../errors'; /** @@ -22,3 +29,47 @@ export function createGraph( ): ResultAsync { return client.mutation(CreateGraphMutation, { request }); } + +/** + * Fetch a Graph. + * + * ```ts + * const result = await fetchGraph(anyClient, { + * graph: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'), + * }); + * ``` + * + * @param client - Any Lens client. + * @param request - The Graph query request. + * @returns The Graph or `null` if it does not exist. + */ +export function fetchGraph( + client: AnyClient, + request: GraphRequest, +): ResultAsync { + return client.query(GraphQuery, { request }); +} + +/** + * Fetch Graphs. + * + * ```ts + * const result = await fetchGraphs(anyClient, { + * filter: { + * managedBy: { + * address: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5') + * } + * }, + * }); + * ``` + * + * @param client - Any Lens client. + * @param request - The Graphs query request. + * @returns The list of Graphs or empty list if none exist. + */ +export function fetchGraphs( + client: AnyClient, + request: GraphsRequest, +): ResultAsync | null, UnexpectedError> { + return client.query(GraphsQuery, { request }); +} diff --git a/packages/client/src/actions/namespace.ts b/packages/client/src/actions/namespace.ts index f4a22c2df..d5251e2c6 100644 --- a/packages/client/src/actions/namespace.ts +++ b/packages/client/src/actions/namespace.ts @@ -1,11 +1,19 @@ import type { CreateUsernameNamespaceRequest, CreateUsernameNamespaceResult, + NamespacesRequest, + Paginated, + UsernameNamespace, + UsernameNamespaceRequest, +} from '@lens-protocol/graphql'; +import { + CreateUsernameNamespaceMutation, + NamespacesQuery, + UsernameNamespaceQuery, } from '@lens-protocol/graphql'; -import { CreateUsernameNamespaceMutation } from '@lens-protocol/graphql'; import type { ResultAsync } from '@lens-protocol/types'; -import type { SessionClient } from '../clients'; +import type { AnyClient, SessionClient } from '../clients'; import type { UnauthenticatedError, UnexpectedError } from '../errors'; /** @@ -28,3 +36,47 @@ export function createUsernameNamespace( ): ResultAsync { return client.mutation(CreateUsernameNamespaceMutation, { request }); } + +/** + * Fetch a UsernameNamespace. + * + * ```ts + * const result = await fetchUsernameNamespace(anyClient, { + * namespace: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'), + * }); + * ``` + * + * @param client - Any Lens client. + * @param request - The query request. + * @returns The UsernameNamespace or `null` if it does not exist. + */ +export function fetchUsernameNamespace( + client: AnyClient, + request: UsernameNamespaceRequest, +): ResultAsync { + return client.query(UsernameNamespaceQuery, { request }); +} + +/** + * Fetch Namespaces. + * + * ```ts + * const result = await fetchNamespaces(anyClient, { + * filter: { + * managedBy: { + * address: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5') + * } + * }, + * }); + * ``` + * + * @param client - Any Lens client. + * @param request - The query request. + * @returns The list of Namespaces or empty list if none exist. + */ +export function fetchNamespaces( + client: AnyClient, + request: NamespacesRequest, +): ResultAsync | null, UnexpectedError> { + return client.query(NamespacesQuery, { request }); +} diff --git a/packages/graphql/src/admins.ts b/packages/graphql/src/admins.ts index 64edd50b2..ac1ce1a34 100644 --- a/packages/graphql/src/admins.ts +++ b/packages/graphql/src/admins.ts @@ -1,5 +1,6 @@ import type { FragmentOf } from 'gql.tada'; import { + PaginatedResultInfoFragment, SelfFundedTransactionRequest, SponsoredTransactionRequest, TransactionWillFail, @@ -57,3 +58,28 @@ export const RemoveAdminsMutation = graphql( [RemoveAdminsResult], ); export type RemoveAdminsRequest = RequestOf; + +export const AdminFragment = graphql( + `fragment Admin on Admin { + __typename + address + addedAt + }`, +); +export type Admin = FragmentOf; + +export const AdminsForQuery = graphql( + `query AdminsFor($request: AdminsForRequest!) { + value: adminsFor(request: $request) { + __typename + items { + ...Admin + } + pageInfo { + ...PaginatedResultInfo + } + } + }`, + [AdminFragment, PaginatedResultInfoFragment], +); +export type AdminsForRequest = RequestOf; diff --git a/packages/graphql/src/feed.ts b/packages/graphql/src/feed.ts index 166312318..6eed2df02 100644 --- a/packages/graphql/src/feed.ts +++ b/packages/graphql/src/feed.ts @@ -1,5 +1,10 @@ import type { FragmentOf } from 'gql.tada'; -import { SelfFundedTransactionRequest, TransactionWillFail } from './fragments'; +import { + FeedFragment, + PaginatedResultInfoFragment, + SelfFundedTransactionRequest, + TransactionWillFail, +} from './fragments'; import { type RequestOf, graphql } from './graphql'; const CreateFeedResponse = graphql( @@ -35,3 +40,29 @@ export const CreateFeedMutation = graphql( [CreateFeedResult], ); export type CreateFeedRequest = RequestOf; + +export const FeedQuery = graphql( + `query Feed($request: FeedRequest!) { + value: feed(request: $request) { + ...Feed + } + }`, + [FeedFragment], +); +export type FeedRequest = RequestOf; + +export const FeedsQuery = graphql( + `query Feeds($request: FeedsRequest!) { + value: feeds(request: $request) { + __typename + items { + ...Feed + } + pageInfo { + ...PaginatedResultInfo + } + } + }`, + [FeedFragment, PaginatedResultInfoFragment], +); +export type FeedsRequest = RequestOf; diff --git a/packages/graphql/src/fragments/index.ts b/packages/graphql/src/fragments/index.ts index 8ce6a2d25..cc171dc43 100644 --- a/packages/graphql/src/fragments/index.ts +++ b/packages/graphql/src/fragments/index.ts @@ -7,6 +7,5 @@ export * from './SponsoredTransactionRequest'; export * from './SelfFundedTransactionRequest'; export * from './TransactionWillFail'; export * from './username'; -export * from './namespace'; export * from './media'; export * from './metadata'; diff --git a/packages/graphql/src/fragments/namespace.ts b/packages/graphql/src/fragments/namespace.ts deleted file mode 100644 index 368b03517..000000000 --- a/packages/graphql/src/fragments/namespace.ts +++ /dev/null @@ -1,27 +0,0 @@ -import type { FragmentOf } from 'gql.tada'; -import { graphql } from '../graphql'; - -export const UsernameNamespace = graphql( - `fragment UsernameNamespace on UsernameNamespace { - __typename - address - namespace - createdAt - metadata { - __typename - description - id - } - owner - # TODO: Implement rules - # rules { - # anyOf { - # ...RULE - # } - # required { - # ...RULE - # } - # } - }`, -); -export type UsernameNamespace = FragmentOf; diff --git a/packages/graphql/src/fragments/post.ts b/packages/graphql/src/fragments/post.ts index 69ddb33d1..3f80042b9 100644 --- a/packages/graphql/src/fragments/post.ts +++ b/packages/graphql/src/fragments/post.ts @@ -18,7 +18,7 @@ import { TransactionMetadataFragment, VideoMetadataFragment, } from './metadata'; -import { AppFragment, Feed } from './primitives'; +import { AppFragment, FeedFragment } from './primitives'; export const RecipientDataOutputFragment = graphql( `fragment RecipientDataOutput on RecipientDataOutput { @@ -207,7 +207,7 @@ export const ReferencedPostFragment = graphql( [ AccountFragment, AppFragment, - Feed, + FeedFragment, PostMetadataFragment, PostActionFragment, LoggedInPostOperationsFragment, @@ -264,7 +264,7 @@ export const PostFragment = graphql( [ AccountFragment, AppFragment, - Feed, + FeedFragment, PostMetadataFragment, PostActionFragment, NestedPostFragment, diff --git a/packages/graphql/src/fragments/primitives.ts b/packages/graphql/src/fragments/primitives.ts index 4ceb9ed6d..576a101f4 100644 --- a/packages/graphql/src/fragments/primitives.ts +++ b/packages/graphql/src/fragments/primitives.ts @@ -34,8 +34,78 @@ export const AppFragment = graphql( ); export type App = FragmentOf; -export const Feed = graphql( +export const FeedMetadataFragment = graphql( + `fragment FeedMetadata on FeedMetadata { + __typename + description + id + name + title + }`, +); +export type FeedMetadata = FragmentOf; + +// TODO: Add operations and rules +export const FeedFragment = graphql( `fragment Feed on Feed { - address + __typename + address + createdAt + metadata { + ...FeedMetadata + } + owner + }`, + [FeedMetadataFragment], +); +export type Feed = FragmentOf; + +export const GraphMetadataFragment = graphql( + `fragment GraphMetadata on GraphMetadata { + __typename + description + id + name + title + }`, +); +export type GraphMetadata = FragmentOf; + +// TODO: Add rules +export const GraphFragment = graphql( + `fragment Graph on Graph { + __typename + address + createdAt + metadata { + ...GraphMetadata + } + owner + }`, + [GraphMetadataFragment], +); +export type Graph = FragmentOf; + +export const UsernameNamespaceMetadataFragment = graphql( + `fragment UsernameNamespaceMetadata on UsernameNamespaceMetadata { + __typename + description + id + }`, +); +export type UsernameNamespaceMetadata = FragmentOf; + +// TODO: Add rules +export const UsernameNamespaceFragment = graphql( + `fragment UsernameNamespace on UsernameNamespace { + __typename + address + createdAt + metadata { + ...UsernameNamespaceMetadata + } + owner }`, + [UsernameNamespaceMetadataFragment], ); +export type UsernameNamespace = FragmentOf; diff --git a/packages/graphql/src/fragments/username.ts b/packages/graphql/src/fragments/username.ts index 3a7fcb89b..1bbb80b1b 100644 --- a/packages/graphql/src/fragments/username.ts +++ b/packages/graphql/src/fragments/username.ts @@ -1,6 +1,6 @@ import type { FragmentOf } from 'gql.tada'; import { graphql } from '../graphql'; -import { UsernameNamespace } from './namespace'; +import { UsernameNamespaceFragment } from './primitives'; export const Username = graphql( `fragment Username on Username { @@ -15,6 +15,6 @@ export const Username = graphql( ...UsernameNamespace } }`, - [UsernameNamespace], + [UsernameNamespaceFragment], ); export type Username = FragmentOf; diff --git a/packages/graphql/src/graph.ts b/packages/graphql/src/graph.ts index 2e19d76ed..760290055 100644 --- a/packages/graphql/src/graph.ts +++ b/packages/graphql/src/graph.ts @@ -1,5 +1,10 @@ import type { FragmentOf } from 'gql.tada'; -import { SelfFundedTransactionRequest, TransactionWillFail } from './fragments'; +import { + GraphFragment, + PaginatedResultInfoFragment, + SelfFundedTransactionRequest, + TransactionWillFail, +} from './fragments'; import { type RequestOf, graphql } from './graphql'; const CreateGraphResponse = graphql( @@ -35,3 +40,29 @@ export const CreateGraphMutation = graphql( [CreateGraphResult], ); export type CreateGraphRequest = RequestOf; + +export const GraphQuery = graphql( + `query Graph($request: GraphRequest!) { + value: graph(request: $request) { + ...Graph + } + }`, + [GraphFragment], +); +export type GraphRequest = RequestOf; + +export const GraphsQuery = graphql( + `query Graphs($request: GraphsRequest!) { + value: graphs(request: $request) { + __typename + items { + ...Graph + } + pageInfo { + ...PaginatedResultInfo + } + } + }`, + [GraphFragment, PaginatedResultInfoFragment], +); +export type GraphsRequest = RequestOf; diff --git a/packages/graphql/src/namespace.ts b/packages/graphql/src/namespace.ts index aee15631c..5a8f227d4 100644 --- a/packages/graphql/src/namespace.ts +++ b/packages/graphql/src/namespace.ts @@ -1,5 +1,10 @@ import type { FragmentOf } from 'gql.tada'; -import { SelfFundedTransactionRequest, TransactionWillFail } from './fragments'; +import { + PaginatedResultInfoFragment, + SelfFundedTransactionRequest, + TransactionWillFail, + UsernameNamespaceFragment, +} from './fragments'; import { type RequestOf, graphql } from './graphql'; const CreateNamespaceResponse = graphql( @@ -35,3 +40,29 @@ export const CreateUsernameNamespaceMutation = graphql( [CreateUsernameNamespaceResult], ); export type CreateUsernameNamespaceRequest = RequestOf; + +export const UsernameNamespaceQuery = graphql( + `query UsernameNamespace($request: UsernameNamespaceRequest!) { + value: usernameNamespace(request: $request) { + ...UsernameNamespace + } + }`, + [UsernameNamespaceFragment], +); +export type UsernameNamespaceRequest = RequestOf; + +export const NamespacesQuery = graphql( + `query Namespaces($request: NamespacesRequest!) { + value: namespaces(request: $request) { + __typename + items { + ...UsernameNamespace + } + pageInfo { + ...PaginatedResultInfo + } + } + }`, + [UsernameNamespaceFragment, PaginatedResultInfoFragment], +); +export type NamespacesRequest = RequestOf;