【问题标题】:GraphQL with RESTful returning empty response带有 RESTful 的 GraphQL 返回空响应
【发布时间】:2019-01-25 14:06:00
【问题描述】:

我正在将GraphQLREST 端点连接起来,我确认每当我调用http://localhost:3001/graphql 时,它都会点击REST 端点并且它正在返回JSONGraphQL 服务器的响应,但我得到了从GraphQL 服务器到GUI 的空响应如下:

{
  "data": {
    "merchant": {
      "id": null
    }
  }
}

查询(手动解码):

http://localhost:3001/graphql?query={
  merchant(id: 1) {
    id
  }
}

下面是我的GraphQLObjectType 的样子:

const MerchantType = new GraphQLObjectType({
  name: 'Merchant',
  description: 'Merchant details',
  fields : () => ({
    id : {
      type: GraphQLString // ,
      // resolve: merchant => merchant.id
    },
    email: {type: GraphQLString}, // same name as field in REST response, so resolver is not requested
    mobile: {type: GraphQLString}
  })
});


const QueryType = new GraphQLObjectType({
  name: 'Query',
  description: 'The root of all... queries',
  fields: () => ({
    merchant: {
      type: merchant.MerchantType,
      args: {
        id: {type: new GraphQLNonNull(GraphQLID)},
      },
      resolve: (root, args) => rest.fetchResponseByURL(`merchant/${args.id}/`)
    },
  }),
});

来自REST 端点的响应(我也尝试使用 JSON 中的单个对象而不是 JSON 数组):

[
  {
    "merchant": {
      "id": "1",
      "email": "a@b.com",
      "mobile": "1234567890"
    }
  }
]

使用node-fetch的REST调用

function fetchResponseByURL(relativeURL) {

  return fetch(`${config.BASE_URL}${relativeURL}`, {
    method: 'GET',
    headers: {
      Accept: 'application/json',
    }
  })
  .then(response => {
    if (response.ok) {
      return response.json();
    }
  })
  .catch(error => { console.log('request failed', error); });

}

const rest = {
  fetchResponseByURL
}

export default rest

GitHub:https://github.com/vishrantgupta/graphql
JSON 端点(虚拟):https://api.myjson.com/bins/8lwqk

编辑:添加node.js标签,可能是promise对象的问题。

【问题讨论】:

  • 来自 REST 端点的响应形状可能与您的架构不匹配。您能否更新您的问题以包含来自 REST 端点的完整响应?
  • 感谢@DanielRearden 检查了这一点。我已经用端点响应更新了我的问题。
  • rest 中的 resolve: (root, args) => rest.fetchResponseByURL(merchant/${args.id}/) 是什么?
  • 这是从其他 js 文件 export default rest 导出的,这有一个 REST fetch fetchResponseByURL 方法
  • @xadm 我已经用这些细节更新了我的问题

标签: node.js express graphql express-graphql


【解决方案1】:

您的 fetchResponseByURL 函数获取空字符串。

我认为主要问题是您使用错误的函数来获取您的 JSON 字符串,请尝试安装 request-promise 并使用它来获取您的 JSON 字符串。

https://github.com/request/request-promise#readme

类似

var rp = require('request-promise');
function fetchResponseByURL(relativeURL) {
  return rp('https://api.myjson.com/bins/8lwqk')
    .then((html) => {
      const data = JSON.parse(html)
      return data.merchant
    })
    .catch((err) => console.error(err));
  // .catch(error => { console.log('request failed', error); });

}

【讨论】:

  • 我实现了request-promise,但结果还是一样。如果我从 REST 端点得到一个空的 JSON 响应,我会进行交叉验证,但事实并非如此。我已经分享了这个流程的执行drive.google.com/open?id=1aZkw66S7JUO4wYxnDL-rUiWwm5Dnho8l
  • 我不知道你有什么要求。您是否使用链接“api.myjson.com/bins/8lwqk”将所有数据存储在 JSON 中?你想通过id获取具体的商家信息吗?
  • 这将是一个链接,就像我在上面发布的方式和回复Content-Type application/json
  • 我已经更新了代码,尝试在我的回答中使用 fetchResponseByURL 函数
  • graphql 的解析器,它会自动匹配对象字段。因此,您应该在解析器结束时返回商家对象。然后它可以过滤你想要的字段。
【解决方案2】:

在这种情况下,使用data.merchant 解决了我的问题。但上述建议的解决方案,即使用 JSON.parse(...) 可能不是最佳做法,因为如果 JSON 中没有对象,那么预期的响应可能如下:

{
  "data": {
    "merchant": null
  }
}

字段改为null

{
  "data": {
    "merchant": {
      "id": null // even though merchant is null in JSON, 
                 // I am getting a merchant object in response from GraphQL
    }
  }
}

我已经用工作代码更新了我的 GitHub:https://github.com/vishrantgupta/graphql

【讨论】:

    猜你喜欢
    • 2017-01-11
    • 2020-01-25
    • 2014-08-08
    • 2021-06-05
    • 2016-06-05
    • 2017-11-25
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    相关资源
    最近更新 更多