Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement mutations for username #1009

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading