From 7b878fcb0101ab1ff450b1d22267c9f6cea5bcc9 Mon Sep 17 00:00:00 2001 From: Cesare Naldi Date: Thu, 26 Dec 2024 14:53:43 +0100 Subject: [PATCH] feat: fetchAccounts support custom fragments --- examples/custom-fragments/index.ts | 17 +++++------------ packages/client/src/actions/account.ts | 18 ++++++++++++------ packages/graphql/src/accounts/account.ts | 18 +++++++++++------- packages/graphql/src/fragments/account.ts | 8 +------- 4 files changed, 29 insertions(+), 32 deletions(-) diff --git a/examples/custom-fragments/index.ts b/examples/custom-fragments/index.ts index c5daf26a7..0d9f3b3e2 100644 --- a/examples/custom-fragments/index.ts +++ b/examples/custom-fragments/index.ts @@ -3,33 +3,26 @@ import 'viem/window'; import { type FragmentOf, PublicClient, - UsernameFragment, evmAddress, graphql, testnet as protocolTestnet, } from '@lens-protocol/client'; import { fetchAccount } from '@lens-protocol/client/actions'; -const MyAccountMetadataFragment = graphql( - `fragment AccountMetadata on AccountMetadata { - __typename - name - picture - }`, -); - const MyAccountFragment = graphql( `fragment Account on Account { __typename address username { - ...Username + value } metadata { - ...AccountMetadata + __typename + name + picture } }`, - [UsernameFragment, MyAccountMetadataFragment], + [], ); type MyAccount = FragmentOf; diff --git a/packages/client/src/actions/account.ts b/packages/client/src/actions/account.ts index 78c916750..8b043fa55 100644 --- a/packages/client/src/actions/account.ts +++ b/packages/client/src/actions/account.ts @@ -37,7 +37,6 @@ import { AccountsAvailableQuery, AccountsBlockedQuery, AccountsBulkQuery, - AccountsQuery, BlockMutation, CreateAccountWithUsernameMutation, EnableSignlessMutation, @@ -50,6 +49,7 @@ import { UndoRecommendAccountMutation, UnmuteAccountMutation, accountQuery, + accountsQuery, } from '@lens-protocol/graphql'; import type { ResultAsync } from '@lens-protocol/types'; @@ -85,18 +85,24 @@ export function fetchAccount( * Using a {@link SessionClient} will yield {@link Account#operations} specific to the authenticated Account. * * ```ts - * const result = await fetchAccounts(anyClient); + * const result = await fetchAccounts(anyClient, { + * filter: { + * searchBy: { + * localNameQuery: 'stani', + * } + * } + * }); * ``` * * @param client - Any Lens client. * @param request - The query request. * @returns The list of accounts. */ -export function fetchAccounts( - client: AnyClient, +export function fetchAccounts( + client: AnyClient>, request: AccountsRequest = {}, -): ResultAsync | null, UnexpectedError> { - return client.query(AccountsQuery, { request }); +): ResultAsync | null, UnexpectedError> { + return client.query(accountsQuery(client.context.accountFragment), { request }); } /** diff --git a/packages/graphql/src/accounts/account.ts b/packages/graphql/src/accounts/account.ts index abe986505..1019bd162 100644 --- a/packages/graphql/src/accounts/account.ts +++ b/packages/graphql/src/accounts/account.ts @@ -1,4 +1,5 @@ import type { FragmentOf } from 'gql.tada'; +import type { Paginated } from '../common'; import { type Account, AccountAvailableFragment, @@ -18,7 +19,6 @@ import { } from '../graphql'; export type AccountRequest = RequestTypeOf<'AccountRequest'>; - export function accountQuery( fragment: FragmentDocumentFor, ): StandardDocumentNode { @@ -32,11 +32,15 @@ export function accountQuery( ) as StandardDocumentNode; } -export const AccountsQuery = graphql( - `query Accounts($request: AccountsRequest!) { +export type AccountsRequest = RequestTypeOf<'AccountsRequest'>; +export function accountsQuery( + fragment: FragmentDocumentFor, +): StandardDocumentNode, AccountsRequest> { + return graphql( + `query Accounts($request: AccountsRequest!) { value: accounts(request: $request) { __typename - items{ + items { ...Account } pageInfo { @@ -44,9 +48,9 @@ export const AccountsQuery = graphql( } } }`, - [AccountFragment, PaginatedResultInfoFragment], -); -export type AccountsRequest = RequestOf; + [fragment, PaginatedResultInfoFragment], + ) as StandardDocumentNode; +} export const AccountsBulkQuery = graphql( `query AccountsBulk($request: AccountsBulkRequest!) { diff --git a/packages/graphql/src/fragments/account.ts b/packages/graphql/src/fragments/account.ts index ad5642b32..112ed209d 100644 --- a/packages/graphql/src/fragments/account.ts +++ b/packages/graphql/src/fragments/account.ts @@ -48,14 +48,8 @@ export const AccountFragment = graphql( `fragment Account on Account { __typename address - owner - score - createdAt - username{ - ...Username - } }`, - [UsernameFragment], + [], ); export type Account = FragmentOf;