代码之家  ›  专栏  ›  技术社区  ›  Vishrant

RESTful返回空响应的GraphQL

  •  1
  • Vishrant  · 技术社区  · 6 年前

    GraphQL 具有 REST 终点,我确认每次我打电话 http://localhost:3001/graphql 它正在撞击 休息 端点,它正在返回 JSON 回应 图形 图形 服务器到 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}/`)
        },
      }),
    });
    

    答复 端点(我还尝试使用JSON中的单个对象而不是JSON数组):

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

    使用REST调用 node-fetch

    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
    https://api.myjson.com/bins/8lwqk

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

    2 回复  |  直到 6 年前
        1
  •  2
  •   Yu Huang    6 年前

    你的 获取响应

    我认为主要的问题是您使用了错误的函数来获取JSON字符串,请尝试安装 并使用它获取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); });
    
    }
    
        2
  •  1
  •   Vishrant    6 年前

    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 使用工作代码。