diff --git a/packages/client/src/actions/index.ts b/packages/client/src/actions/index.ts index b2203d3ff..d790a8619 100644 --- a/packages/client/src/actions/index.ts +++ b/packages/client/src/actions/index.ts @@ -4,10 +4,11 @@ export * from './app'; export * from './authentication'; export * from './feed'; export * from './follow'; -export * from './health'; export * from './graph'; -export * from './namespace'; export * from './group'; +export * from './health'; +export * from './ml'; +export * from './namespace'; export * from './post'; export * from './posts'; export * from './timeline'; diff --git a/packages/client/src/actions/ml.ts b/packages/client/src/actions/ml.ts new file mode 100644 index 000000000..10b6a05fe --- /dev/null +++ b/packages/client/src/actions/ml.ts @@ -0,0 +1,76 @@ +import type { + Account, + MlAccountRecommendationsRequest, + MlPostsExploreRequest, + MlPostsForYouRequest, + Paginated, + Post, + PostForYou, +} from '@lens-protocol/graphql'; +import { + MlAccountRecommendationsQuery, + MlPostsExploreQuery, + MlPostsForYouQuery, +} from '@lens-protocol/graphql'; +import type { ResultAsync } from '@lens-protocol/types'; + +import type { AnyClient } from '../clients'; +import type { UnexpectedError } from '../errors'; + +/** + * Fetch account recommendations from ML. + * + * ```ts + * const result = await fetchMlAccountRecommendations(anyClient, { + * account: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'), + * }); + * ``` + * + * @param client - Any Lens client. + * @param request - The query request. + * @returns The list accounts recommended. + */ +export function fetchMlAccountRecommendations( + client: AnyClient, + request: MlAccountRecommendationsRequest, +): ResultAsync | null, UnexpectedError> { + return client.query(MlAccountRecommendationsQuery, { request }); +} + +/** + * Fetch posts for you from ML. + * + * ```ts + * const result = await fetchPostsForYou(anyClient, { + * account: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'), + * }); + * ``` + * + * @param client - Any Lens client. + * @param request - The query request. + * @returns The list of recommended posts. + */ +export function fetchPostsForYou( + client: AnyClient, + request: MlPostsForYouRequest, +): ResultAsync | null, UnexpectedError> { + return client.query(MlPostsForYouQuery, { request }); +} + +/** + * Fetch posts to explore. + * + * ```ts + * const result = await fetchPostsToExplore(anyClient); + * ``` + * + * @param client - Any Lens client. + * @param request - The query request. + * @returns The list of posts to explore. + */ +export function fetchPostsToExplore( + client: AnyClient, + request: MlPostsExploreRequest, +): ResultAsync | null, UnexpectedError> { + return client.query(MlPostsExploreQuery, { request }); +} diff --git a/packages/graphql/src/index.ts b/packages/graphql/src/index.ts index 3b69bd8a4..50926a1c1 100644 --- a/packages/graphql/src/index.ts +++ b/packages/graphql/src/index.ts @@ -11,6 +11,7 @@ export * from './graph'; export * from './graphql'; export * from './group'; export * from './health'; +export * from './ml'; export * from './namespace'; export * from './notifications'; export * from './post'; diff --git a/packages/graphql/src/ml.ts b/packages/graphql/src/ml.ts new file mode 100644 index 000000000..584974467 --- /dev/null +++ b/packages/graphql/src/ml.ts @@ -0,0 +1,63 @@ +import type { FragmentOf } from 'gql.tada'; +import { AccountFragment, PaginatedResultInfoFragment, PostFragment } from './fragments'; +import { type RequestOf, graphql } from './graphql'; + +export const MlAccountRecommendationsQuery = graphql( + `query MlAccountRecommendations($request: MlaccountRecommendationsRequest!) { + value: mlAccountRecommendations(request: $request) { + __typename + items { + ...Account + } + pageInfo { + ...PaginatedResultInfo + } + } + }`, + [AccountFragment, PaginatedResultInfoFragment], +); +export type MlAccountRecommendationsRequest = RequestOf; + +export const PostForYouFragment = graphql( + `fragment PostForYou on PostForYou { + __typename + post { + ...Post + } + source + }`, + [PostFragment], +); +export type PostForYou = FragmentOf; + +export const MlPostsForYouQuery = graphql( + `query MlPostsForYou($request: MlpostsForYouRequest!) { + value: mlPostsForYou(request: $request) { + __typename + items { + ...PostForYou + } + pageInfo { + ...PaginatedResultInfo + } + } + }`, + [PostForYouFragment, PaginatedResultInfoFragment], +); +export type MlPostsForYouRequest = RequestOf; + +export const MlPostsExploreQuery = graphql( + `query MlPostsExplore($request: MlexplorePostsRequest!) { + value: mlPostsExplore(request: $request) { + __typename + items { + ...Post + } + pageInfo { + ...PaginatedResultInfo + } + } + }`, + [PostFragment, PaginatedResultInfoFragment], +); +export type MlPostsExploreRequest = RequestOf;