在 SearchType 上中继 RANGE_ADD queryConfig

Relay RANGE_ADD queryConfig on SearchType

如果新项目的 parent 类似于没有 ID 属性的 SearchType,我正在尝试了解应该使用什么配置进行 RANGE_ADD 突变。 在这种情况下应该使用什么作为 parentId 或者应该如何配置这个突变?如果我不指定 parentID 那么新创建的项目将不会添加到中继存储。

我在 Relay 仓库中阅读了很多问题,但我仍然不清楚如何正确处理这种突变。

在您的情况下,可以创建具有静态 ID 的 SearchType 对象。这是一个使用 graphql-ruby 的例子,但这个概念应该适用于任何实现:

https://github.com/gauravtiwari/relay-rails-blog/blob/8b257c3d32b7ef9d1aab4420257d8b6471006d0a/app/models/viewer.rb

客户端实现如下所示:

class SearchPostMutation extends Relay.Mutation {
  getMutation() {
    return Relay.QL`mutation {searchPosts}`;
  }

  getVariables() {
    return {
      // For debug, you can hard code your SearchType object's id here. 
      id: 'SA9bdDbpc4QtwzppZD0+IbJsE4QgzQ==',
    };
  }

  getConfigs() {
    return [{
      type: 'RANGE_ADD',
      parentName: 'searchPost',
      // For debug, you can hard code your SearchType object's id here. 
      parentID: 'SA9bdDbpc4QtwzppZD0+IbJsE4QgzQ==',
      connectionName: 'posts',
      edgeName: 'newPostEdge',
      rangeBehaviors: {
        '': 'prepend',
      },
    }];
  }
}