使用cache.modify更新字段后未调用自定义合并函数

发布于 2025-01-10 14:53:32 字数 2153 浏览 0 评论 0原文

我为 Session 类型上的 products 字段编写了一个自定义合并函数。似乎合并函数仅在我使用其 products 初始化对象 Session:1 时被调用,而不是在我稍后使用 products 更新时被调用缓存.修改

我的合并功能:

const client = new ApolloClient({
  uri: 'http://localhost:8081/graphql',
  cache: new InMemoryCache({
    typePolicies: {
      Session: {
        fields: {
          products: {
            merge (existing, incoming) {
              // this is only being called on useQuery(HydrateSession), not useMutation(UpsertProduct)
              console.log('existing', JSON.stringify(existing, null, 2))
              console.log('incoming', JSON.stringify(incoming, null, 2))
              // remove duplicates when latestProduct has the same id as an existing product — [..., latestProduct]
              if (incoming.filter(p => p.id === incoming[incoming.length - 1].id).length > 1) return existing
              return incoming
            }
          }
        }
      }
    }
  })
})

Session的初始化:

const HydrateSession = gql`
  query {
    session {
      id
      products {
        id
      }
    }
  }
`

...

useQuery(HydrateSession)

稍后使用cache.modify更新products

const UpsertProduct = gql`
  mutation UpsertProduct($product: ProductInput!) {
    upsertProduct(product: $product) {
      id
    }
  }
`

...

const [upsertProductMutation] = useMutation(UpsertProduct)

const onClick = async () => {
  await upsertProductMutation({
    variables: {
      product: {
        id: 2
      }
    },
    update: (cache, mutationResult) => {
      cache.modify({
        id: 'Session:1',
        fields: {
          products: previous => [...previous, mutationResult.data.upsertProduct]
        }
      })
    }
  })
}

我在这里有一个完整的工作示例https://github.com/jsindos/apollo-play,运行 npm i 然后启动两个单独的进程npm startnpmserve。单击触发突变的按钮后,合并功能不会运行(从控制台中缺少 console.log 语句可以看出)。

I have written a custom merge function for the field products on type Session. It seems the merge function is only being called when I initialise the object Session:1 with its products, and not when I update products later using cache.modify.

My merge function:

const client = new ApolloClient({
  uri: 'http://localhost:8081/graphql',
  cache: new InMemoryCache({
    typePolicies: {
      Session: {
        fields: {
          products: {
            merge (existing, incoming) {
              // this is only being called on useQuery(HydrateSession), not useMutation(UpsertProduct)
              console.log('existing', JSON.stringify(existing, null, 2))
              console.log('incoming', JSON.stringify(incoming, null, 2))
              // remove duplicates when latestProduct has the same id as an existing product — [..., latestProduct]
              if (incoming.filter(p => p.id === incoming[incoming.length - 1].id).length > 1) return existing
              return incoming
            }
          }
        }
      }
    }
  })
})

Initialisation of Session:

const HydrateSession = gql`
  query {
    session {
      id
      products {
        id
      }
    }
  }
`

...

useQuery(HydrateSession)

Updating products later using cache.modify:

const UpsertProduct = gql`
  mutation UpsertProduct($product: ProductInput!) {
    upsertProduct(product: $product) {
      id
    }
  }
`

...

const [upsertProductMutation] = useMutation(UpsertProduct)

const onClick = async () => {
  await upsertProductMutation({
    variables: {
      product: {
        id: 2
      }
    },
    update: (cache, mutationResult) => {
      cache.modify({
        id: 'Session:1',
        fields: {
          products: previous => [...previous, mutationResult.data.upsertProduct]
        }
      })
    }
  })
}

I have a full working example here https://github.com/jsindos/apollo-play, run npm i and then start two separate processes with npm start and npm serve. After clicking the button triggering the mutation, the merge function is not run (as seen by the absence of console.log statements in the console).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

燃情 2025-01-17 14:53:32

modify 会绕过您定义的任何合并函数,这意味着字段始终会被您指定的值完全覆盖。

阅读文档是一件好事。

modify circumvents any merge functions you've defined, which means that fields are always overwritten with exactly the values you specify.

Reading documentation is a good thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文