Skip to content

Commit

Permalink
Disable subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitayutanov committed Mar 27, 2024
1 parent 20fb91d commit 9f045ce
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import styles from './collection-card.module.scss';
type Props = Pick<Collection, 'id' | 'name' | 'collectionBanner' | 'collectionLogo' | 'admin' | 'tokensLimit'> & {
nfts: Pick<Nft, 'id' | 'mediaUrl'>[];
} & {
nftsCount: string | undefined;
nftsCount: number | undefined;
};

const PREVIEW_NFTS_COUNT = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import LandscapeSVG from '../../assets/landscape.svg?react';

type Props = Omit<InfoCardProps, 'heading' | 'text' | 'SVG'> & {
heading: string | null | undefined;
text: string | number | undefined;
text: number | undefined;
};

function MintLimitInfoCard({ heading, text, ...props }: Props) {
Expand Down
16 changes: 8 additions & 8 deletions frontend/src/graphql/gql.ts

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions frontend/src/graphql/graphql.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion frontend/src/pages/collection/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const SOCIAL_ICON = {
};

const COLLECTION_SUBSCRIPTION = graphql(`
subscription CollectionSub($id: String!) {
query CollectionSub($id: String!) {
collectionById(id: $id) {
id
name
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/collection/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useSubscription } from '@apollo/client';
import { useQuery, useSubscription } from '@apollo/client';

// TODO: reusing temporary solution for nfts subscriptiption
import { useNFTs } from '../lists/hooks';
Expand All @@ -9,7 +9,7 @@ function useCollection(id: string, owner: string) {
const [nfts, nftsCount, hasMoreNFTs, isNFTsQueryReady, fetchNFTs] = useNFTs(owner, id);

// subscription to handle update in case of newly created collection
const { data, loading } = useSubscription(COLLECTION_SUBSCRIPTION, { variables: { id } });
const { data, loading } = useQuery(COLLECTION_SUBSCRIPTION, { variables: { id } });
const collection = data?.collectionById;
const isCollectionQueryReady = !loading && isNFTsQueryReady;

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/lists/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const COLLECTIONS_QUERY = graphql(`
`);

const COLLECTIONS_SUBSCRIPTION = graphql(`
subscription CollectionsSub($limit: Int!, $offset: Int!, $where: CollectionWhereInput!) {
query CollectionsSub($limit: Int!, $offset: Int!, $where: CollectionWhereInput!) {
collections(limit: $limit, offset: $offset, orderBy: [createdAt_DESC, name_DESC, id_DESC], where: $where) {
id
name
Expand Down Expand Up @@ -97,7 +97,7 @@ const NFTS_QUERY = graphql(`
`);

const NFTS_SUBSCRIPTION = graphql(`
subscription NFTsSubscription($limit: Int!, $offset: Int!, $where: NftWhereInput!) {
query NFTsSubscription($limit: Int!, $offset: Int!, $where: NftWhereInput!) {
nfts(limit: $limit, offset: $offset, orderBy: [createdAt_DESC, name_DESC, id_DESC], where: $where) {
id
idInCollection
Expand Down
98 changes: 49 additions & 49 deletions frontend/src/pages/lists/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,31 @@ function useCollections(admin: string) {
const isReady = !loading && isTotalCountReady;
const hasMore = totalCount && collectionsCount ? collectionsCount < totalCount : false;

useEffect(() => {
if (loading) return;
// useEffect(() => {
// if (loading) return;

const limit = collectionsCount || 1; // 1 fallback, cuz in case of empty list with limit 0, subscription won't work
const offset = 0;
// const limit = collectionsCount || 1; // 1 fallback, cuz in case of empty list with limit 0, subscription won't work
// const offset = 0;

// same solution as for nfts, but for newly created collections
const unsubscribe = subscribeToMore({
document: COLLECTIONS_SUBSCRIPTION,
variables: { limit, offset, where },
updateQuery: (prev = { collections: [] }, { subscriptionData }) => {
if (!subscriptionData.data) return { collections: [] };
// // same solution as for nfts, but for newly created collections
// const unsubscribe = subscribeToMore({
// document: COLLECTIONS_SUBSCRIPTION,
// variables: { limit, offset, where },
// updateQuery: (prev = { collections: [] }, { subscriptionData }) => {
// if (!subscriptionData.data) return { collections: [] };

const newCollections = subscriptionData.data.collections.filter(
(subCollection) => !prev.collections.some((collection) => collection.id === subCollection.id),
);
// const newCollections = subscriptionData.data.collections.filter(
// (subCollection) => !prev.collections.some((collection) => collection.id === subCollection.id),
// );

return { collections: [...newCollections, ...prev.collections] };
},
});
// return { collections: [...newCollections, ...prev.collections] };
// },
// });

return () => {
unsubscribe();
};
}, [subscribeToMore, collectionsCount, where, loading]);
// return () => {
// unsubscribe();
// };
// }, [subscribeToMore, collectionsCount, where, loading]);

const fetchCollections = useCallback(() => {
const offset = collectionsCount;
Expand Down Expand Up @@ -114,35 +114,35 @@ function useNFTs(owner: string, collectionId?: string) {
const hasMoreNFTs = totalCount && nftsCount ? nftsCount < totalCount : false;
const isNFTsQueryReady = !loading && isTotalCountReady;

useEffect(() => {
if (loading) return;

const limit = nftsCount || 1; // 1 fallback, cuz in case of empty list with limit 0, subscription won't work
const offset = 0;

// kinda tricky subscription to handle live interaction,
// works for now, but worth to reconsider them later.
// maybe would be better to use connection's cursor pagination?
const unsubscribe = subscribeToMore({
document: NFTS_SUBSCRIPTION,
variables: { limit, offset, where },
updateQuery: (prev = { nfts: [] }, { subscriptionData }) => {
// extracting newly minted nfts, merge type policy should handle the rest
if (!subscriptionData.data) return { nfts: [] };

// important to preserve consistent sorting of nfts, otherwise results will be inaccurate
const mintedNfts = subscriptionData.data.nfts.filter(
(subNft) => !prev.nfts.some((nft) => nft.id === subNft.id),
);

return { nfts: [...mintedNfts, ...prev.nfts] };
},
});

return () => {
unsubscribe();
};
}, [subscribeToMore, nftsCount, where, loading]);
// useEffect(() => {
// if (loading) return;

// const limit = nftsCount || 1; // 1 fallback, cuz in case of empty list with limit 0, subscription won't work
// const offset = 0;

// // kinda tricky subscription to handle live interaction,
// // works for now, but worth to reconsider them later.
// // maybe would be better to use connection's cursor pagination?
// const unsubscribe = subscribeToMore({
// document: NFTS_SUBSCRIPTION,
// variables: { limit, offset, where },
// updateQuery: (prev = { nfts: [] }, { subscriptionData }) => {
// // extracting newly minted nfts, merge type policy should handle the rest
// if (!subscriptionData.data) return { nfts: [] };

// // important to preserve consistent sorting of nfts, otherwise results will be inaccurate
// const mintedNfts = subscriptionData.data.nfts.filter(
// (subNft) => !prev.nfts.some((nft) => nft.id === subNft.id),
// );

// return { nfts: [...mintedNfts, ...prev.nfts] };
// },
// });

// return () => {
// unsubscribe();
// };
// }, [subscribeToMore, nftsCount, where, loading]);

const fetchNFTs = useCallback(() => {
const offset = nftsCount;
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/nft/consts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { graphql } from '@/graphql';

const NFT_QUERY = graphql(`
subscription NFTQuery($id: String!) {
query NFTQuery($id: String!) {
nftById(id: $id) {
id
idInCollection
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/nft/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useSubscription } from '@apollo/client';
import { useQuery, useSubscription } from '@apollo/client';

import { NFT_QUERY } from './consts';

function useNFT(collectionId: string, idInCollection: string) {
const id = `${collectionId}-${idInCollection}`;
const { data } = useSubscription(NFT_QUERY, { variables: { id } });
const { data } = useQuery(NFT_QUERY, { variables: { id } });

return data?.nftById;
}
Expand Down

0 comments on commit 9f045ce

Please sign in to comment.