From add181bb9b63e636dee5e67ecb219358042fefee Mon Sep 17 00:00:00 2001 From: Juan Garcia Date: Wed, 4 Dec 2024 16:55:58 +0100 Subject: [PATCH 1/4] Implement username/usernames queries --- packages/client/src/actions/index.ts | 1 + packages/client/src/actions/timeline.ts | 2 +- packages/client/src/actions/username.ts | 47 +++++++++++++++++++++ packages/client/src/clients.ts | 2 +- packages/graphql/src/fragments/index.ts | 1 + packages/graphql/src/fragments/namespace.ts | 27 ++++++++++++ packages/graphql/src/fragments/username.ts | 22 +++++----- packages/graphql/src/index.ts | 5 ++- packages/graphql/src/username.ts | 28 ++++++++++++ 9 files changed, 119 insertions(+), 16 deletions(-) create mode 100644 packages/client/src/actions/username.ts create mode 100644 packages/graphql/src/fragments/namespace.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 f9bbb6bfe..1b75820d1 100644 --- a/packages/client/src/actions/index.ts +++ b/packages/client/src/actions/index.ts @@ -5,3 +5,4 @@ export * from './follow'; export * from './post'; export * from './posts'; export * from './transactions'; +export * from './username'; diff --git a/packages/client/src/actions/timeline.ts b/packages/client/src/actions/timeline.ts index e9d2243e0..6fa34244d 100644 --- a/packages/client/src/actions/timeline.ts +++ b/packages/client/src/actions/timeline.ts @@ -32,7 +32,7 @@ export function fetchTimeline( } /** - * Fetch fetchTimelineHighlights from an account. + * Fetch Timeline Highlights from an account. * * ```ts * const result = await fetchTimelineHighlights(anyClient, { diff --git a/packages/client/src/actions/username.ts b/packages/client/src/actions/username.ts new file mode 100644 index 000000000..4dc8f30d3 --- /dev/null +++ b/packages/client/src/actions/username.ts @@ -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 { + return client.query(UsernameQuery, { request }); +} + +/** + * Fetch usernames owned by an account. + * + * ```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 | null, UnexpectedError> { + return client.query(UsernamesQuery, { request }); +} diff --git a/packages/client/src/clients.ts b/packages/client/src/clients.ts index 152dd7e33..1082d98a1 100644 --- a/packages/client/src/clients.ts +++ b/packages/client/src/clients.ts @@ -6,7 +6,7 @@ import type { SignedAuthChallenge, } from '@lens-protocol/graphql'; import type { Credentials, IStorage, IStorageProvider } from '@lens-protocol/storage'; -import { InMemoryStorageProvider, createCredentialsStorage } from '@lens-protocol/storage'; +import { createCredentialsStorage } from '@lens-protocol/storage'; import { ResultAsync, type TxHash, diff --git a/packages/graphql/src/fragments/index.ts b/packages/graphql/src/fragments/index.ts index 42e7f483d..dd90d4266 100644 --- a/packages/graphql/src/fragments/index.ts +++ b/packages/graphql/src/fragments/index.ts @@ -7,3 +7,4 @@ export * from './SponsoredTransactionRequest'; export * from './SelfFundedTransactionRequest'; export * from './TransactionWillFail'; export * from './username'; +export * from './namespace'; diff --git a/packages/graphql/src/fragments/namespace.ts b/packages/graphql/src/fragments/namespace.ts new file mode 100644 index 000000000..368b03517 --- /dev/null +++ b/packages/graphql/src/fragments/namespace.ts @@ -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; diff --git a/packages/graphql/src/fragments/username.ts b/packages/graphql/src/fragments/username.ts index e99fde15b..3a7fcb89b 100644 --- a/packages/graphql/src/fragments/username.ts +++ b/packages/graphql/src/fragments/username.ts @@ -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; diff --git a/packages/graphql/src/index.ts b/packages/graphql/src/index.ts index 2bddf8312..76234a115 100644 --- a/packages/graphql/src/index.ts +++ b/packages/graphql/src/index.ts @@ -2,10 +2,11 @@ export * from './accounts'; export * from './app'; export * from './authentication'; export * from './enums'; -export * from './fragments'; export * from './follow'; +export * from './fragments'; export * from './graphql'; export * from './health'; export * from './post'; -export * from './transactions'; 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..e5e57092a --- /dev/null +++ b/packages/graphql/src/username.ts @@ -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; + +export const UsernamesQuery = graphql( + `query Usernames($request: UsernamesRequest!) { + value: usernames(request: $request) { + __typename + items { + ...Username + } + pageInfo { + ...PaginatedResultInfo + } + } + }`, + [Username, PaginatedResultInfo], +); +export type UsernamesRequest = RequestOf; From a3ee003309cf7e1c7540d44ab4d6da07051ad7dc Mon Sep 17 00:00:00 2001 From: Juan Garcia Date: Mon, 9 Dec 2024 09:44:23 +0100 Subject: [PATCH 2/4] Fix lint --- packages/client/src/actions/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/client/src/actions/index.ts b/packages/client/src/actions/index.ts index 5e63918ab..cca361c90 100644 --- a/packages/client/src/actions/index.ts +++ b/packages/client/src/actions/index.ts @@ -7,4 +7,3 @@ export * from './posts'; export * from './transactions'; export * from './username'; export * from './timeline'; - From 5cf9f0ff4fc5ce9cfd83e0d170dd73db98e76465 Mon Sep 17 00:00:00 2001 From: Juan Date: Tue, 10 Dec 2024 18:32:05 +0100 Subject: [PATCH 3/4] Update packages/client/src/actions/timeline.ts Co-authored-by: Cesare Naldi --- packages/client/src/actions/timeline.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/actions/timeline.ts b/packages/client/src/actions/timeline.ts index 6fa34244d..41c709a2a 100644 --- a/packages/client/src/actions/timeline.ts +++ b/packages/client/src/actions/timeline.ts @@ -32,7 +32,7 @@ export function fetchTimeline( } /** - * Fetch Timeline Highlights from an account. + * Fetch Timeline Highlights for an account. * * ```ts * const result = await fetchTimelineHighlights(anyClient, { From 91a89cab66f10a77b86669100a21dd86e5f64246 Mon Sep 17 00:00:00 2001 From: Juan Date: Tue, 10 Dec 2024 18:32:21 +0100 Subject: [PATCH 4/4] Update packages/client/src/actions/username.ts Co-authored-by: Cesare Naldi --- packages/client/src/actions/username.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/actions/username.ts b/packages/client/src/actions/username.ts index 4dc8f30d3..3ec1b3b8c 100644 --- a/packages/client/src/actions/username.ts +++ b/packages/client/src/actions/username.ts @@ -27,7 +27,7 @@ export function fetchUsername( } /** - * Fetch usernames owned by an account. + * Fetch usernames owned by an address. * * ```ts * const result = await fetchUsernames(anyClient, {