Files
30-seconds-of-code/node_modules/relay-runtime/lib/commitRelayModernMutation.js
2019-08-20 15:52:05 +02:00

80 lines
3.2 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* @format
*/
'use strict';
/**
* Higher-level helper function to execute a mutation against a specific
* environment.
*/
function commitRelayModernMutation(environment, config) {
!require("./isRelayModernEnvironment")(environment) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'commitRelayModernMutation: expected `environment` to be an instance of ' + '`RelayModernEnvironment`.\n' + 'When using Relay Modern and Relay Classic in the same ' + 'application, ensure mutations use Relay Compat to work in ' + 'both environments.\n' + 'See: http://facebook.github.io/relay/docs/relay-compat.html') : require("fbjs/lib/invariant")(false) : void 0;
var _environment$unstable = environment.unstable_internal,
createOperationDescriptor = _environment$unstable.createOperationDescriptor,
getRequest = _environment$unstable.getRequest;
var mutation = getRequest(config.mutation);
if (mutation.params.operationKind !== 'mutation') {
throw new Error('commitRelayModernMutation: Expected mutation operation');
}
if (mutation.kind !== 'Request') {
throw new Error('commitRelayModernMutation: Expected mutation to be of type request');
}
var optimisticResponse = config.optimisticResponse,
optimisticUpdater = config.optimisticUpdater,
updater = config.updater;
var configs = config.configs,
onError = config.onError,
variables = config.variables,
uploadables = config.uploadables;
var operation = createOperationDescriptor(mutation, variables); // TODO: remove this check after we fix flow.
if (typeof optimisticResponse === 'function') {
optimisticResponse = optimisticResponse();
process.env.NODE_ENV !== "production" ? require("fbjs/lib/warning")(false, 'commitRelayModernMutation: Expected `optimisticResponse` to be an object, ' + 'received a function.') : void 0;
}
if (process.env.NODE_ENV !== "production") {
if (optimisticResponse instanceof Object) {
require("./validateMutation")(optimisticResponse, mutation, config.variables);
}
}
if (configs) {
var _RelayDeclarativeMuta = require("./RelayDeclarativeMutationConfig").convert(configs, mutation, optimisticUpdater, updater);
optimisticUpdater = _RelayDeclarativeMuta.optimisticUpdater;
updater = _RelayDeclarativeMuta.updater;
}
return environment.executeMutation({
operation: operation,
optimisticResponse: optimisticResponse,
optimisticUpdater: optimisticUpdater,
updater: updater,
uploadables: uploadables
}).subscribeLegacy({
onNext: function onNext(payload) {
// NOTE: commitRelayModernMutation has a non-standard use of
// onCompleted() by calling it on every next value. It may be called
// multiple times if a network request produces multiple responses.
var onCompleted = config.onCompleted;
if (onCompleted) {
var snapshot = environment.lookup(operation.fragment);
onCompleted(snapshot.data, payload.errors);
}
},
onError: onError
});
}
module.exports = commitRelayModernMutation;