Skip to content

Commit

Permalink
Merge pull request #1009 from lens-protocol/T-23255/js-actions-add-us…
Browse files Browse the repository at this point in the history
…ername-mutations

Implement mutations for username
  • Loading branch information
juangm authored Dec 12, 2024
2 parents 129ae5a + ae5ee42 commit 3fec089
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/client/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './app';
export * from './authentication';
export * from './feed';
export * from './follow';
export * from './health';
export * from './graph';
export * from './namespace';
export * from './group';
Expand Down
90 changes: 87 additions & 3 deletions packages/client/src/actions/username.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,95 @@
import type { Username, UsernameRequest, UsernamesRequest } from '@lens-protocol/graphql';
import { UsernameQuery, UsernamesQuery } from '@lens-protocol/graphql';
import type {
AssignUsernameToAccountRequest,
AssignUsernameToAccountResult,
CreateUsernameRequest,
CreateUsernameResult,
UnassignUsernameFromAccountRequest,
UnassignUsernameToAccountResult,
Username,
UsernameRequest,
UsernamesRequest,
} from '@lens-protocol/graphql';
import {
AssignUsernameToAccountMutation,
CreateUsernameMutation,
UnassignUsernameFromAccountMutation,
UsernameQuery,
UsernamesQuery,
} from '@lens-protocol/graphql';
import type { ResultAsync } from '@lens-protocol/types';

import type { SessionClient } from '../clients';
import type { UnauthenticatedError, UnexpectedError } from '../errors';

import type { AnyClient } from '../clients';
import type { 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<CreateUsernameResult, UnexpectedError | UnauthenticatedError> {
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: SessionClient,
request: AssignUsernameToAccountRequest,
): ResultAsync<AssignUsernameToAccountResult, UnauthenticatedError | UnexpectedError> {
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: SessionClient,
request: UnassignUsernameFromAccountRequest,
): ResultAsync<UnassignUsernameToAccountResult, UnauthenticatedError | UnexpectedError> {
return client.query(UnassignUsernameFromAccountMutation, { request });
}

/**
* Fetch username details.
*
Expand Down
137 changes: 136 additions & 1 deletion packages/graphql/src/username.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,141 @@
import { PaginatedResultInfo, Username } from './fragments';
import type { FragmentOf } from 'gql.tada';
import {
PaginatedResultInfo,
SelfFundedTransactionRequest,
SponsoredTransactionRequest,
TransactionWillFail,
Username,
} from './fragments';
import { type RequestOf, graphql } from './graphql';

const CreateUsernameResponse = graphql(
`fragment CreateUsernameResponse on CreateUsernameResponse {
__typename
hash
}`,
);
export type CreateUsernameResponse = FragmentOf<typeof CreateUsernameResponse>;

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<typeof CreateUsernameResult>;

export const CreateUsernameMutation = graphql(
`mutation CreateUsername($request: CreateUsernameRequest!) {
value: createUsername(request: $request) {
...CreateUsernameResult
}
}`,
[CreateUsernameResult],
);
export type CreateUsernameRequest = RequestOf<typeof CreateUsernameMutation>;

const AssignUsernameResponse = graphql(
`fragment AssignUsernameResponse on AssignUsernameResponse {
__typename
hash
}`,
);
export type AssignUsernameResponse = FragmentOf<typeof AssignUsernameResponse>;

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<typeof AssignUsernameToAccountResult>;

export const AssignUsernameToAccountMutation = graphql(
`mutation AssignUsernameToAccount($request: AssignUsernameToAccountRequest!) {
value: assignUsernameToAccount(request: $request) {
...AssignUsernameToAccountResult
}
}`,
[AssignUsernameToAccountResult],
);
export type AssignUsernameToAccountRequest = RequestOf<typeof AssignUsernameToAccountMutation>;

const UnassignUsernameResponse = graphql(
`fragment UnassignUsernameResponse on UnassignUsernameResponse {
__typename
hash
}`,
);
export type UnassignUsernameResponse = FragmentOf<typeof UnassignUsernameResponse>;

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<typeof UnassignUsernameToAccountResult>;

export const UnassignUsernameFromAccountMutation = graphql(
`mutation LeaveGroup($request: UnassignUsernameFromAccountRequest!) {
value: unassignUsernameFromAccount(request: $request) {
...UnassignUsernameToAccountResult
}
}`,
[UnassignUsernameToAccountResult],
);
export type UnassignUsernameFromAccountRequest = RequestOf<
typeof UnassignUsernameFromAccountMutation
>;

export const UsernameQuery = graphql(
`query Username($request: UsernameRequest!) {
value: username(request: $request) {
Expand Down

0 comments on commit 3fec089

Please sign in to comment.