How to use optimisticUpdater and updater in modern relays to create without edges?

How to update ui with a new element in a relay?

All the other CRUDs work for me, but I can’t figure out how to update the update to commit for a new item. When I refresh the page there. All examples use a viewer, which I do not have in my schema.

schema.graphql

type Note {
  id: String
  avatar: String
  message: String
  date: String
}

create_mutation.js

    export function createMutation({ mutation, variables, environment, 
      create 
    }) {

    const fragment = mutation().fragment;
    const operation = fragment.selections[0].name;
    const mutationName = fragment.name;
    const type = fragment.selections[0].concreteType;
    const id = 'client:${operation}:${cuid()}';
    const config = {
      mutation,
      variables,
      optimisticUpdater: proxyStore => {
        // 1. Create mock record that to be added to the store
        const record = proxyStore.create(id, type);
        Object.keys({ ...variables, id }).forEach(key =>
          record.setValue(key, `${key}`)
        );

        const viewerProxy = proxyStore.get(operation);
        const connection = ConnectionHandler.getConnection(record, mutationName);

        if (connection) {
          ConnectionHandler.insertEdgeAfter(viewerProxy, record);
        }
      },
      updater: proxyStore => {

      },
      onError: error => console.log(error)
    };

    commitMutation(environment, config);
  }
+4
source share

Source: https://habr.com/ru/post/1684522/


All Articles