缺少对象 GraphQL+Apollo 的选择集错误 [英] Missing selection set for object GraphQL+Apollo error

查看:21
本文介绍了缺少对象 GraphQL+Apollo 的选择集错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组触发某些类型弹出窗口的本地状态的突变.它们通常是这样设置的:

I have a set of mutations that trigger the local state of certain types of popups. They're generally set up like this:

  openDialog: (_, variables, { cache }) => {
    const data = {
      popups: {
        ...popups,
        dialog: {
          id: 'dialog',
          __typename: 'Dialog',
          type: variables.type
        }
      }
    };

    cache.writeData({
      data: data
    });
    return null;
  }

我传入的默认值如下所示:

And the defaults I pass in look like:

const defaults = {
  popups: {
    __typename: TYPENAMES.POPUPS,
    id,
    message: null,
    modal: null,
    menu: null,
    dialog: null
  }
};

它们在我的 React 代码中的使用方式是使用 Mutation 包装器组件,如下所示:

The way they're used in my React code is with a Mutation wrapper component, like so:

const OPEN_ALERT_FORM = gql`
  mutation AlertOpenDialog($type: String!) {
    openDialog(type: $type) @client
  }
`;

class Alert extends Component {
  render() {
    return (
      <Mutation mutation={OPEN_ALERT_FORM} variables={{ type: ALERT_FORM }}>
        {openDialog => {
          return (
            <Button
              classes="alert-button"
              onClick={openDialog}
              label="Trigger Alert"
            />
          );
        }}
      </Mutation>
    );
  }
}

对于我的各种弹出窗口(我有 3 或 4 个不同的弹出窗口,例如 menumodal),打开和关闭它们的更改看起来都一样,只是不同类型名称和内容等.但是,对于对话框,单击它们时出现此错误:

For my various popups (I have 3 or 4 different ones, like menu and modal), the mutations to open and close them all look the same, just different typenames and content etc. But, for Dialogs, I get this error when I click on them:

网络错误:为查询字段对话框返回的 Dialog 类型的对象缺少选择集

...然后触发组件从页面上消失.另外,一旦发生这种情况,当您尝试单击它们时,所有其他弹出类型都会消失,然后重新抛出该错误,或​​者说:

...and then the triggering component disappears from the page. Plus, once that happens, all other popup types disappear when you try clicking on them, and either re-throw that error, or say:

未捕获的错误:引发了跨源错误.React 无权访问开发中的实际错误对象.

我已经尝试重新编写对话框以匹配其他弹出类型,并重新编写组件,但我仍然收到此错误.它似乎是特定于对话+阿波罗的.这个问题的根源是什么?它不能是后端的东西,因为这只是与本地 Apollo 打交道.我以前从未见过这个错误,我不知道从哪里开始.

I've tried re-writing dialogs to match up with other popup types, and re-writing the components as well, but I'm still getting this error. It does appear to be dialog+Apollo specific. What could be the root of this issue? It can't be a backend thing, because this is only dealing with local Apollo. I haven't seen this error before and I'm not sure where to go from here.

推荐答案

通过将 dialog 字段视为字符串而不是对象来解决此问题.将函数更改为 this 可以解决问题并使错误消失:

The fix for this turned out to work by treating the dialog field as a string instead of an object. Changing the function to this did the trick and made the errors go away:

  openDialog: (_, variables, { cache }) => {
    const data = {
      popups: {
        ...popups,
        dialog: variables.type
      }
    };

    cache.writeData({
      data: data
    });
    return null;
  }

这篇关于缺少对象 GraphQL+Apollo 的选择集错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆