Skip to content

Commit

Permalink
Add group queries
Browse files Browse the repository at this point in the history
  • Loading branch information
juangm committed Dec 13, 2024
1 parent d36d021 commit 88259cc
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 2 deletions.
98 changes: 96 additions & 2 deletions packages/client/src/actions/group.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -69,3 +85,81 @@ export function leaveGroup(
): ResultAsync<LeaveGroupResult, UnexpectedError | UnauthenticatedError> {
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<Group | null, UnexpectedError> {
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<Paginated<Group> | 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<Paginated<Account>, 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<GroupStatsResponse | null, UnexpectedError> {
return client.query(GroupStatsQuery, { request });
}
1 change: 1 addition & 0 deletions packages/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
28 changes: 28 additions & 0 deletions packages/graphql/src/fragments/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof GroupMetadataFragment>;

// TODO: add GroupRulesConfig
export const GroupFragment = graphql(
`fragment Group on Group {
__typename
address
timestamp
metadata {
...GroupMetadata
}
isMember
owner
}`,
[GroupMetadataFragment],
);
export type Group = FragmentOf<typeof GroupFragment>;
63 changes: 63 additions & 0 deletions packages/graphql/src/group.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { FragmentOf } from 'gql.tada';
import {
AccountFragment,
GroupFragment,
PaginatedResultInfoFragment,
SelfFundedTransactionRequest,
SponsoredTransactionRequest,
TransactionWillFail,
Expand Down Expand Up @@ -123,3 +126,63 @@ export const LeaveGroupMutation = graphql(
[LeaveGroupResult],
);
export type LeaveGroupRequest = RequestOf<typeof LeaveGroupMutation>;

export const GroupQuery = graphql(
`query Group($request: GroupRequest!) {
value: group(request: $request) {
...Group
}
}`,
[GroupFragment],
);
export type GroupRequest = RequestOf<typeof GroupQuery>;

export const GroupsQuery = graphql(
`query Groups($request: GroupsRequest!) {
value: groups(request: $request) {
__typename
items {
...Group
}
pageInfo {
...PaginatedResultInfo
}
}
}`,
[GroupFragment, PaginatedResultInfoFragment],
);
export type GroupsRequest = RequestOf<typeof GroupsQuery>;

export const GroupMembersQuery = graphql(
`query GroupMembers($request: GroupMembersRequest!) {
value: groupMembers(request: $request) {
__typename
items {
...Account
}
pageInfo {
...PaginatedResultInfo
}
}
}`,
[AccountFragment, PaginatedResultInfoFragment],
);
export type GroupMembersRequest = RequestOf<typeof GroupMembersQuery>;

export const GroupStatsResponseFragment = graphql(
`fragment GroupStatsResponse on GroupStatsResponse {
__typename
totalMembers
}`,
);
export type GroupStatsResponse = FragmentOf<typeof GroupStatsResponseFragment>;

export const GroupStatsQuery = graphql(
`query GroupStats($request: GroupStatsRequest!) {
value: groupStats(request: $request) {
...GroupStatsResponse
}
}`,
[GroupStatsResponseFragment],
);
export type GroupStatsRequest = RequestOf<typeof GroupStatsQuery>;

0 comments on commit 88259cc

Please sign in to comment.