Skip to content

Commit

Permalink
feat: implements PostFields fragment injection
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarenaldi committed Jan 7, 2025
1 parent 531439f commit 430c082
Show file tree
Hide file tree
Showing 14 changed files with 427 additions and 262 deletions.
4 changes: 2 additions & 2 deletions packages/client/src/actions/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function fetchAccount<TAccount extends Account>(
client: AnyClient<Context<TAccount>>,
request: AccountRequest,
): ResultAsync<TAccount | null, UnexpectedError> {
return client.query(accountQuery(client.context.accountFragment), { request });
return client.query(accountQuery([client.context.accountFragment]), { request });
}

/**
Expand All @@ -102,7 +102,7 @@ export function fetchAccounts<TAccount extends Account>(
client: AnyClient<Context<TAccount>>,
request: AccountsRequest = {},
): ResultAsync<Paginated<TAccount> | null, UnexpectedError> {
return client.query(accountsQuery(client.context.accountFragment), { request });
return client.query(accountsQuery([client.context.accountFragment]), { request });
}

/**
Expand Down
10 changes: 6 additions & 4 deletions packages/client/src/actions/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ import {
AuthenticatedSessionsQuery,
CurrentSessionQuery,
LegacyRolloverRefreshMutation,
MeQuery,
RefreshMutation,
RevokeAuthenticationMutation,
SwitchAccountMutation,
meQuery,
} from '@lens-protocol/graphql';
import type { ResultAsync } from '@lens-protocol/types';

import type { Account } from '@lens-protocol/graphql';
import type { AnyClient, SessionClient } from '../clients';
import type { Context } from '../context';
import type { UnauthenticatedError, UnexpectedError } from '../errors';

/**
Expand Down Expand Up @@ -151,8 +153,8 @@ export function switchAccount(
* @param client - The session client for the authenticated Account.
* @returns The details of the authenticated Account.
*/
export function fetchMeDetails(
client: SessionClient,
export function fetchMeDetails<TAccount extends Account>(
client: SessionClient<Context<TAccount>>,
): ResultAsync<MeResult, UnauthenticatedError | UnexpectedError> {
return client.query(MeQuery, {});
return client.query(meQuery([client.context.accountFragment]), {});
}
43 changes: 24 additions & 19 deletions packages/client/src/actions/posts.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
import type {
Account,
AccountPostReaction,
ActionInfo,
AnyPost,
Paginated,
Post,
PostActionsRequest,
PostBookmarksRequest,
PostEdit,
PostEditsRequest,
PostFields,
PostReactionStatus,
PostReactionStatusRequest,
PostReactionsRequest,
PostReferencesRequest,
PostRequest,
PostTagsRequest,
PostsRequest,
WhoActedOnPostQueryRequest,
WhoReferencedPostRequest,
} from '@lens-protocol/graphql';
import {
PostActionsQuery,
PostBookmarksQuery,
PostEditsQuery,
PostQuery,
PostReactionStatusQuery,
PostReactionsQuery,
PostReferencesQuery,
PostTagsQuery,
PostsQuery,
WhoActedOnPostQuery,
WhoReferencedPostQuery,
postBookmarksQuery,
postQuery,
postReferencesQuery,
postsQuery,
} from '@lens-protocol/graphql';
import type { ResultAsync } from '@lens-protocol/types';

import type { PostTagsRequest } from '@lens-protocol/graphql';
import type { PostReactionStatusRequest } from '@lens-protocol/graphql';
import type { PostReactionStatus } from '@lens-protocol/graphql';
import type { WhoReferencedPostRequest } from '@lens-protocol/graphql';
import type { Account } from '@lens-protocol/graphql';
import type { WhoActedOnPostQueryRequest } from '@lens-protocol/graphql';
import type { PostEditsRequest } from '@lens-protocol/graphql';
import type { PostEdit } from '@lens-protocol/graphql';
import type { AnyClient, SessionClient } from '../clients';
import type { Context } from '../context';
import type { UnauthenticatedError, UnexpectedError } from '../errors';

/**
Expand All @@ -53,11 +55,14 @@ import type { UnauthenticatedError, UnexpectedError } from '../errors';
* @param request - The query request.
* @returns The Post or `null` if it does not exist.
*/
export function fetchPost(
client: AnyClient,
export function fetchPost<TAccount extends Account, TPostFields extends PostFields>(
client: AnyClient<Context<TAccount, TPostFields>>,
request: PostRequest,
): ResultAsync<AnyPost | null, UnexpectedError> {
return client.query(PostQuery, { request });
): ResultAsync<AnyPost<TPostFields, TAccount> | null, UnexpectedError> {
return client.query(
postQuery([client.context.postFieldsFragment, client.context.accountFragment]),
{ request },
);
}

/**
Expand All @@ -82,7 +87,7 @@ export function fetchPosts(
client: AnyClient,
request: PostsRequest,
): ResultAsync<Paginated<AnyPost>, UnexpectedError> {
return client.query(PostsQuery, { request });
return client.query(postsQuery, { request });
}

/**
Expand Down Expand Up @@ -138,7 +143,7 @@ export function fetchPostBookmarks(
client: SessionClient,
request: PostBookmarksRequest = {},
): ResultAsync<Paginated<AnyPost>, UnexpectedError | UnauthenticatedError> {
return client.query(PostBookmarksQuery, { request });
return client.query(postBookmarksQuery, { request });
}

/**
Expand All @@ -158,7 +163,7 @@ export function fetchPostReferences(
client: AnyClient,
request: PostReferencesRequest,
): ResultAsync<Paginated<AnyPost>, UnexpectedError | UnauthenticatedError> {
return client.query(PostReferencesQuery, { request });
return client.query(postReferencesQuery, { request });
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/client/src/actions/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
TimelineItem,
TimelineRequest,
} from '@lens-protocol/graphql';
import { TimelineHighlightsQuery, TimelineQuery } from '@lens-protocol/graphql';
import { timelineHighlightsQuery, timelineQuery } from '@lens-protocol/graphql';
import type { ResultAsync } from '@lens-protocol/types';

import type { AnyClient } from '../clients';
Expand All @@ -28,7 +28,7 @@ export function fetchTimeline(
client: AnyClient,
request: TimelineRequest,
): ResultAsync<Paginated<TimelineItem> | null, UnexpectedError> {
return client.query(TimelineQuery, { request });
return client.query(timelineQuery, { request });
}

/**
Expand All @@ -48,5 +48,5 @@ export function fetchTimelineHighlights(
client: AnyClient,
request: TimelineHighlightsRequest,
): ResultAsync<Paginated<AnyPost>, UnexpectedError> {
return client.query(TimelineHighlightsQuery, { request });
return client.query(timelineHighlightsQuery, { request });
}
15 changes: 12 additions & 3 deletions packages/client/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { EnvironmentConfig } from '@lens-protocol/env';
import type { Account } from '@lens-protocol/graphql';
import type { Account, PostFields } from '@lens-protocol/graphql';
import type { FragmentDocumentFor } from '@lens-protocol/graphql';
import type { IStorageProvider } from '@lens-protocol/storage';

/**
* The client configuration.
*/
export type ClientConfig<TAccount extends Account = Account> = {
export type ClientConfig<
TAccount extends Account = Account,
TPostFields extends PostFields = PostFields,
> = {
/**
* The environment configuration to use (e.g. `mainnet`, `testnet`).
*/
Expand Down Expand Up @@ -40,5 +43,11 @@ export type ClientConfig<TAccount extends Account = Account> = {
*
* @defaultValue {@link AccountFragment}
*/
accountFragment?: FragmentDocumentFor<TAccount>;
accountFragment?: FragmentDocumentFor<TAccount, 'Account'>;
/**
* The Post Fragment to use.
*
* @defaultValue {@link PostFragment}
*/
postFieldsFragment?: FragmentDocumentFor<TPostFields, 'Post', 'PostFields'>;
};
13 changes: 9 additions & 4 deletions packages/client/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import type { EnvironmentConfig } from '@lens-protocol/env';
import { type Account, AccountFragment } from '@lens-protocol/graphql';
import type { FragmentDocumentFor } from '@lens-protocol/graphql';
import { type Account, AccountFragment, PostFieldsFragment } from '@lens-protocol/graphql';
import type { FragmentDocumentFor, PostFields } from '@lens-protocol/graphql';
import { type IStorageProvider, InMemoryStorageProvider } from '@lens-protocol/storage';
import type { ClientConfig } from './config';

/**
* @internal
*/
export type Context<TAccount extends Account = Account> = {
export type Context<
TAccount extends Account = Account,
TPostFields extends PostFields = PostFields,
> = {
environment: EnvironmentConfig;
cache: boolean;
debug: boolean;
origin?: string;
storage: IStorageProvider;
accountFragment: FragmentDocumentFor<TAccount>;
accountFragment: FragmentDocumentFor<TAccount, 'Account'>;
postFieldsFragment: FragmentDocumentFor<TPostFields, 'Post', 'PostFields'>;
};

/**
Expand All @@ -36,5 +40,6 @@ export function configureContext<TConfig extends ClientConfig>(
origin: from.origin,
storage: from.storage ?? new InMemoryStorageProvider(),
accountFragment: from.accountFragment ?? AccountFragment,
postFieldsFragment: from.postFieldsFragment ?? PostFieldsFragment,
} as ContextFrom<TConfig>;
}
3 changes: 3 additions & 0 deletions packages/client/testing-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { evmAddress } from '@lens-protocol/types';
import { http, type Account, type Transport, type WalletClient, createWalletClient } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';

import { FullAccountFragment, FullPostFieldsFragment } from '@lens-protocol/graphql';
import { GraphQLErrorCode, PublicClient, testnet as apiEnv } from './src';

const pk = privateKeyToAccount(import.meta.env.PRIVATE_KEY);
Expand All @@ -16,6 +17,8 @@ export function createPublicClient() {
return PublicClient.create({
environment: apiEnv,
origin: 'http://example.com',
accountFragment: FullAccountFragment,
postFieldsFragment: FullPostFieldsFragment,
});
}

Expand Down
42 changes: 13 additions & 29 deletions packages/graphql/src/accounts/account.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { FragmentOf } from 'gql.tada';
import type { Paginated } from '../common';
import {
type Account,
AccountAvailableFragment,
AccountBlockedFragment,
AccountFragment,
Expand All @@ -10,34 +8,20 @@ import {
SponsoredTransactionRequestFragment,
TransactionWillFailFragment,
} from '../fragments';
import {
type FragmentDocumentFor,
type RequestOf,
type RequestTypeOf,
type StandardDocumentNode,
graphql,
} from '../graphql';

export type AccountRequest = RequestTypeOf<'AccountRequest'>;
export function accountQuery<TAccount extends Account>(
fragment: FragmentDocumentFor<TAccount>,
): StandardDocumentNode<TAccount | null, AccountRequest> {
return graphql(
`query Account($request: AccountRequest!) {
import { type RequestOf, dynamic, graphql } from '../graphql';

export const accountQuery = dynamic(
`query Account($request: AccountRequest!) {
value: account(request: $request) {
...Account
}
}`,
[fragment],
) as StandardDocumentNode;
}

export type AccountsRequest = RequestTypeOf<'AccountsRequest'>;
export function accountsQuery<TAccount extends Account>(
fragment: FragmentDocumentFor<TAccount>,
): StandardDocumentNode<Paginated<TAccount>, AccountsRequest> {
return graphql(
`query Accounts($request: AccountsRequest!) {
[],
);
export type AccountRequest = RequestOf<typeof accountQuery>;

export const accountsQuery = dynamic(
`query Accounts($request: AccountsRequest!) {
value: accounts(request: $request) {
__typename
items {
Expand All @@ -48,9 +32,9 @@ export function accountsQuery<TAccount extends Account>(
}
}
}`,
[fragment, PaginatedResultInfoFragment],
) as StandardDocumentNode;
}
[PaginatedResultInfoFragment],
);
export type AccountsRequest = RequestOf<typeof accountsQuery>;

export const AccountsBulkQuery = graphql(
`query AccountsBulk($request: AccountsBulkRequest!) {
Expand Down
27 changes: 23 additions & 4 deletions packages/graphql/src/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import type { FragmentOf } from 'gql.tada';
import { AccountAvailableFragment, PaginatedResultInfoFragment } from './fragments';
import { type RequestOf, graphql } from './graphql';
import {
type Account,
AccountAvailableFragment,
type FullAccount,
PaginatedResultInfoFragment,
} from './fragments';
import {
type FragmentDocumentFor,
type PartialFragmentOf,
type RequestOf,
dynamic,
graphql,
partial,
} from './graphql';

const AuthenticationChallengeFragment = graphql(
`fragment AuthenticationChallenge on AuthenticationChallenge {
Expand Down Expand Up @@ -198,7 +210,7 @@ export const SwitchAccountMutation = graphql(
);
export type SwitchAccountRequest = RequestOf<typeof SwitchAccountMutation>;

const MeResultFragment = graphql(
const MeResultFragment = partial(
`fragment MeResult on MeResult {
appLoggedIn
isSignless
Expand All @@ -215,9 +227,16 @@ const MeResultFragment = graphql(
}`,
[AccountAvailableFragment],
);
export type MeDetails<TAccount extends Account> = PartialFragmentOf<
typeof MeResultFragment,
[FragmentDocumentFor<TAccount>]
>;

type test = MeDetails<FullAccount>;

export type MeResult = FragmentOf<typeof MeResultFragment>;

export const MeQuery = graphql(
export const meQuery = dynamic(
`query Me {
value: me {
...MeResult
Expand Down
Loading

0 comments on commit 430c082

Please sign in to comment.