From 5eac56195e8817c9aac5359deca20bfbab59cd18 Mon Sep 17 00:00:00 2001 From: Juan Garcia Date: Wed, 11 Dec 2024 10:23:13 +0100 Subject: [PATCH 1/2] Implement mutations for username --- packages/client/src/actions/index.ts | 4 +- packages/client/src/actions/username.ts | 84 +++++++++++++++ packages/graphql/src/index.ts | 1 + packages/graphql/src/username.ts | 135 ++++++++++++++++++++++++ 4 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 packages/client/src/actions/username.ts create mode 100644 packages/graphql/src/username.ts diff --git a/packages/client/src/actions/index.ts b/packages/client/src/actions/index.ts index d3c97fe25..1ed0020da 100644 --- a/packages/client/src/actions/index.ts +++ b/packages/client/src/actions/index.ts @@ -2,7 +2,9 @@ export * from './account'; export * from './app'; export * from './authentication'; export * from './follow'; +export * from './health'; export * from './post'; export * from './posts'; -export * from './transactions'; export * from './timeline'; +export * from './transactions'; +export * from './username'; diff --git a/packages/client/src/actions/username.ts b/packages/client/src/actions/username.ts new file mode 100644 index 000000000..2ca5dd6b7 --- /dev/null +++ b/packages/client/src/actions/username.ts @@ -0,0 +1,84 @@ +import type { Post, TimelineHighlightsRequest } from '@lens-protocol/graphql'; +import { + AssignUsernameToAccountMutation, + CreateUsernameMutation, + TimelineHighlightsQuery, + UnassignUsernameFromAccountMutation, +} from '@lens-protocol/graphql'; +import type { ResultAsync } from '@lens-protocol/types'; + +import type { CreateUsernameRequest } from '@lens-protocol/graphql'; +import type { CreateUsernameResult } from '@lens-protocol/graphql'; +import type { AssignUsernameToAccountRequest } from '@lens-protocol/graphql'; +import type { AssignUsernameToAccountResult } from '@lens-protocol/graphql'; +import type { UnassignUsernameFromAccountRequest } from '@lens-protocol/graphql'; +import type { UnassignUsernameToAccountResult } from '@lens-protocol/graphql'; +import type { AnyClient, SessionClient } from '../clients'; +import type { UnauthenticatedError, UnexpectedError } from '../errors'; +import type { Paginated } from '../types'; + +/** + * Create a username + * + * ```ts + * const result = await createUsername(sessionClient, { + * username: { + * localName: 'wagmi' + * } + * }); + * ``` + * + * @param client - The session client for the authenticated Account. + * @param request - The mutation request. + * @returns Tiered transaction result. + */ +export function createUsername( + client: SessionClient, + request: CreateUsernameRequest, +): ResultAsync { + return client.mutation(CreateUsernameMutation, { request }); +} + +/** + * Assign a username to the account associated with the authenticated session. + * + * ```ts + * const result = await assignUsernameToAccount(sessionClient, { + * username: { + * localName: 'wagmi' + * } + * }); + * ``` + * + * @param client - The session client for the authenticated Account. + * @param request - The mutation request. + * @returns Tiered transaction result. + */ +export function assignUsernameToAccount( + client: AnyClient, + request: AssignUsernameToAccountRequest, +): ResultAsync { + return client.mutation(AssignUsernameToAccountMutation, { request }); +} + +/** + * Unassign a username to the account associated with the authenticated session. + * + * ```ts + * const result = await unassignUsernameFromAccount(sessionClient, { + * username: { + * localName: 'wagmi' + * } + * }); + * ``` + * + * @param client - The session client for the authenticated Account. + * @param request - The mutation request. + * @returns Tiered transaction result. + */ +export function unassignUsernameFromAccount( + client: AnyClient, + request: UnassignUsernameFromAccountRequest, +): ResultAsync { + return client.query(UnassignUsernameFromAccountMutation, { request }); +} diff --git a/packages/graphql/src/index.ts b/packages/graphql/src/index.ts index a4190f76b..510341bac 100644 --- a/packages/graphql/src/index.ts +++ b/packages/graphql/src/index.ts @@ -10,3 +10,4 @@ export * from './notifications'; export * from './post'; export * from './timeline'; export * from './transactions'; +export * from './username'; diff --git a/packages/graphql/src/username.ts b/packages/graphql/src/username.ts new file mode 100644 index 000000000..2a1fc2c8e --- /dev/null +++ b/packages/graphql/src/username.ts @@ -0,0 +1,135 @@ +import type { FragmentOf } from 'gql.tada'; +import { + SelfFundedTransactionRequest, + SponsoredTransactionRequest, + TransactionWillFail, +} from './fragments'; +import { type RequestOf, graphql } from './graphql'; + +const CreateUsernameResponse = graphql( + `fragment CreateUsernameResponse on CreateUsernameResponse { + __typename + hash + }`, +); +export type CreateUsernameResponse = FragmentOf; + +const CreateUsernameResult = graphql( + `fragment CreateUsernameResult on CreateUsernameResult { + ...on CreateUsernameResponse { + ...CreateUsernameResponse + } + ...on SponsoredTransactionRequest { + ...SponsoredTransactionRequest + } + ...on SelfFundedTransactionRequest { + ...SelfFundedTransactionRequest + } + ...on TransactionWillFail { + ...TransactionWillFail + } + }`, + [ + CreateUsernameResponse, + SelfFundedTransactionRequest, + TransactionWillFail, + SponsoredTransactionRequest, + ], +); +export type CreateUsernameResult = FragmentOf; + +export const CreateUsernameMutation = graphql( + `mutation CreateUsername($request: CreateUsernameRequest!) { + value: createUsername(request: $request) { + ...CreateUsernameResult + } + }`, + [CreateUsernameResult], +); +export type CreateUsernameRequest = RequestOf; + +const AssignUsernameResponse = graphql( + `fragment AssignUsernameResponse on AssignUsernameResponse { + __typename + hash + }`, +); +export type AssignUsernameResponse = FragmentOf; + +const AssignUsernameToAccountResult = graphql( + `fragment AssignUsernameToAccountResult on AssignUsernameToAccountResult { + ...on AssignUsernameResponse { + ...AssignUsernameResponse + } + ...on SponsoredTransactionRequest { + ...SponsoredTransactionRequest + } + ...on SelfFundedTransactionRequest { + ...SelfFundedTransactionRequest + } + ...on TransactionWillFail { + ...TransactionWillFail + } + }`, + [ + SponsoredTransactionRequest, + SelfFundedTransactionRequest, + TransactionWillFail, + AssignUsernameResponse, + ], +); +export type AssignUsernameToAccountResult = FragmentOf; + +export const AssignUsernameToAccountMutation = graphql( + `mutation AssignUsernameToAccount($request: AssignUsernameToAccountRequest!) { + value: assignUsernameToAccount(request: $request) { + ...AssignUsernameToAccountResult + } + }`, + [AssignUsernameToAccountResult], +); +export type AssignUsernameToAccountRequest = RequestOf; + +const UnassignUsernameResponse = graphql( + `fragment UnassignUsernameResponse on UnassignUsernameResponse { + __typename + hash + }`, +); +export type UnassignUsernameResponse = FragmentOf; + +const UnassignUsernameToAccountResult = graphql( + `fragment UnassignUsernameToAccountResult on UnassignUsernameToAccountResult { + ...on UnassignUsernameResponse { + ...UnassignUsernameResponse + } + ...on SponsoredTransactionRequest { + ...SponsoredTransactionRequest + } + ...on SelfFundedTransactionRequest { + ...SelfFundedTransactionRequest + } + ...on TransactionWillFail { + ...TransactionWillFail + } + }`, + [ + SponsoredTransactionRequest, + SelfFundedTransactionRequest, + TransactionWillFail, + UnassignUsernameResponse, + ], +); +export type UnassignUsernameToAccountResult = FragmentOf; + +export const UnassignUsernameFromAccountMutation = graphql( + `mutation LeaveGroup($request: UnassignUsernameFromAccountRequest!) { + value: unassignUsernameFromAccount(request: $request) { + ...UnassignUsernameToAccountResult + } + }`, + [UnassignUsernameToAccountResult], +); +export type UnassignUsernameFromAccountRequest = RequestOf< + typeof UnassignUsernameFromAccountMutation +>; From 54816ab598aca6f1a1a44f95c4ae5580fa712ddd Mon Sep 17 00:00:00 2001 From: Juan Garcia Date: Wed, 11 Dec 2024 10:31:12 +0100 Subject: [PATCH 2/2] Remove not needed imports --- packages/client/src/actions/username.ts | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/packages/client/src/actions/username.ts b/packages/client/src/actions/username.ts index 2ca5dd6b7..82b65b76c 100644 --- a/packages/client/src/actions/username.ts +++ b/packages/client/src/actions/username.ts @@ -1,21 +1,20 @@ -import type { Post, TimelineHighlightsRequest } from '@lens-protocol/graphql'; +import type { + AssignUsernameToAccountRequest, + AssignUsernameToAccountResult, + CreateUsernameRequest, + CreateUsernameResult, + UnassignUsernameFromAccountRequest, + UnassignUsernameToAccountResult, +} from '@lens-protocol/graphql'; import { AssignUsernameToAccountMutation, CreateUsernameMutation, - TimelineHighlightsQuery, UnassignUsernameFromAccountMutation, } from '@lens-protocol/graphql'; import type { ResultAsync } from '@lens-protocol/types'; -import type { CreateUsernameRequest } from '@lens-protocol/graphql'; -import type { CreateUsernameResult } from '@lens-protocol/graphql'; -import type { AssignUsernameToAccountRequest } from '@lens-protocol/graphql'; -import type { AssignUsernameToAccountResult } from '@lens-protocol/graphql'; -import type { UnassignUsernameFromAccountRequest } from '@lens-protocol/graphql'; -import type { UnassignUsernameToAccountResult } from '@lens-protocol/graphql'; -import type { AnyClient, SessionClient } from '../clients'; +import type { SessionClient } from '../clients'; import type { UnauthenticatedError, UnexpectedError } from '../errors'; -import type { Paginated } from '../types'; /** * Create a username @@ -55,7 +54,7 @@ export function createUsername( * @returns Tiered transaction result. */ export function assignUsernameToAccount( - client: AnyClient, + client: SessionClient, request: AssignUsernameToAccountRequest, ): ResultAsync { return client.mutation(AssignUsernameToAccountMutation, { request }); @@ -77,7 +76,7 @@ export function assignUsernameToAccount( * @returns Tiered transaction result. */ export function unassignUsernameFromAccount( - client: AnyClient, + client: SessionClient, request: UnassignUsernameFromAccountRequest, ): ResultAsync { return client.query(UnassignUsernameFromAccountMutation, { request });