graphql - 带参数的连接上的 RANGE_DELETE 和 RANGE_ADD

标签 graphql relayjs

我正在尝试实现一个突变,即连接上的 RANGE_DELETE,但仅限于指定的参数。

场景:登录用户Viewer可以查看其他用户的请求并批准它们。

User 类型有一个字段 viewableRequests,它是一个中继连接,并带有一个额外的可选参数 state,它将过滤请求,只保留对应状态的。

架构:

type User {
  id: ID
  viewableRequests(first: Int, [...], state: RequestStateEnum): ViewableRequestsConnection
}

type ViewableRequestsConnection {
  edges: [RequestEdge]
  ...
}

type RequestEdge {
  cursor: GraphQLString
  node: Request
}

type Request {
  id: ID
  user_id: Int
  state: RequestStateEnum
}

enum RequestStateEnum {
  pending
  approved
}

因此,如果我调用 viewableRequests(state:ending),我只会收到处于待处理状态的请求,如果我调用 viewableRequests(state:approved),我只会收到处于挂起状态的请求。获取状态已批准的请求。

如何编写一个 ApproveRequest 突变,对 viewableRequests(state:ending) 连接执行 RANGE_DELETE,并在 viewableRequests(state) 上执行 RANGE_ADD :已批准) 连接,以及我应该如何塑造 getOptimisticResponse() 方法?

这是我的突变模式:

type ApproveRequestInput {
  requestId: ID
}

type ApproveRequestPayload {
  viewer: User
  approvedRequest: Request
}

mutation {
  approveRequest($input: ApproveRequestInput): ApproveRequestPayload
}

这是我当前的代码:

import Relay, {
  Mutation,
} from 'react-relay';

export default class ApproveRequestMutation extends Mutation {
  static fragments = {
    viewer: () => Relay.QL`
      fragment on User {
        id
      }
    `,
    request: () => Relay.QL`
      fragment on Request {
        id
      }
    `,
  };

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

  getVariables() {
    return {
      requestId: this.props.request.id,
    };
  }

  getFatQuery() {
    return Relay.QL`
      fragment on ApproveRequestPayload @relay(pattern: true) {
        viewer {
          pendingRequests: viewableRequests(state: pending)
          approvedRequests: viewableRequests(state: approved)
        }
        approvedRequest
      }
    `;
  }

  getOptimisticResponse() {
    return {
      // What should I write here ?
    };
  }

  getConfigs() {
    // The RANGE_DELETE config is not working ; how can I make it work ?
    return [{
      type: 'RANGE_DELETE',
      parentName: 'viewer',
      parentID: this.props.viewer.id,
      connectionName: 'pendingRequests',
      deletedIDFieldName: ['approvedRequest'],
      pathToConnection: ['viewer', 'pendingRequests'],
    }];
    // How can I can add here a RANGE_ADD config for the `approvedRequests` connection ?
    // I guess I must add an `approvedRequestEdge` on the mutation payload, but what is the config then ?
  }

}

最佳答案

我有一个非常相似的设置。一名用户在提交之前会看到处于 IN_PROGRESS 状态的案例。当他这样做时,我希望它从 IN_PROGRESS 移动到 WAITING。我刚刚通过以下配置让它工作:

return [
  {
    type: "FIELDS_CHANGE",
    fieldIDs: {
      case: this.props.caseId
    }
  },
  {
    type: "RANGE_ADD",
    parentName: "viewer",
    parentID: this.props.viewer.__dataID__, // eslint-disable-line
    connectionName: "cases",
    edgeName: "case",
    rangeBehaviors: {
      "": "refetch",
      "type(IN_PROGRESS)": "remove",
      "type(WAITING)": "prepend"
    }
  }
];

我猜用 state(pending) 替换 type(IN_PROGRESS) 应该可以!

关于graphql - 带参数的连接上的 RANGE_DELETE 和 RANGE_ADD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40914883/

相关文章:

asp.net - 使用 GraphQL 和 Hot Choclate 将 IDictionary<string, object> 序列化为动态 JSON

reactjs - 使用 Relay 和 GraphQL 进行查询

node.js - graphQL - 类型必须是输出类型

json - Gatsby:在页面上组合两个 graphql 源(.json 和 .jpg 源)

javascript - Relay Modern - 如何在没有 fragmentContainer 的情况下使用 fetchQuery

javascript - 现有工作 graphQL 服务器的中继客户端?

relayjs - 如何在中继现代(实验)中共享用于创建和更新对象的相同表单?

reactjs - React Native graphql 查询与中继不工作

graphql - React Relay 对查询而不是片段进行分页

express - 无法读取未定义的属性 'context' - GraphQL