Skip to content

Commit

Permalink
Merge pull request #997 from lens-protocol/T-23199/js-action-username…
Browse files Browse the repository at this point in the history
…-usernames-queries

Co-authored-by: Cesare Naldi <[email protected]>
  • Loading branch information
juangm and cesarenaldi authored Dec 11, 2024
2 parents ab16f96 + b04d6cb commit 129ae5a
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 13 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 @@ -11,3 +11,4 @@ export * from './post';
export * from './posts';
export * from './timeline';
export * from './transactions';
export * from './username';
2 changes: 1 addition & 1 deletion packages/client/src/actions/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function fetchTimeline(
}

/**
* Fetch fetchTimelineHighlights from an account.
* Fetch Timeline Highlights for an account.
*
* ```ts
* const result = await fetchTimelineHighlights(anyClient, {
Expand Down
47 changes: 47 additions & 0 deletions packages/client/src/actions/username.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { Username, UsernameRequest, UsernamesRequest } from '@lens-protocol/graphql';
import { UsernameQuery, UsernamesQuery } from '@lens-protocol/graphql';
import type { ResultAsync } from '@lens-protocol/types';

import type { AnyClient } from '../clients';
import type { UnexpectedError } from '../errors';
import type { Paginated } from '../types';

/**
* Fetch username details.
*
* ```ts
* const result = await fetchUsername(anyClient, {
* username: { localName: 'wagmi' },
* });
* ```
*
* @param client - Any Lens client.
* @param request - The query request.
* @returns The username details.
*/
export function fetchUsername(
client: AnyClient,
request: UsernameRequest,
): ResultAsync<Username | null, UnexpectedError> {
return client.query(UsernameQuery, { request });
}

/**
* Fetch usernames owned by an address.
*
* ```ts
* const result = await fetchUsernames(anyClient, {
* owner: evmAddress('0xe2f2a5C287993345a840db3B0845fbc70f5935a5'),
* });
* ```
*
* @param client - Any Lens client.
* @param request - The query request.
* @returns The list of owned usernames.
*/
export function fetchUsernames(
client: AnyClient,
request: UsernamesRequest,
): ResultAsync<Paginated<Username> | null, UnexpectedError> {
return client.query(UsernamesQuery, { request });
}
1 change: 1 addition & 0 deletions packages/graphql/src/fragments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './SponsoredTransactionRequest';
export * from './SelfFundedTransactionRequest';
export * from './TransactionWillFail';
export * from './username';
export * from './namespace';
27 changes: 27 additions & 0 deletions packages/graphql/src/fragments/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { FragmentOf } from 'gql.tada';
import { graphql } from '../graphql';

export const UsernameNamespace = graphql(
`fragment UsernameNamespace on UsernameNamespace {
__typename
address
namespace
createdAt
metadata {
__typename
description
id
}
owner
# TODO: Implement rules
# rules {
# anyOf {
# ...RULE
# }
# required {
# ...RULE
# }
# }
}`,
);
export type UsernameNamespace = FragmentOf<typeof UsernameNamespace>;
22 changes: 10 additions & 12 deletions packages/graphql/src/fragments/username.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import type { FragmentOf } from 'gql.tada';
import { graphql } from '../graphql';
import { UsernameNamespace } from './namespace';

export const Username = graphql(`
fragment Username on Username {
export const Username = graphql(
`fragment Username on Username {
__typename
id
value
namespace {
address
namespace
metadata {
description
id
}
}
localName
linkedTo
ownedBy
timestamp
}
`);
namespace {
...UsernameNamespace
}
}`,
[UsernameNamespace],
);
export type Username = FragmentOf<typeof Username>;
1 change: 1 addition & 0 deletions packages/graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from './notifications';
export * from './post';
export * from './timeline';
export * from './transactions';
export * from './username';
28 changes: 28 additions & 0 deletions packages/graphql/src/username.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { PaginatedResultInfo, Username } from './fragments';
import { type RequestOf, graphql } from './graphql';

export const UsernameQuery = graphql(
`query Username($request: UsernameRequest!) {
value: username(request: $request) {
...Username
}
}`,
[Username],
);
export type UsernameRequest = RequestOf<typeof UsernameQuery>;

export const UsernamesQuery = graphql(
`query Usernames($request: UsernamesRequest!) {
value: usernames(request: $request) {
__typename
items {
...Username
}
pageInfo {
...PaginatedResultInfo
}
}
}`,
[Username, PaginatedResultInfo],
);
export type UsernamesRequest = RequestOf<typeof UsernamesQuery>;

0 comments on commit 129ae5a

Please sign in to comment.