writeFragment method

void writeFragment(
  1. FragmentRequest request,
  2. {required Map<String, dynamic> data,
  3. bool? broadcast = true}
)
inherited

Writes a GraphQL fragment to any arbitrary id. then broadcast changes to watchers unless broadcast: false

If there is more than one fragment in the provided document then a fragmentName must be provided to fragmentRequest.fragment to select the correct fragment.

For complex normalize type policies that involve custom reads, optimistic will be the default.

Will throw a PartialDataException if the data structure doesn't match that of the fragmentRequest fragment.document, or a CacheMisconfigurationException if the write fails for some other reason.

Implementation

void writeFragment(
  FragmentRequest request, {
  required Map<String, dynamic> data,
  bool? broadcast = true,
}) {
  try {
    normalizeFragment(
      // provided from cache
      write: (dataId, value) => writeNormalized(dataId, value),
      read: (dataId) => readNormalized(dataId),
      typePolicies: typePolicies,
      dataIdFromObject: dataIdFromObject,
      acceptPartialData: acceptPartialData,
      addTypename: addTypename,
      // provided from request
      document: request.fragment.document,
      idFields: request.idFields,
      fragmentName: request.fragment.fragmentName,
      variables: sanitizeVariables(request.variables)!,
      // data
      data: data,
      possibleTypes: possibleTypes,
    );
    if (broadcast ?? true) {
      broadcastRequested = true;
    }
  } on PartialDataException catch (e, stackTrace) {
    if (request.validatesStructureOf(data)) {
      throw CacheMisconfigurationException(
        e,
        stackTrace,
        fragmentRequest: request,
        data: data,
      );
    }
    rethrow;
  }
}