Apollo GQL USEMUNT不会返回数据字段

发布于 2025-02-12 01:24:26 字数 1238 浏览 1 评论 0原文

我有以下在沙箱中工作正常的突变(它创建并返回书签):

mutation CreateBookmark ($options: BookmarkInput!) {
  createBookmark(options: $options) {
    bookmark {
      id
      title
      url
    }
  }
}

当我尝试从我的React应用程序创建书签时,正在创建书签,但数据不确定。

这是我的代码:

const CREATE_BOOKMARK = gql`
    mutation CreateBookmark($options: BookmarkInput!) {
        createBookmark(options: $options) {
            bookmark {
                id
                title
                url
            }
        }
    }
`;

const [mutateFunction, {data, error, loading}] = useMutation(CREATE_BOOKMARK);

mutateFunction({
    variables: {
        options: {
            title: bookmark.title,
            url: bookmark.url,
        },
    },
});

当我尝试console.log(data)时,它会返回未定义。

我还试图不破坏这样返回的对象:

const [mutateFunction, obj] = useMutation(CREATE_BOOKMARK);

当i console.log(obj)时,它返回以下内容:

{
  error: ...,
  loading: ...,
  client: ...,
  reset: ...
}

如您所见,没有数据字段。为什么会发生这种情况?

I have the following mutation that works just fine in the Sandbox (it creates and returns the bookmark):

mutation CreateBookmark ($options: BookmarkInput!) {
  createBookmark(options: $options) {
    bookmark {
      id
      title
      url
    }
  }
}

When I try to create a bookmark from my react app, the bookmark is being created but data is undefined.

This is my code:

const CREATE_BOOKMARK = gql`
    mutation CreateBookmark($options: BookmarkInput!) {
        createBookmark(options: $options) {
            bookmark {
                id
                title
                url
            }
        }
    }
`;

const [mutateFunction, {data, error, loading}] = useMutation(CREATE_BOOKMARK);

mutateFunction({
    variables: {
        options: {
            title: bookmark.title,
            url: bookmark.url,
        },
    },
});

When I try to console.log(data) it returns undefined.

I have also tried to not destructure the object that is being returned like this:

const [mutateFunction, obj] = useMutation(CREATE_BOOKMARK);

When I console.log(obj), it returns this:

{
  error: ...,
  loading: ...,
  client: ...,
  reset: ...
}

As you can see, there is no data field. Why is this happening?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文