Relay Modern Mutations, RANGE_ADD / Append

1k views Asked by At

I have a collection and a mutation to add a new item to it. I haven't been able to get Relay Modern to update the UI after a successful mutation.

I've got a PaginationContainer setup with the following query: prop

  {
    query: graphql`
      fragment ContactsList_query on WPQuery {
          id
          contacts: posts(
              first: $count,
              after: $cursor
              post_type: $postType,
              order: $order,
              category_name: $categoryName
          ) @connection(key: "ContactsList_contacts" ) {
            edges {
              node {
                id
                ...ContactListItem_contact
              }
            }
            pageInfo {
              hasNextPage
              endCursor
            }
          }
      }
  `
  },

That fetches correctly. I've then got a mutation to add a contact to this list. Neither the config RANGE_ADD or the updater: callback techniques work at all.

I'm triggering this mutation like so

 onSave = (fields) => {
    insertPost(
      fields.toJS(),
      this.props.query.id,
      this.props.relay.environment
    );
  }

No errors, just nothing updates.

const mutation = graphql`
  mutation InsertPostMutation(
    $data: InsertPostInput!
  ) {
    insert_post(input: $data) {
      wp_query {
        id
      }
      postEdge {
        node {
          id
          title
        }
      }
    }
  }
`;

export default function insertPost(data, id, environment) {
  const variables = {
    data,
  };

  commitMutation(
    environment,
    {
      mutation,
      variables,
      onCompleted: (response, errors) => {
        console.log('Response received from server.')
      },
      onError: err => console.error(err),
      configs: [{
        type: 'RANGE_ADD',
        parentID: id,
        connectionInfo: [{
          key: 'ContactsList_contacts',
          rangeBehavior: 'append',
        }],
        edgeName: 'postEdge'
      }],
      // updater: (store) => {
      //   // const inspector = new RecordSourceInspector(store)
      //   const payload = store.getRootField('insert_post')
      //   const newEdge = payload.getLinkedRecord('postEdge')
      //   const proxy = store.get(id)
      //   // Conn is always undefined here
      //   const conn = ConnectionHandler.getConnection(proxy, 'ContactsList_contacts')
      //   ConnectionHandler.insertEdgeAfter(conn, newEdge)
      // }
    },
  );
}
1

There are 1 answers

0
Tim On

Well, I was able to fix this by changing the line

@connection(key: "ContactsList_contacts")

To

@connection(key: "ContactsList_contacts", filters: [])

Seems it couldn't find the connection otherwise...

https://facebook.github.io/relay/docs/pagination-container.html#connection-directive

Then using the updater function the connection was found.