function rangeAdd()

in packages/relay-runtime/mutations/RelayDeclarativeMutationConfig.js [170:236]


function rangeAdd(
  config: RangeAddConfig,
  request: ConcreteRequest,
): ?SelectorStoreUpdater<mixed> {
  const {parentID, connectionInfo, edgeName} = config;
  if (!parentID) {
    warning(
      false,
      'RelayDeclarativeMutationConfig: For mutation config RANGE_ADD ' +
        'to work you must include a parentID',
    );
    return null;
  }
  const rootField = getRootField(request);
  if (!connectionInfo || !rootField) {
    return null;
  }
  return (store: RecordSourceSelectorProxy, data: ?mixed) => {
    const parent = store.get(parentID);
    if (!parent) {
      return;
    }
    const payload = store.getRootField(rootField);
    if (!payload) {
      return;
    }
    const serverEdge = payload.getLinkedRecord(edgeName);
    for (const info of connectionInfo) {
      if (!serverEdge) {
        continue;
      }
      const connection = ConnectionHandler.getConnection(
        parent,
        info.key,
        info.filters,
      );
      if (!connection) {
        continue;
      }
      const clientEdge = ConnectionHandler.buildConnectionEdge(
        store,
        connection,
        serverEdge,
      );
      if (!clientEdge) {
        continue;
      }
      switch (info.rangeBehavior) {
        case 'append':
          ConnectionHandler.insertEdgeAfter(connection, clientEdge);
          break;
        case 'prepend':
          ConnectionHandler.insertEdgeBefore(connection, clientEdge);
          break;
        default:
          warning(
            false,
            'RelayDeclarativeMutationConfig: RANGE_ADD range behavior `%s` ' +
              'will not work as expected in RelayModern, supported range ' +
              "behaviors are 'append', 'prepend'.",
            info.rangeBehavior,
          );
          break;
      }
    }
  };
}