【问题标题】:Relay Modern updater ConnectionHandler.getConnection() returns undefined when parent record is root当父记录为根时,Relay Modern updater ConnectionHandler.getConnection() 返回 undefined
【发布时间】:2020-06-14 23:48:19
【问题描述】:

调试更新:

因此,我们在调试方面更进一步,似乎 'client:root' 本身根本无法访问连接。

为了调试完整的 store,我们在从 relay/environment 导出 store 变量后,在 updater 函数中添加了这一行。

console.log(relayEnvStore.getSource().toJSON())

如果我使用带有特定字符串client:root:__ItemList_items_connection.get(),我可以访问我一直在寻找的记录,但它肯定不漂亮。

const testStore = store.get('client:root:__ItemList_items_connection')

console.log(testStore.getLinkedRecords('edges'))

原文:

我正在使用 Relay Modern 并尝试在使用更新程序完成 updateItem 突变后更新缓存。对ConnectionHandler.getConnection('client:root', 'ItemList_items') 的调用返回未定义。

我不确定是因为我试图使用 'client:root' 作为我的父记录,还是我的代码有问题。有没有人发现自己有类似的问题?

这是 paginationContainer:

const ItemListPaginationContainer = createPaginationContainer(
  ItemList,
  {
    node: graphql`
      fragment ItemList_node on Query
        @argumentDefinitions(count: { type: "Int", defaultValue: 3 }, cursor: { type: "String" }) {
        items(first: $count, after: $cursor) @connection(key: "ItemList_items") {
          edges {
            cursor
            node {
              id
              name
            }
          }
          pageInfo {
            hasNextPage
            hasPreviousPage
            startCursor
            endCursor
          }
        }
      }
    `
  },
  {
    direction: 'forward',
    getConnectionFromProps: props => props.node && props.node.items,
    getVariables(props, { count, cursor }) {
      return {
        count,
        cursor
      }
    },
    query: graphql`
      query ItemListQuery($count: Int!, $cursor: String) {
        ...ItemList_node @arguments(count: $count, cursor: $cursor)
      }
    `
  }
)

这是突变:

const mutation = graphql`
  mutation UpdateItemMutation($id: ID!, $name: String) {
    updateItem(id: $id, name: $name) {
      id
      name
    }
  }
`

这是更新程序:

updater: (store) => {
  const root = store.getRoot()
  const conn = ConnectionHandler.getConnection(
    root, // parent record
    'ItemList_items' // connection key
    )

  console.log(conn)
},

【问题讨论】:

    标签: relayjs relay relaymodern


    【解决方案1】:

    原来我的环境设置不正确。每次我进行查询或突变时,商店都会自行重置,因此我无法访问任何连接。我最初有以下内容:

    export default server => {
      return new Environment({
        network: network(server),
        store: new Store(new RecordSource())
      })
    }
    

    通过此更改可以访问所有连接:

    const storeObject = new Store(new RecordSource())
    
    export default server => {
      return new Environment({
        network: network(server),
        store: storeObject
      })
    } 
    

    【讨论】:

      猜你喜欢
      • 2017-11-27
      • 2019-09-23
      • 2017-10-22
      • 2018-05-17
      • 2017-11-11
      • 1970-01-01
      • 2017-10-15
      • 2022-10-04
      • 2018-05-15
      相关资源
      最近更新 更多