【问题标题】:Graphql query with $http.post does not work使用 $http.post 的 Graphql 查询不起作用
【发布时间】:2018-05-04 03:36:04
【问题描述】:

对下面代码的请求一切正常,但我没有返回任何数据(只是在 chrome 请求的“预览”选项卡中说“未找到错误”)。我在GraphiQL 中尝试了相同的查询,它返回了相关数据。我不确定我在这里缺少什么。请指教,有点坚持这个。

PlayService.prototype.getPlays = function(playID) {
 //The query. 
 const playsQuery = `query ($playid: String) {
    plays(id: $playid) {
      id
      items {
        nodes {
          id
          name
        }
      }
    }
  }`;

  // variables to pass.
  const variables = {
    playid: playID
  };

  //The request.
  const result = $http.post(
    /graphql,
    {
      method: 'POST',
      credentials: 'same-origin',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ playsQuery, variables })
    }
  );
  return result && result.data;
};

【问题讨论】:

    标签: javascript angularjs node.js http graphql


    【解决方案1】:

    您似乎将数据正文发送为:

    {
      playsQuery: "<query string>",
      variables: {}
    }
    

    GraphQL 规范规定您必须遵循这种格式来通过 REST 进行 GraphQL 查询:

    http://graphql.org/learn/serving-over-http/

    {
      "query": "...",
      "operationName": "...",
      "variables": { "myVariable": "someValue", ... }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-25
      • 1970-01-01
      • 2014-05-26
      • 1970-01-01
      • 2021-11-18
      • 2020-12-26
      • 2019-09-22
      • 2023-04-05
      • 2021-06-25
      相关资源
      最近更新 更多