Skip to content

Commit

Permalink
Add ML queries
Browse files Browse the repository at this point in the history
  • Loading branch information
juangm committed Feb 12, 2025
1 parent 04d1e74 commit f89e8be
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/client/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ export * from './feed';
export * from './follow';
export * from './graph';
export * from './group';
export * from './graph';
export * from './group';
export * from './health';
export * from './ml';
export * from './namespace';
export * from './ml';
export * from './notifications';
export * from './post';
export * from './posts';
Expand Down
76 changes: 76 additions & 0 deletions packages/client/src/actions/ml.ts
Original file line number Diff line number Diff line change
@@ -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<Paginated<Account> | 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<Paginated<PostForYou> | 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<Paginated<Post> | null, UnexpectedError> {
return client.query(MlPostsExploreQuery, { request });
}
1 change: 1 addition & 0 deletions packages/graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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';
Expand Down
63 changes: 63 additions & 0 deletions packages/graphql/src/ml.ts
Original file line number Diff line number Diff line change
@@ -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<typeof MlAccountRecommendationsQuery>;

export const PostForYouFragment = graphql(
`fragment PostForYou on PostForYou {
__typename
post {
...Post
}
source
}`,
[PostFragment],
);
export type PostForYou = FragmentOf<typeof PostForYouFragment>;

export const MlPostsForYouQuery = graphql(
`query MlPostsForYou($request: MlpostsForYouRequest!) {
value: mlPostsForYou(request: $request) {
__typename
items {
...PostForYou
}
pageInfo {
...PaginatedResultInfo
}
}
}`,
[PostForYouFragment, PaginatedResultInfoFragment],
);
export type MlPostsForYouRequest = RequestOf<typeof MlPostsForYouQuery>;

export const MlPostsExploreQuery = graphql(
`query MlPostsExplore($request: MlexplorePostsRequest!) {
value: mlPostsExplore(request: $request) {
__typename
items {
...Post
}
pageInfo {
...PaginatedResultInfo
}
}
}`,
[PostFragment, PaginatedResultInfoFragment],
);
export type MlPostsExploreRequest = RequestOf<typeof MlPostsExploreQuery>;

0 comments on commit f89e8be

Please sign in to comment.