website/versioned_docs/version-v17.0.0/guided-tour/updating-data/local-data-updates.md
import DocsRating from '@site/src/core/DocsRating'; import {OssOnly, FbInternalOnly} from 'docusaurus-plugin-internaldocs-fb/internal';
There are a couple of APIs that Relay provides in order to make purely local updates to the Relay store (i.e. updates not tied to a server operation).
Note that local data updates can be made both on client-only data, or on regular data that was fetched from the server via an operation.
To make updates using an updater function, you can use the commitLocalUpdate API:
import type {Environment} from 'react-relay';
const {commitLocalUpdate, graphql} = require('react-relay');
function commitCommentCreateLocally(
environment: Environment,
feedbackID: string,
) {
return commitLocalUpdate(environment, store => {
// Imperatively mutate the store here
});
}
module.exports = {commit: commitCommentCreateLocally};
commitLocalUpdate update simply takes an environment and an updater function.
updater takes a store argument, which is an instance of a RecordSourceSelectorProxy; this interface allows you to imperatively write and read data directly to and from the Relay store. This means that you have full control over how to update the store: you can create entirely new records, or update or delete existing ones.commitLocalUpdate does not accept a second parameter. This is because there is no associated network response.commitPayload takes an OperationDescriptor and the payload for the query, and writes it to the Relay Store. The payload will be resolved like a normal server response for a query, and will also resolve Data Driven Dependencies that are passed as JSResource, requireDefer, etc.
import type {FooQueryRawResponse} from 'FooQuery.graphql'
const {createOperationDescriptor} = require('relay-runtime');
const operationDescriptor = createOperationDescriptor(FooQuery, {
id: 'an-id',
otherVariable: 'value',
});
const payload: FooQueryRawResponse = {...};
environment.commitPayload(operationDescriptor, payload);
OperationDescriptor can be created by createOperationDescriptor; it takes the query and the query variables.@raw_response_type to the query.