@apollo/client/utilities#maybeDeepFreeze TypeScript Examples

The following examples show how to use @apollo/client/utilities#maybeDeepFreeze. 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: CacheResultProcessor.ts    From apollo-cache-policies with Apache License 2.0 4 votes vote down vote up
processReadResult<T>(
    result: T,
    options: Cache.ReadOptions<any>
  ): ReadResultStatus {
    const { cache, entityTypeMap, invalidationPolicyManager } = this.config;
    const { rootId: dataId = "ROOT_QUERY" } = options;

    if (isPlainObject(result)) {
      if (isQuery(dataId)) {
        const { variables } = options;

        const aggregateResultComplete = this.getFieldsForQuery(options).reduce<
          boolean
        >((acc, field) => {
          const fieldName = field.name.value;
          // While the field name is used as the key in the cache, the result object
          // will have it keyed by an alias name if provided so we keep track of the 
          // result key name in case it needs to be removed from the response due to an evicted TTL
          const resultKeyName = resultKeyNameFromField(field);
          const subResultStatus = this.processReadSubResult(result, fieldName);

          const typename = entityTypeMap.readEntityById(
            makeEntityId(dataId, fieldName)
          )?.typename;

          if (typename) {
            const storeFieldNameForEntity = cache.policies.getStoreFieldName({
              typename,
              fieldName,
              field,
              variables,
            });
            const queryTypename = cache.policies.rootTypenamesById[dataId];
            const storeFieldNameForQuery = cache.policies.getStoreFieldName({
              typename: queryTypename,
              fieldName,
              field,
              variables,
            });

            const renewalPolicy = invalidationPolicyManager.getRenewalPolicyForType(
              typename
            );
            if (
              renewalPolicy === RenewalPolicy.AccessAndWrite ||
              renewalPolicy === RenewalPolicy.AccessOnly
            ) {
              entityTypeMap.renewEntity(dataId, storeFieldNameForEntity);
              entityTypeMap.renewEntity(dataId, storeFieldNameForQuery);
            }

            const evictedByStoreFieldNameForEntity = invalidationPolicyManager.runReadPolicy({
              typename,
              dataId,
              fieldName,
              storeFieldName: storeFieldNameForEntity,
            });
            const evictedByStoreFieldNameForQuery = invalidationPolicyManager.runReadPolicy({
              typename,
              dataId,
              fieldName,
              storeFieldName: storeFieldNameForQuery,
            });

            if (evictedByStoreFieldNameForEntity || evictedByStoreFieldNameForQuery) {
              delete (result as Record<string, any>)[resultKeyName];
              return false;
            }
          }

          return acc && subResultStatus === ReadResultStatus.Complete;
        }, true);

        maybeDeepFreeze(result);

        return aggregateResultComplete
          ? ReadResultStatus.Complete
          : ReadResultStatus.Incomplete;
      }

      maybeDeepFreeze(result);
      return this.processReadSubResult(result);
    }

    return ReadResultStatus.Complete;
  }