How to use optimisticUpdater and updater in relay modern for create without edges?
Asked Answered
I

0

6

how do I update the ui with a new item in relay modern?

I have all other CRUD working and but can't figure out the updater on commit mutation for a new item. When I refresh the page it there. All examples use viewer object which I don't have one 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);
}
Idelia answered 26/8, 2017 at 12:30 Comment(2)
have you figureed it out? Does your optimistic updater works actually?Hubsher
No, I never got it workingIdelia

© 2022 - 2024 — McMap. All rights reserved.