@apollo/client#FieldPolicy TypeScript Examples

The following examples show how to use @apollo/client#FieldPolicy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: paginationHandler.ts    From lexicon with MIT License 6 votes vote down vote up
export function userActivityPagination(
  keyArgs: KeyArgs = false,
): FieldPolicy<Array<UserActivity>> {
  return {
    keyArgs,
    merge(existing, incoming, { args }) {
      let merged: Array<UserActivity> = existing ? existing.slice(0) : [];

      if (args) {
        const { offset = 0 } = args;
        for (let i = 0; i < incoming.length; i++) {
          merged[offset + i] = incoming[i];
        }
      } else {
        merged = [...merged, ...incoming];
      }

      const filteredArr = merged.reduce((acc: Array<UserActivity>, current) => {
        const duplicateValue = acc.find(
          (item) =>
            item.postId === current.postId &&
            item.actionType === current.actionType,
        );
        if (!duplicateValue) {
          return acc.concat([current]);
        }
        return acc;
      }, []);

      return filteredArr;
    },
  };
}
Example #2
Source File: paginationHandler.ts    From lexicon with MIT License 6 votes vote down vote up
export function prependAppendPagination<T = Reference>(
  keyArgs: KeyArgs = [],
): FieldPolicy<Array<T>> {
  return {
    keyArgs,
    merge: (existing: any, incoming: any) => {
      if (
        !existing ||
        !incoming ||
        existing.length === 0 ||
        incoming.length === 0
      ) {
        return incoming || existing || null;
      }

      if (
        JSON.stringify(existing[existing.length - 1]) <
        JSON.stringify(incoming[incoming.length - 1])
      ) {
        return handleDuplicateRef(existing, incoming);
      } else {
        // Prepending data will trigger data reindexing.
        // There's an issue with data reindexing inside a FlatList,
        // because the cursor position is not "held" at the current post.
        return handleDuplicateRef(incoming, existing);
      }
    },
  };
}
Example #3
Source File: utils.ts    From mStable-apps with GNU Lesser General Public License v3.0 6 votes vote down vote up
readAsBN = <T>(field: keyof T): FieldPolicy<BigNumber> => ({
  read(existing, options) {
    const exact = options.readField(field as string)
    return BigNumber.from(exact ?? 0)
  },
})
Example #4
Source File: utils.ts    From mStable-apps with GNU Lesser General Public License v3.0 6 votes vote down vote up
readAsBD = <T>(field: keyof T, decimals = 18): FieldPolicy<BigDecimal> => ({
  read(existing, options) {
    const exact: number = options.readField(field as string) as number
    return new BigDecimal(exact ?? 0, decimals)
  },
})
Example #5
Source File: paginationHandler.ts    From lexicon with MIT License 5 votes vote down vote up
export function replaceDataPagination<T = Reference>(
  keyArgs: KeyArgs = [],
): FieldPolicy<Array<T>> {
  return {
    keyArgs,
    merge: (existing: any, incoming: any) => incoming || existing || null,
  };
}
Example #6
Source File: paginationHandler.ts    From lexicon with MIT License 5 votes vote down vote up
export function appendPagination<T = Reference>(
  keyArgs: KeyArgs = [],
  screen: 'HOME' | 'SEARCH' | 'MESSAGE_DETAIL' | 'NOTIFICATIONS',
): FieldPolicy<Array<T>> {
  return {
    keyArgs,
    merge: (existing: any, incoming: any, { args }) => {
      if (!existing || !incoming) {
        return existing || incoming || null;
      }

      let page;

      switch (screen) {
        case 'HOME':
          page = args?.page || 0;
          incoming.users = handleDuplicateRef(existing.users, incoming.users);
          if (page > 0) {
            incoming.topicList.topics = handleDuplicateRef(
              existing.topicList.topics,
              incoming.topicList.topics,
            );
          } else {
            incoming.topicList.topics = handleDuplicateRef(
              incoming.topicList.topics,
              existing.topicList.topics,
            );
          }

          break;
        case 'SEARCH':
          page = args?.page || 1;
          if (page > 1) {
            incoming.posts = handleDuplicateRef(existing.posts, incoming.posts);
            incoming.topics = handleDuplicateRef(
              existing.topics,
              incoming.topics,
            );
          }
          break;
        case 'MESSAGE_DETAIL':
          page = args?.page || 0;
          if (page >= 0) {
            incoming.topicList.topics = handleDuplicateRef(
              incoming.topicList.topics,
              existing.topicList.topics,
            );
            incoming.users = handleDuplicateRef(incoming.users, existing.users);
          }
          break;
        case 'NOTIFICATIONS':
          incoming.notifications = handleDuplicateRef(
            existing.notifications,
            incoming.notifications,
          );
          break;
      }

      return incoming;
    },
  };
}
Example #7
Source File: staking.ts    From mStable-apps with GNU Lesser General Public License v3.0 5 votes vote down vote up
typePolicies: TypedTypePolicies = {
  StakedTokenBalance: {
    fields: {
      rawBD: readAsBD<StakedTokenBalance>('raw'),
      votesBD: readAsBD<StakedTokenBalance>('votes'),
      timeMultiplierSimple: {
        read(existing, options) {
          const timeMultiplier = (options.readField('timeMultiplier') ?? 10) as number
          return timeMultiplier * 0.1
        },
      },
      questMultiplierSimple: {
        read(existing, options) {
          const questMultiplier = (options.readField('questMultiplier') ?? 10) as number
          return questMultiplier * 0.1
        },
      },
    },
  },
  Account: {
    fields: {
      totalVotesAllBD: readAsBD<Account>('totalVotesAll'),
      totalVotesMTABD: readAsBD<Account>('totalVotesMTA'),
      totalVotesBPTBD: readAsBD<Account>('totalVotesBPT'),
      permMultiplierSimple: {
        read(existing, options) {
          const permMultiplier = (options.readField('permMultiplier') ?? 10) as number
          return permMultiplier * 0.1
        },
      },
      seasonMultiplierSimple: {
        read(existing, options) {
          const seasonMultiplier = (options.readField('seasonMultiplier') ?? 10) as number
          return seasonMultiplier * 0.1
        },
      },
    },
  },
  Metric: {
    fields: {
      bigDecimal: {
        read(existing, options) {
          const decimals: number = options.readField('decimals') as Metric['decimals']
          const exact = options.readField('exact') as Metric['exact']
          return new BigDecimal(exact, decimals)
        },
      } as FieldPolicy<Metric['bigDecimal']>,
    },
  },
}