简体   繁体   English

如何从Apollo graphql客户端状态检索值

[英]How to retrieve value from Apollo graphql client state

Looking at Apollo link state I can see how to write/update a value to local store/cache by using a mutation or directly by using cache.writeData({ data }) . 查看Apollo链接状态,我可以看到如何通过使用突变或直接使用cache.writeData({ data })将值写入/更新到本地存储/缓存中。

But how do i retrieve that cached value directly to use as a variable for a mutation? 但是,我如何直接检索该缓存的值以用作突变的变量? Or do I need chain 2 queries using the @client on first one? 还是我需要在第一个查询中使用@client进行链2查询?

Note: if I use the ApolloProvider I get access to cache but it looks like this: {cache.cache.data.$ROOT.data.myVariable} 注意:如果我使用ApolloProvider,则可以访问缓存,但它看起来像这样: {cache.cache.data.$ROOT.data.myVariable}

You could nest the Mutation component within a Query component. 您可以将Mutation组件嵌套在Query组件中。 Like below. 像下面。 If you do it a lot then you could create your own wrapper component. 如果您经常这样做,则可以创建自己的包装器组件。

Or you could do a client.readQuery as seen in the docs here: https://www.apollographql.com/docs/react/advanced/caching.html#readquery . 或者,您可以执行一个client.readQuery ,如此处的文档所示: https : client.readQuery

const ADD_TODO = gql`
  mutation addTodo($type: String!) {
    addTodo(type: $type) {
      id
      type
    }
  }
`;

<Query query={gql`
  {
    myVariable @client
  }
`>
 {({ data: { myVariable }}) => {

  return (
    <Mutation mutation={ADD_TODO}>
      {(addTodo, { data }) => (
        <div>
          <form
            onSubmit={e => {
              e.preventDefault();
              addTodo({ variables: { type: myVariable } });
              input.value = "";
            }}
          >
            <input
              ref={node => {
                input = node;
              }}
            />
            <button type="submit">Add Todo</button>
          </form>
        </div>
      )}
    </Mutation>
</Query>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Redux客户端从REST转换为GraphQL Apollo吗? - Converting redux client from REST to GraphQL Apollo? 使用graphql和apollo客户端获取数据时将数据存储在状态中 - storing data in state when data is fetched using graphql and apollo client 如何使用 Apollo GraphQL 更改查询值(其中:{}) - How change query value (where : { }) with Apollo GraphQL React Apollo:从Component状态动态更新GraphQL查询 - React Apollo: Dynamically update GraphQL query from Component state 如何在React中的Apollo客户端突变组件中包装GraphQL突变 - How to wrap GraphQL mutation in Apollo client mutation component in React 你如何使用 Apollo Client 有效地批量处理 GraphQL 突变? - How do you effectively batch GraphQL mutations using Apollo Client? 如何在Vanilla JS中使用Apollo Client创建GraphQL订阅 - How do I create a GraphQL subscription with Apollo Client in Vanilla JS 我正在从 apollo-link-state 迁移到 @apollo/client,如何在不使用 cache.writeData 的情况下将默认数据写入 @apollo/client 的 InMemoryCache? - I am migrating from apollo-link-state to @apollo/client, how do I write default data to InMemoryCache of @apollo/client without using cache.writeData? 使用Apollo客户端进行外部状态管理? - External state management with Apollo client? GraphQL变异后的updateQueries无法与Apollo客户端一起使用 - updateQueries after GraphQL mutation not working with the Apollo client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM