【问题标题】:GraphQL returns null on mutation [duplicate]GraphQL 在突变时返回 null [重复]
【发布时间】:2019-06-28 15:55:43
【问题描述】:

我正在使用 GraphQL 连接到多个 RESTful 端点。我正在编写一个突变来更新用户详细信息,但是 GraphiQL 显示以下响应为 null 而不是更新的用户 ID 和名称...

{
  "data": {
    "updateUser": null
  }
}
  updateUser: (parent, args) => {
    const { id, name } = args;
    return fetch(`${url}/users/${id}`, {
      method: 'PUT',
      body: JSON.stringify({ id, name }),
      headers: { 'Content-Type': 'application/json' },
    })
      .then(res => res.json())
      .then(json => console.log(json));
  },

提前致谢。

【问题讨论】:

  • 摆脱.then(json => console.log(json))console.log 没有返回值。
  • 请参阅this answer 中的常见场景#5。

标签: javascript rest graphql


【解决方案1】:

您需要返回已解析的 json,否则 fetch 调用没有返回值。

.then(json => json);

如果你愿意

.then(json => {
  console.log(json);
  return json;
});

【讨论】:

    猜你喜欢
    • 2020-12-22
    • 2019-08-20
    • 2021-06-25
    • 2021-04-01
    • 2019-09-21
    • 2019-11-25
    • 2018-03-02
    • 2017-03-13
    • 2019-06-24
    相关资源
    最近更新 更多