React-Apollo 错误:“...仅支持每个 HOC 的查询、订阅或突变”

Posted

技术标签:

【中文标题】React-Apollo 错误:“...仅支持每个 HOC 的查询、订阅或突变”【英文标题】:React-Apollo error: "...only supports a query, subscription, or a mutation per HOC" 【发布时间】:2021-05-21 02:41:02 【问题描述】:

我正在尝试以这种格式创建两个单独的突变:

import gql from 'graphql-tag';

const MUTATION = gql`
  mutation setItem($item:String!) 
    setItem(item:$item)
  ,
  mutation setAnotherItem($setAnotherItem:Bool) 
    setAnotherItem(setAnotherItem:$setAnotherItem)
  
`;

但我收到此错误:

Invariant Violation: react-apollo only supports a query, subscription, or a mutation per HOC. [object Object] had 0 queries, 0 subscriptions and 2 mutations. You can use 'compose' to join multiple operation types to a component

什么是构造这个以使两个突变都可用的正确方法?

【问题讨论】:

【参考方案1】:

您只需键入一次mutation 并嵌套在(变异)字段中:

import gql from 'graphql-tag';

const MUTATION = gql`
  mutation MyMutation ($item: String, $setAnotherItem: Book) 
    setItem(item: $item)  ...
    setAnotherItem(setAnotherItem: $setAnotherItem) ...
  
`;

【讨论】:

以上是关于React-Apollo 错误:“...仅支持每个 HOC 的查询、订阅或突变”的主要内容,如果未能解决你的问题,请参考以下文章