Skip to content

Commit

Permalink
Implement follow queries
Browse files Browse the repository at this point in the history
  • Loading branch information
juangm committed Dec 4, 2024
1 parent 2fe3769 commit a9ce53b
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 2 deletions.
101 changes: 99 additions & 2 deletions packages/client/src/actions/follow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,26 @@ import type {
FollowResult,
UnfollowResult,
} from '@lens-protocol/graphql';
import { FollowMutation, UnfollowMutation } from '@lens-protocol/graphql';
import {
FollowMutation,
FollowStatusQuery,
FollowersQuery,
FollowersYouKnowQuery,
FollowingQuery,
UnfollowMutation,
} from '@lens-protocol/graphql';
import type { ResultAsync } from '@lens-protocol/types';

import type { SessionClient } from '../clients';
import type { FollowersRequest } from '@lens-protocol/graphql';
import type { Follower } from '@lens-protocol/graphql';
import type { FollowingRequest } from '@lens-protocol/graphql';
import type { Following } from '@lens-protocol/graphql';
import type { FollowersYouKnowRequest } from '@lens-protocol/graphql';
import type { FollowStatusRequest } from '@lens-protocol/graphql';
import type { FollowStatusResult } from '@lens-protocol/graphql';
import type { AnyClient, SessionClient } from '../clients';
import type { UnauthenticatedError, UnexpectedError } from '../errors';
import type { Paginated } from '../types';

/**
* Follow an Account
Expand Down Expand Up @@ -49,3 +64,85 @@ export function unfollow(
): ResultAsync<UnfollowResult, UnexpectedError | UnauthenticatedError> {
return client.mutation(UnfollowMutation, { request });
}

/**
* Fetch followers accounts.
*
* ```ts
* const result = await fetchFollowers(anyClient, {
* account: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'),
* });
* ```
*
* @param client - Any Lens client.
* @param request - The query request.
* @returns List of followers accounts.
*/
export function fetchFollowers(
client: AnyClient,
request: FollowersRequest,
): ResultAsync<Paginated<Follower>, UnexpectedError> {
return client.query(FollowersQuery, { request });
}

/**
* Fetch accounts following.
*
* ```ts
* const result = await fetchFollowing(anyClient, {
* account: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'),
* });
* ```
*
* @param client - Any Lens client.
* @param request - The query request.
* @returns List of accounts following.
*/
export function fetchFollowing(
client: AnyClient,
request: FollowingRequest,
): ResultAsync<Paginated<Following>, UnexpectedError> {
return client.query(FollowingQuery, { request });
}

/**
* Fetch accounts following.
*
* ```ts
* const result = await fetchFollowersYouKnow(sessionClient, {
* observer: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'),
* target: evmAddress('0xe5439696f4057aF073c0FB2dc6e5e755392922e1'),
* });
* ```
*
* @param client - The session client for the authenticated Account.
* @param request - The query request.
* @returns List of accounts following.
*/
export function fetchFollowersYouKnow(
client: SessionClient,
request: FollowersYouKnowRequest,
): ResultAsync<Paginated<Follower>, UnexpectedError> {
return client.query(FollowersYouKnowQuery, { request });
}

/**
* Fetch follow status.
*
* ```ts
* const result = await fetchFollowStatus(anyClient, {
* account: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'),
* follower: evmAddress('0xe5439696f4057aF073c0FB2dc6e5e755392922e1'),
* });
* ```
*
* @param client - Any Lens client.
* @param request - The query request.
* @returns Status of the follow action.
*/
export function fetchFollowStatus(
client: AnyClient,
request: FollowStatusRequest,
): ResultAsync<FollowStatusResult[], UnexpectedError> {
return client.query(FollowStatusQuery, { request });
}
99 changes: 99 additions & 0 deletions packages/graphql/src/follow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { FragmentOf } from 'gql.tada';

import {
Account,
BooleanValue,
PaginatedResultInfo,
SelfFundedTransactionRequest,
SponsoredTransactionRequest,
TransactionWillFail,
Expand Down Expand Up @@ -85,3 +88,99 @@ export const UnfollowMutation = graphql(
[UnfollowResult],
);
export type CreateUnfollowRequest = RequestOf<typeof UnfollowMutation>;

const Follower = graphql(
`fragment Follower on Follower {
__typename
follower {
...Account
}
followedOn
}`,
[Account],
);
export type Follower = FragmentOf<typeof Follower>;

const Following = graphql(
`fragment Following on Following {
__typename
following {
...Account
}
followedOn
}`,
[Account],
);
export type Following = FragmentOf<typeof Following>;

export const FollowersQuery = graphql(
`query Followers ($request: FollowersRequest!) {
value: followers(request: $request) {
__typename
items {
...Follower
}
pageInfo {
...PaginatedResultInfo
}
}
}`,
[Follower, PaginatedResultInfo],
);
export type FollowersRequest = RequestOf<typeof FollowersQuery>;

export const FollowingQuery = graphql(
`query Following ($request: FollowingRequest!) {
value: following(request: $request) {
__typename
items {
...Following
}
pageInfo {
...PaginatedResultInfo
}
}
}`,
[Following, PaginatedResultInfo],
);
export type FollowingRequest = RequestOf<typeof FollowingQuery>;

export const FollowersYouKnowQuery = graphql(
`query FollowersYouKnow ($request: FollowersYouKnowRequest!) {
value: followersYouKnow(request: $request) {
__typename
items {
...Follower
}
pageInfo {
...PaginatedResultInfo
}
}
}`,
[Follower, PaginatedResultInfo],
);
export type FollowersYouKnowRequest = RequestOf<typeof FollowersYouKnowQuery>;

const FollowStatusResult = graphql(
`fragment FollowStatusResult on FollowStatusResult {
__typename
graph
follower
account
isFollowing {
...BooleanValue
}
}`,
[BooleanValue],
);
export type FollowStatusResult = FragmentOf<typeof FollowStatusResult>;

export const FollowStatusQuery = graphql(
`query FollowStatus ($request: FollowStatusRequest!) {
value: followStatus(request: $request) {
...FollowStatusResult
}
}`,
[FollowStatusResult],
);
export type FollowStatusRequest = RequestOf<typeof FollowStatusQuery>;

0 comments on commit a9ce53b

Please sign in to comment.