使用Query获取深度本地数据

问题描述 投票:0回答:0

我们使用 Apollo Client 作为我们的状态管理器。

我们成功地改变了一个深层对象,但没有查询连接到 id 的特定数据。

  • 下面有详细的解释。
  • 底部查询失败

在这种情况下,我们正在保存一个带有模板选项的嵌套对象。

const clientTemplate = {
    __typename: 'ClientTemplate',
    id: 'template',
    template: 'grid',
    displayGroups: [{
        __typename: 'ClientDisplayGroup',
        id: 'display-group-1',
        displays: [
        {
            __typename: 'ClientDisplay',
            id: 'display-1',
            label: 'Display 1',
            show: true,
            options: [
                {
                    __typename: 'ClientOption',
                    id: 'display-1-option-1',
                    value: 'normal'
                }
            ]
        }]
    }]
}

const DISPLAY_GROUP_MUTATION = gql`
    mutation TemplateClient {
        clientTemplate @client {
            id
            template
            options {
                id
                value
            }
            displayGroups {
                id
                displays {
                    id
                    show
                    options {
                        id
                        value
                    }
                }
            }
        }
    }
`

当用户更改显示选项时,我们会更新该选项的数据。

const DISPLAY_SHOW_MUTATION = gql`
    mutation ClientDisplay {
        clientDisplay @client {
            id
            show
        }
    }
`

运行 TemplateClient 和 ClientDisplay 突变时,数据会为此查询更新。

const CLIENT_TEMPLATE = gql`
    query ClientTemplate {
        clientTemplate @client {
            id
            template
            options {
                id
                value
            }
            displayGroups {
                id
                displays {
                    id
                    show
                    options {
                        id
                        value
                    }
                }
            }
        }
    }
`

到目前为止,一切都很好!

但我没有成功的是根据 id 获取特定的 DisplayGroup。这是示例查询。

const CLIENT_DISPLAY_GROUP = gql`
    query ClientDisplayGroup($id: ID!) {
        clientDisplayGroup(id: $id) @client {
            id
            displays {
                id
                show
                options {
                    id
                    value
                }
            }
        }
    }
`
const { data, loading, error } = useQuery(CLIENT_DISPLAY_GROUP, {
    variables: { id: 'display-group-1' }
})

useQuery 数据为空,所以它在缓存中找不到任何数据。但是 ClientDisplayGroup:display-group-1 存在,正如我提到的。 ClientTemplate 返回数据!

我怀疑是否有可能改变一个深层对象但不查询相同的数据。有没有人可以解释我们在这里缺少什么?

提前谢谢你!

reactjs apollo apollo-client graphql-js
© www.soinside.com 2019 - 2024. All rights reserved.