From 44a90362d6940806f19ce8ce28da9b85d7a17b1e Mon Sep 17 00:00:00 2001 From: Juan Garcia Date: Tue, 10 Dec 2024 10:59:36 +0100 Subject: [PATCH] Implement group mutations --- packages/client/src/actions/group.ts | 71 +++++++++++++++ packages/client/src/actions/index.ts | 3 +- packages/graphql/src/group.ts | 125 +++++++++++++++++++++++++++ packages/graphql/src/index.ts | 1 + 4 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 packages/client/src/actions/group.ts create mode 100644 packages/graphql/src/group.ts diff --git a/packages/client/src/actions/group.ts b/packages/client/src/actions/group.ts new file mode 100644 index 000000000..6932272d9 --- /dev/null +++ b/packages/client/src/actions/group.ts @@ -0,0 +1,71 @@ +import type { + CreateGroupRequest, + CreateGroupResult, + JoinGroupRequest, + JoinGroupResult, + LeaveGroupRequest, + LeaveGroupResult, +} 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 { UnauthenticatedError, UnexpectedError } from '../errors'; + +/** + * Create a Group + * + * ```ts + * const result = await createGroup(sessionClient); + * ``` + * + * @param client - The session client logged as a builder. + * @param request - The mutation request. + * @returns Tiered transaction result. + */ +export function createGroup( + client: SessionClient, + request: CreateGroupRequest, +): ResultAsync { + return client.mutation(CreateGroupMutation, { request }); +} + +/** + * Join a Group + * + * ```ts + * const result = await joinGroup(sessionClient, { + * group: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'), + * }); + * ``` + * + * @param client - The session client for the authenticated Account. + * @param request - The mutation request. + * @returns Tiered transaction result. + */ +export function joinGroup( + client: SessionClient, + request: JoinGroupRequest, +): ResultAsync { + return client.mutation(JoinGroupMutation, { request }); +} + +/** + * Leave a Group + * + * ```ts + * const result = await leaveGroup(sessionClient, { + * group: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'), + * }); + * ``` + * + * @param client - The session client for the authenticated Account. + * @param request - The mutation request. + * @returns Tiered transaction result. + */ +export function leaveGroup( + client: SessionClient, + request: LeaveGroupRequest, +): ResultAsync { + return client.mutation(LeaveGroupMutation, { request }); +} diff --git a/packages/client/src/actions/index.ts b/packages/client/src/actions/index.ts index d3c97fe25..63b2af8d1 100644 --- a/packages/client/src/actions/index.ts +++ b/packages/client/src/actions/index.ts @@ -2,7 +2,8 @@ export * from './account'; export * from './app'; export * from './authentication'; export * from './follow'; +export * from './group'; export * from './post'; export * from './posts'; -export * from './transactions'; export * from './timeline'; +export * from './transactions'; diff --git a/packages/graphql/src/group.ts b/packages/graphql/src/group.ts new file mode 100644 index 000000000..848d58e49 --- /dev/null +++ b/packages/graphql/src/group.ts @@ -0,0 +1,125 @@ +import type { FragmentOf } from 'gql.tada'; +import { + SelfFundedTransactionRequest, + SponsoredTransactionRequest, + TransactionWillFail, +} from './fragments'; +import { type RequestOf, graphql } from './graphql'; + +const CreateGroupResponse = graphql( + `fragment CreateGroupResponse on CreateGroupResponse { + __typename + hash + }`, +); +export type CreateGroupResponse = FragmentOf; + +const CreateGroupResult = graphql( + `fragment CreateGroupResult on CreateGroupResult { + ...on CreateGroupResponse { + ...CreateGroupResponse + } + ...on SelfFundedTransactionRequest { + ...SelfFundedTransactionRequest + } + ...on TransactionWillFail { + ...TransactionWillFail + } + }`, + [CreateGroupResponse, SelfFundedTransactionRequest, TransactionWillFail], +); +export type CreateGroupResult = FragmentOf; + +export const CreateGroupMutation = graphql( + `mutation CreateGroup($request: CreateGroupRequest!) { + value: createGroup(request: $request) { + ...CreateGroupResult + } + }`, + [CreateGroupResult], +); +export type CreateGroupRequest = RequestOf; + +const JoinGroupResponse = graphql( + `fragment JoinGroupResponse on JoinGroupResponse { + __typename + hash + }`, +); +export type JoinGroupResponse = FragmentOf; + +const JoinGroupResult = graphql( + `fragment JoinGroupResult on JoinGroupResult { + ...on JoinGroupResponse { + ...JoinGroupResponse + } + ...on SponsoredTransactionRequest { + ...SponsoredTransactionRequest + } + ...on SelfFundedTransactionRequest { + ...SelfFundedTransactionRequest + } + ...on TransactionWillFail { + ...TransactionWillFail + } + }`, + [ + SponsoredTransactionRequest, + SelfFundedTransactionRequest, + TransactionWillFail, + JoinGroupResponse, + ], +); +export type JoinGroupResult = FragmentOf; + +export const JoinGroupMutation = graphql( + `mutation JoinGroup($request: JoinGroupRequest!) { + value: joinGroup(request: $request) { + ...JoinGroupResult + } + }`, + [JoinGroupResult], +); +export type JoinGroupRequest = RequestOf; + +const LeaveGroupResponse = graphql( + `fragment LeaveGroupResponse on LeaveGroupResponse { + __typename + hash + }`, +); +export type LeaveGroupResponse = FragmentOf; + +const LeaveGroupResult = graphql( + `fragment LeaveGroupResult on LeaveGroupResult { + ...on LeaveGroupResponse { + ...LeaveGroupResponse + } + ...on SponsoredTransactionRequest { + ...SponsoredTransactionRequest + } + ...on SelfFundedTransactionRequest { + ...SelfFundedTransactionRequest + } + ...on TransactionWillFail { + ...TransactionWillFail + } + }`, + [ + SponsoredTransactionRequest, + SelfFundedTransactionRequest, + TransactionWillFail, + LeaveGroupResponse, + ], +); +export type LeaveGroupResult = FragmentOf; + +export const LeaveGroupMutation = graphql( + `mutation LeaveGroup($request: LeaveGroupRequest!) { + value: leaveGroup(request: $request) { + ...LeaveGroupResult + } + }`, + [LeaveGroupResult], +); +export type LeaveGroupRequest = RequestOf; diff --git a/packages/graphql/src/index.ts b/packages/graphql/src/index.ts index a4190f76b..1ae6e7d74 100644 --- a/packages/graphql/src/index.ts +++ b/packages/graphql/src/index.ts @@ -5,6 +5,7 @@ export * from './enums'; export * from './follow'; export * from './fragments'; export * from './graphql'; +export * from './group'; export * from './health'; export * from './notifications'; export * from './post';