denormalizeOperation function

Map<String, dynamic>? denormalizeOperation({
  1. required Map<String, dynamic>? read(
    1. String dataId
    ),
  2. required DocumentNode document,
  3. String? operationName,
  4. Map<String, dynamic> variables = const {},
  5. Map<String, TypePolicy> typePolicies = const {},
  6. DataIdResolver? dataIdFromObject,
  7. bool addTypename = false,
  8. bool returnPartialData = false,
  9. bool allowDanglingReference = false,
  10. bool handleException = true,
  11. String referenceKey = kDefaultReferenceKey,
  12. Map<String, Set<String>> possibleTypes = const {},
})

Denormalizes data for a given query

Pass in a read function to read the normalized map.

If any TypePolicys were used to normalize the data, they must be provided to ensure that the appropriate normalized entity can be found.

Likewise, if a custom referenceKey was used to normalize the data, it must be provided. Otherwise, the default '$ref' key will be used.

Implementation

Map<String, dynamic>? denormalizeOperation({
  required Map<String, dynamic>? Function(String dataId) read,
  required DocumentNode document,
  String? operationName,
  Map<String, dynamic> variables = const {},
  Map<String, TypePolicy> typePolicies = const {},
  DataIdResolver? dataIdFromObject,
  bool addTypename = false,
  bool returnPartialData = false,
  bool allowDanglingReference = false,
  bool handleException = true,
  String referenceKey = kDefaultReferenceKey,
  Map<String, Set<String>> possibleTypes = const {},
}) {
  if (addTypename) {
    document = transform(
      document,
      [AddTypenameVisitor()],
    );
  }

  final operationDefinition = getOperationDefinition(document, operationName);

  final rootTypeName = resolveRootTypename(operationDefinition, typePolicies);
  final dataId = resolveDataId(
        data: {'__typename': rootTypeName},
        typePolicies: typePolicies,
        dataIdFromObject: dataIdFromObject,
      ) ??
      rootTypeName;

  final config = NormalizationConfig(
    read: read,
    variables: variables,
    typePolicies: typePolicies,
    referenceKey: referenceKey,
    fragmentMap: getFragmentMap(document),
    dataIdFromObject: dataIdFromObject,
    addTypename: addTypename,
    allowPartialData: returnPartialData,
    allowDanglingReference: allowDanglingReference,
    possibleTypes: possibleTypes,
  );

  try {
    return denormalizeNode(
      selectionSet: operationDefinition.selectionSet,
      dataForNode: read(dataId),
      config: config,
    ) as Map<String, dynamic>?;
  } on PartialDataException {
    if (handleException) {
      return null;
    }
    rethrow;
  }
}