Skip to content

Commit

Permalink
feat: fetchAccounts support custom fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarenaldi committed Jan 6, 2025
1 parent 0d3fd5f commit 7b878fc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
17 changes: 5 additions & 12 deletions examples/custom-fragments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof MyAccountFragment>;
Expand Down
18 changes: 12 additions & 6 deletions packages/client/src/actions/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
AccountsAvailableQuery,
AccountsBlockedQuery,
AccountsBulkQuery,
AccountsQuery,
BlockMutation,
CreateAccountWithUsernameMutation,
EnableSignlessMutation,
Expand All @@ -50,6 +49,7 @@ import {
UndoRecommendAccountMutation,
UnmuteAccountMutation,
accountQuery,
accountsQuery,
} from '@lens-protocol/graphql';
import type { ResultAsync } from '@lens-protocol/types';

Expand Down Expand Up @@ -85,18 +85,24 @@ export function fetchAccount<TAccount extends Account>(
* 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<TAccount extends Account>(
client: AnyClient<Context<TAccount>>,
request: AccountsRequest = {},
): ResultAsync<Paginated<Account> | null, UnexpectedError> {
return client.query(AccountsQuery, { request });
): ResultAsync<Paginated<TAccount> | null, UnexpectedError> {
return client.query(accountsQuery(client.context.accountFragment), { request });
}

/**
Expand Down
18 changes: 11 additions & 7 deletions packages/graphql/src/accounts/account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { FragmentOf } from 'gql.tada';
import type { Paginated } from '../common';
import {
type Account,
AccountAvailableFragment,
Expand All @@ -18,7 +19,6 @@ import {
} from '../graphql';

export type AccountRequest = RequestTypeOf<'AccountRequest'>;

export function accountQuery<TAccount extends Account>(
fragment: FragmentDocumentFor<TAccount>,
): StandardDocumentNode<TAccount | null, AccountRequest> {
Expand All @@ -32,21 +32,25 @@ export function accountQuery<TAccount extends Account>(
) as StandardDocumentNode;
}

export const AccountsQuery = graphql(
`query Accounts($request: AccountsRequest!) {
export type AccountsRequest = RequestTypeOf<'AccountsRequest'>;
export function accountsQuery<TAccount extends Account>(
fragment: FragmentDocumentFor<TAccount>,
): StandardDocumentNode<Paginated<TAccount>, AccountsRequest> {
return graphql(
`query Accounts($request: AccountsRequest!) {
value: accounts(request: $request) {
__typename
items{
items {
...Account
}
pageInfo {
...PaginatedResultInfo
}
}
}`,
[AccountFragment, PaginatedResultInfoFragment],
);
export type AccountsRequest = RequestOf<typeof AccountsQuery>;
[fragment, PaginatedResultInfoFragment],
) as StandardDocumentNode;
}

export const AccountsBulkQuery = graphql(
`query AccountsBulk($request: AccountsBulkRequest!) {
Expand Down
8 changes: 1 addition & 7 deletions packages/graphql/src/fragments/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,8 @@ export const AccountFragment = graphql(
`fragment Account on Account {
__typename
address
owner
score
createdAt
username{
...Username
}
}`,
[UsernameFragment],
[],
);
export type Account = FragmentOf<typeof AccountFragment>;

Expand Down

0 comments on commit 7b878fc

Please sign in to comment.