Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement username/usernames queries #997

Merged
merged 7 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/client/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './follow';
export * from './post';
export * from './posts';
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 from an account.
juangm marked this conversation as resolved.
Show resolved Hide resolved
*
* ```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 account.
juangm marked this conversation as resolved.
Show resolved Hide resolved
*
* ```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 });
}
2 changes: 1 addition & 1 deletion packages/client/src/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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,
Expand Down Expand Up @@ -50,7 +50,7 @@
data,
error,
}: OperationResult<StandardData<T> | undefined, AnyVariables>): T {
invariant(data, `Expected a value, got: ${error?.message}`);

Check failure on line 53 in packages/client/src/clients.ts

View workflow job for this annotation

GitHub Actions / Verify / Test

src/actions/posts.test.ts > Given the Post query actions > When invoking the 'fetchPost' action > Then it should not fail w/ a GQL BadRequest error

InvariantError: Expected a value, got: [GraphQL] Unknown field "createdAt" on type "UsernameNamespace". [GraphQL] Unknown field "owner" on type "UsernameNamespace". ❯ Module.p ../types/src/helpers/invariant.ts:17:11 ❯ takeValue src/clients.ts:53:3 ❯ ResultAsync.<anonymous> ../../node_modules/.pnpm/[email protected]/node_modules/neverthrow/dist/index.cjs.js:127:33 ❯ ../../node_modules/.pnpm/[email protected]/node_modules/neverthrow/dist/index.cjs.js:43:57 ❯ __awaiter ../../node_modules/.pnpm/[email protected]/node_modules/neverthrow/dist/index.cjs.js:39:12 ❯ ../../node_modules/.pnpm/[email protected]/node_modules/neverthrow/dist/index.cjs.js:123:60 ❯ src/actions/posts.test.ts:16:22
return data.value;
}

Expand Down
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think gql.tada likes GQL comments, does it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't complain when I do pnpm run check so I guess it is fine 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

over on the types generated from it, if you get never it failed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@juangm did you check if the type is usable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just removed the comments, when we have the rules properly defined I will add where needed

# 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>;
5 changes: 3 additions & 2 deletions packages/graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
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>;
Loading