Apollo客户端(反应):是否可以从查询对象中获取规范化数据?

时间:2018-07-28 22:56:19

标签: graphql react-apollo apollo-client

Query.data的{​​{1}}对象以嵌套形式提供数据,如下所示:

react-apollo

我有一个用例,需要规范化的数据,就像这样:

{
    "findUser": [
      {
        "__typename": "User",
        "id": "5a6efb94b0e8c36f99fba013",
        "email": "Lloyd.Nikolaus@yahoo.com",
        "posts": [
          {
            "__typename": "BlogPost",
            "id": "5a6efb94b0e8c36f99fba016",
            "title": "Dolorem voluptatem molestiae",
            "comments": [
              {
                "__typename": "Comment",
                "id": "5a6efb94b0e8c36f99fba019",
                "message":
                  "Alias quod est voluptatibus aut quis sunt aut numquam."
              },
              {
                "__typename": "Comment",
                "id": "5a6efb94b0e8c36f99fba01b",
                "message": "Harum quia asperiores nemo."
              },
              {
                "__typename": "Comment",
                "id": "5a6efb94b0e8c36f99fba01c",
                "message": "Vel veniam consectetur laborum."
              },
              {
                "__typename": "Comment",
                "id": "5a6efb94b0e8c36f99fba01e",
                "message":
                  "Possimus beatae vero recusandae beatae quas ut commodi laboriosam."
              }
            ]
          }
        ]
      }
    ]
}

Apollo早已准备好以规范化形式缓存数据的持久性,是否有一种方法可以以规范化形式访问相关的响应数据,而无需像graphql-normalizr库那样使用单独的规范化过程?

如果可能的话,我想避免双重标准化的开销。

1 个答案:

答案 0 :(得分:1)

您应该能够使用client.readFragment()

读取缓存中的拼合数据

在这里查看文档: https://www.apollographql.com/docs/react/advanced/caching.html#readfragment