Skip to content

Commit

Permalink
Merge pull request #1033 from lens-protocol/juan/T-23328-js-action-im…
Browse files Browse the repository at this point in the history
…plement-ml-queries

Add ML queries/mutations
  • Loading branch information
juangm authored Feb 21, 2025
2 parents a55e2a7 + 5894ede commit 3b960c3
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/client/src/actions/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ export function removeAppSigners(
*
* ```ts
* const result = await setAppGraph(sessionClient, {
* graph: graph: { globalGraph: true },
* graph: { globalGraph: true },
* app: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'),
* });
* ```
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from './graph';
export * from './group';
export * from './health';
export * from './namespace';
export * from './ml';
export * from './notifications';
export * from './post';
export * from './posts';
Expand Down
141 changes: 141 additions & 0 deletions packages/client/src/actions/ml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import type {
Account,
DismissRecommendedAccountsRequest,
MlAccountRecommendationsRequest,
MlPostsExploreRequest,
MlPostsForYouRequest,
Paginated,
Post,
PostForYou,
PostNotInterestedRequest,
} from '@lens-protocol/graphql';
import {
AddPostNotInterestedMutation,
MlAccountRecommendationsQuery,
MlDismissRecommendedAccountsMutation,
MlPostsExploreQuery,
MlPostsForYouQuery,
UndoPostNotInterestedMutation,
} from '@lens-protocol/graphql';
import type { ResultAsync } from '@lens-protocol/types';

import type { AnyClient, SessionClient } from '../clients';
import type { UnauthenticatedError, UnexpectedError } from '../errors';

/**
* Fetch account recommendations from ML.
*
* ```ts
* const result = await fetchAccountRecommendations(anyClient, {
* account: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'),
* });
* ```
*
* @param client - Any Lens client.
* @param request - The query request.
* @returns The list accounts recommended.
*/
export function fetchAccountRecommendations(
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 });
}

/**
* Dismiss a recommended account.
*
* ```ts
* const result = await dismissRecommendedAccount(sessionClient, {
* accounts: [evmAddress('0xe2f2...')],
* });
* ```
*
* @param client - Session Lens client.
* @param request - The list of accounts to dismiss.
* @returns - void
*/
export function dismissRecommendedAccount(
client: SessionClient,
request: DismissRecommendedAccountsRequest,
): ResultAsync<void, UnexpectedError | UnauthenticatedError> {
return client.mutation(MlDismissRecommendedAccountsMutation, { request });
}

/**
* Flag a post as not of interest.
*
* ```ts
* const result = await addPostNotInterested(sessionClient, {
* post: postID('34fdasd...'),
* });
* ```
*
* @param client - Session Lens client.
* @param request - The post to add as not interested.
* @returns - void
*/
export function addPostNotInterested(
client: SessionClient,
request: PostNotInterestedRequest,
): ResultAsync<void, UnexpectedError | UnauthenticatedError> {
return client.mutation(AddPostNotInterestedMutation, { request });
}

/**
* Undo a previous decision to flag a post as uninteresting.
*
* ```ts
* const result = await undoPostNotInterested(sessionClient, {
* post: postID('34fdasd...'),
* });
* ```
*
* @param client - Session Lens client.
* @param request - The post to remove as not interested.
* @returns - void
*/
export function undoPostNotInterested(
client: SessionClient,
request: PostNotInterestedRequest,
): ResultAsync<void, UnexpectedError | UnauthenticatedError> {
return client.mutation(UndoPostNotInterestedMutation, { request });
}
2 changes: 1 addition & 1 deletion packages/client/src/actions/sponsorship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export function fetchSponsorshipSigners(
*
* @param client - Any Lens client.
* @param request - The query request.
* @returns The paginated list of Sponsorship Signers.
* @returns The paginated list of excluded addresses.
*/
export function fetchSponsorshipLimitExclusions(
client: AnyClient,
Expand Down
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
84 changes: 84 additions & 0 deletions packages/graphql/src/ml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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>;

export const MlDismissRecommendedAccountsMutation = graphql(
`mutation MlDismissRecommendedAccounts($request: DismissRecommendedAccountsRequest!) {
value: mlDismissRecommendedAccounts(request: $request)
}`,
);
export type DismissRecommendedAccountsRequest = RequestOf<
typeof MlDismissRecommendedAccountsMutation
>;

export const AddPostNotInterestedMutation = graphql(
`mutation AddPostNotInterested($request: PostNotInterestedRequest!) {
value: addPostNotInterested(request: $request)
}`,
);
export const UndoPostNotInterestedMutation = graphql(
`mutation UndoPostNotInterested($request: PostNotInterestedRequest!) {
value: undoPostNotInterested(request: $request)
}`,
);
export type PostNotInterestedRequest = RequestOf<typeof AddPostNotInterestedMutation>;

0 comments on commit 3b960c3

Please sign in to comment.