From 88259cc9ddb0f0f2ef38155b48a3c4163acfc38c Mon Sep 17 00:00:00 2001 From: Juan Garcia Date: Fri, 13 Dec 2024 13:28:42 +0100 Subject: [PATCH] Add group queries --- packages/client/src/actions/group.ts | 98 +++++++++++++++++++- packages/graphql/package.json | 1 + packages/graphql/src/fragments/primitives.ts | 28 ++++++ packages/graphql/src/group.ts | 63 +++++++++++++ 4 files changed, 188 insertions(+), 2 deletions(-) diff --git a/packages/client/src/actions/group.ts b/packages/client/src/actions/group.ts index 6932272d9..a84735805 100644 --- a/packages/client/src/actions/group.ts +++ b/packages/client/src/actions/group.ts @@ -1,15 +1,31 @@ import type { CreateGroupRequest, CreateGroupResult, + Group, + GroupMembersRequest, + GroupRequest, + GroupStatsRequest, + GroupStatsResponse, + GroupsRequest, JoinGroupRequest, JoinGroupResult, LeaveGroupRequest, LeaveGroupResult, + Paginated, +} from '@lens-protocol/graphql'; +import { + CreateGroupMutation, + GroupMembersQuery, + GroupQuery, + GroupStatsQuery, + GroupsQuery, + JoinGroupMutation, + LeaveGroupMutation, } from '@lens-protocol/graphql'; -import { CreateGroupMutation, JoinGroupMutation, LeaveGroupMutation } from '@lens-protocol/graphql'; import type { ResultAsync } from '@lens-protocol/types'; -import type { SessionClient } from '../clients'; +import type { Account } from '@lens-protocol/graphql'; +import type { AnyClient, SessionClient } from '../clients'; import type { UnauthenticatedError, UnexpectedError } from '../errors'; /** @@ -69,3 +85,81 @@ export function leaveGroup( ): ResultAsync { return client.mutation(LeaveGroupMutation, { request }); } + +/** + * Fetch a Group. + * + * ```ts + * const result = await fetchGroup(anyClient, { + * group: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'), + * }); + * ``` + * + * @param client - Any Lens client. + * @param request - The query request. + * @returns The Group or `null` if it does not exist. + */ +export function fetchGroup( + client: AnyClient, + request: GroupRequest, +): ResultAsync { + return client.query(GroupQuery, { request }); +} + +/** + * Fetch groups. + * + * ```ts + * const result = await fetchGroups(anyClient); + * ``` + * + * @param client - Any Lens client. + * @param request - The query request. + * @returns The list of groups. + */ +export function fetchGroups( + client: AnyClient, + request: GroupsRequest = {}, +): ResultAsync | null, UnexpectedError> { + return client.query(GroupsQuery, { request }); +} + +/** + * Fetch group members. + * + * ```ts + * const result = await fetchGroupMembers(anyClient, { + * group: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'), + * }); + * ``` + * + * @param client - Any Lens client. + * @param request - The query request. + * @returns The list of members for the group. + */ +export function fetchGroupMembers( + client: AnyClient, + request: GroupMembersRequest, +): ResultAsync, UnexpectedError> { + return client.query(GroupMembersQuery, { request }); +} + +/** + * Fetch stats for a group. + * + * ```ts + * const result = await fetchGroupStats(anyClient, { + * group: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'), + * }); + * ``` + * + * @param client - Any Lens client. + * @param request - The query request. + * @returns The stats for the group. + */ +export function fetchGroupStats( + client: AnyClient, + request: GroupStatsRequest, +): ResultAsync { + return client.query(GroupStatsQuery, { request }); +} diff --git a/packages/graphql/package.json b/packages/graphql/package.json index cacb3e6d8..3de01d7c1 100644 --- a/packages/graphql/package.json +++ b/packages/graphql/package.json @@ -29,6 +29,7 @@ "build": "tsup", "check": "gql.tada check", "doctor": "gql.tada doctor", + "gql:genereate:cache": "gql.tada generate turbo", "gql:download:local": "gql-tada generate schema 'http://localhost:3000/graphql' --output './schema.graphql'", "gql:download:staging": "gql-tada generate schema 'https://api.staging.lens.dev/graphql' --output './schema.graphql'", "gql:generate": "gql-tada generate output" diff --git a/packages/graphql/src/fragments/primitives.ts b/packages/graphql/src/fragments/primitives.ts index 4ceb9ed6d..3bb9abff2 100644 --- a/packages/graphql/src/fragments/primitives.ts +++ b/packages/graphql/src/fragments/primitives.ts @@ -39,3 +39,31 @@ export const Feed = graphql( address }`, ); + +export const GroupMetadataFragment = graphql( + `fragment GroupMetadata on GroupMetadata { + __typename + description + id + icon + name + coverPicture + }`, +); +export type GroupMetadata = FragmentOf; + +// TODO: add GroupRulesConfig +export const GroupFragment = graphql( + `fragment Group on Group { + __typename + address + timestamp + metadata { + ...GroupMetadata + } + isMember + owner + }`, + [GroupMetadataFragment], +); +export type Group = FragmentOf; diff --git a/packages/graphql/src/group.ts b/packages/graphql/src/group.ts index 848d58e49..c82cd1136 100644 --- a/packages/graphql/src/group.ts +++ b/packages/graphql/src/group.ts @@ -1,5 +1,8 @@ import type { FragmentOf } from 'gql.tada'; import { + AccountFragment, + GroupFragment, + PaginatedResultInfoFragment, SelfFundedTransactionRequest, SponsoredTransactionRequest, TransactionWillFail, @@ -123,3 +126,63 @@ export const LeaveGroupMutation = graphql( [LeaveGroupResult], ); export type LeaveGroupRequest = RequestOf; + +export const GroupQuery = graphql( + `query Group($request: GroupRequest!) { + value: group(request: $request) { + ...Group + } + }`, + [GroupFragment], +); +export type GroupRequest = RequestOf; + +export const GroupsQuery = graphql( + `query Groups($request: GroupsRequest!) { + value: groups(request: $request) { + __typename + items { + ...Group + } + pageInfo { + ...PaginatedResultInfo + } + } + }`, + [GroupFragment, PaginatedResultInfoFragment], +); +export type GroupsRequest = RequestOf; + +export const GroupMembersQuery = graphql( + `query GroupMembers($request: GroupMembersRequest!) { + value: groupMembers(request: $request) { + __typename + items { + ...Account + } + pageInfo { + ...PaginatedResultInfo + } + } + }`, + [AccountFragment, PaginatedResultInfoFragment], +); +export type GroupMembersRequest = RequestOf; + +export const GroupStatsResponseFragment = graphql( + `fragment GroupStatsResponse on GroupStatsResponse { + __typename + totalMembers + }`, +); +export type GroupStatsResponse = FragmentOf; + +export const GroupStatsQuery = graphql( + `query GroupStats($request: GroupStatsRequest!) { + value: groupStats(request: $request) { + ...GroupStatsResponse + } + }`, + [GroupStatsResponseFragment], +); +export type GroupStatsRequest = RequestOf;