@apollo/client/utilities#argumentsObjectFromField TypeScript Examples

The following examples show how to use @apollo/client/utilities#argumentsObjectFromField. 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
processWriteResult(options: Cache.WriteOptions<any, any>) {
    const { dataId, variables, result } = options;
    const { entityTypeMap, cache, invalidationPolicyManager } = this.config;

    if (isPlainObject(result)) {
      this.processWriteSubResult(result);
    }

    if (dataId && isQuery(dataId) && isPlainObject(result)) {
      this.getFieldsForQuery(options).forEach((field) => {
        const fieldName = field.name.value;
        const typename = entityTypeMap.readEntityById(
          makeEntityId(dataId, fieldName)
        )?.typename;

        if (typename) {
          const storeFieldName = cache.policies.getStoreFieldName({
            typename,
            field,
            fieldName,
            variables,
          });

          const fieldArgs = argumentsObjectFromField(field, variables);
          const fieldVariables = variables ?? (fieldArgs !== null ? {} : undefined);

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

          // Write a query to the entity type map at `write` in addition to `merge` time so that we can keep track of its variables.
          entityTypeMap.write(typename, dataId, storeFieldName, fieldVariables, fieldArgs);
          entityTypeMap.write(typename, dataId, storeFieldNameForQuery, fieldVariables, fieldArgs);

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

          invalidationPolicyManager.runWritePolicy(typename, {
            parent: {
              id: dataId,
              fieldName,
              storeFieldName,
              ref: makeReference(dataId),
              variables: fieldVariables,
              args: fieldArgs,
            },
          });
        }
      });
    } else if (dataId) {
      const typename = entityTypeMap.readEntityById(makeEntityId(dataId))
        ?.typename;

      if (typename) {
        const renewalPolicy = invalidationPolicyManager.getRenewalPolicyForType(
          typename
        );
        if (
          renewalPolicy === RenewalPolicy.WriteOnly ||
          renewalPolicy === RenewalPolicy.AccessAndWrite
        ) {
          entityTypeMap.renewEntity(dataId);
        }

        invalidationPolicyManager.runWritePolicy(typename, {
          parent: {
            id: dataId,
            ref: makeReference(dataId),
            variables,
          },
        });
      }
    }
  }