RANGE_ADD 配置中指定的任何 rangeBehaviors 都不匹配

No match on any of the rangeBehaviors specified in RANGE_ADD config

我有以下突变:

export default class AddTaskMutation extends Relay.Mutation {
  static fragments = {
    classroom: () => Relay.QL`
      fragment on Classroom {
        id,
      }`,
  };

  getMutation() {
    return Relay.QL`mutation { addTask }`;
  }

  getFatQuery() {
    return Relay.QL`
      fragment on AddTaskPayload {
        classroom {
          taskList,
        },
        taskEdge,
      }
    `;
  }

  getConfigs() {    
    let rangeBehaviors = {};
    let member_id = 'hasNewMessages(true) member_id(' + Global.fromGlobalId(this.props.createdByMember)['id'] + ')';
    rangeBehaviors[''] = 'append';
    rangeBehaviors['hasToDo(true)'] = 'append';
    rangeBehaviors['hasToDo(false)'] = 'append';
    rangeBehaviors['isStart(true)'] = 'ignore';
    rangeBehaviors['isStart(false)'] = 'append';
    rangeBehaviors[`${member_id}`] = 'append';
    rangeBehaviors['hasNewMessages(true) member_id(null)'] = 'ignore';
    return [
      {
        type: 'RANGE_ADD',
        parentName: 'classroom',
        parentID: this.props.classroomId,
        connectionName: 'taskList',
        edgeName: 'taskEdge',
        rangeBehaviors,
      }
    ];
  }

  getVariables() {
    return {
      title: this.props.title,
      instruction: this.props.instruction,
      start_date: this.props.start_date,
      end_date: this.props.end_date,
      is_archived: this.props.is_archived,
      is_published: this.props.is_published,
      is_preview: this.props.is_preview,
      productId: this.props.productId,
      classroomId: this.props.classroomId,
      createdByMember: this.props.createdByMember,
      subTasks: this.props.subTasks,
      students: this.props.students,
    };
  }
}

当 运行 我的应用程序收到以下 2 个警告时:

warning.js:44 Warning: RelayMutation: The connection taskList{hasNewMessages:true,member_id:null} on the mutation field classroom that corresponds to the ID Classroom:35 did not match any of the rangeBehaviors specified in your RANGE_ADD config. This means that the entire connection will be refetched. Configure a range behavior for this mutation in order to fetch only the new edge and to enable optimistic mutations or use refetch to squelch this warning.

warning.js:44 Warning: Using null as a rangeBehavior value is deprecated. Use ignore to avoid refetching a range.

由于另一个 rangeBehaviors 有效,我假设在一个行为中声明 2 个变量时一定存在语法错误 - 在本例中为 hasNewMessagesmemeber_id.

我一直在寻找这个问题的答案,但就是找不到。文档似乎也没有涵盖这种边缘情况。

编辑:我也试过 rangeBehaviors['hasNewMessages(true),member_id(null)'] = 'ignore';(逗号作为分隔符)但没有成功。

在检查 Relay 的源代码(文件 RelayMutationQuery.js)后,我可以看到它在所有 rangeBehaviors 的数组中搜索的数组键。然后我可以将我的代码更新为正确的格式。

由于我没有在网上找到任何关于这种极端情况的信息,我将post 在这里提供我的解决方案 - 也许将来有人会觉得它有用。


rangeBehavior 有 2 个(或更多)变量时,您需要用句点 (.) 分隔它们。此外,在传递 null 时,您不会显式传递它 - 只需将其从其变量中省略即可。

例如:

右:

rangeBehaviors['hasNewMessages(true).member_id()'] = 'ignore';

错误:

rangeBehaviors['hasNewMessages(true),member_id(null)'] = 'ignore';