Matters API 实验二:GraphQL Clients

北小魚
·
(修改过)
·
IPFS

继续我的Matters API 实验。终于搞清楚了实验一里的query 错误,学会了如何查询“fragment”和“edges.node”。

  search(
    input: { type: Article, key : "电影", first: 2 }
  ) {
    totalCount
    edges{
      node {
        id
        ... on Article {
          title
          author { displayName, id, uuid }
          mediaHash
          createdAt
       }
     }
    }
  }
  viewer {
    recommendation {
      hottest (
        input: { first: 2 }
      ) {
        totalCount
        edges{
          node{
            title
            mediaHash
            dataHash
            createdAt
            appreciationsReceivedTotal
            commentCount
            transactionsReceivedBy(input:{purpose:donation}){
              totalCount
            }
            subscribers(input:{}){
              totalCount
            }
          }
        }
        pageInfo {
          startCursor
          endCursor
          hasNextPage
          hasPreviousPage
        }
      }
    }
  }
}

下一步是写自己的GraphQL client,而不是用https://server.matters.news/playground。有很多方法,@bigN《GraphQL - Matters API為例 (use python GQL)》里介绍了用Python的方法,试试几个其他的方法。

Curl:

可以直接从command line 或者任何支持curl 的 language 运行。上面这个query 用 curl 长的是这个样子:

curl 'https://server.matters.news/graphql' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: https://server.matters.news' --data-binary '{"query":"# Write your query or mutation here\nquery {  \n  search(\n    input: { type: Article, key : \"??\", first: 2 }\n  ) {\n    totalCount\n    edges{\n      node {\n        id\n        ... on Article {\n          title\n          author { displayName, id, uuid }\n          mediaHash\n        \tcreatedAt\n       }\n     }\n    }\n  }\n  viewer {\n    recommendation {\n      hottest (\n        input: { first: 2 }\n      ) {\n        totalCount\n        edges{\n        \tnode{\n            title\n            mediaHash\n            dataHash\n            createdAt\n            appreciationsReceivedTotal\n            commentCount\n            transactionsReceivedBy(input:{purpose:donation}){\n              totalCount\n            }\n            subscribers(input:{}){\n              totalCount\n            }\n          }\n        }\n        pageInfo {\n          endCursor\n          hasNextPage\n        }\n      }\n    }\n  }\n}"}' --compressed

JavaScript:

最powerful,也是最常用。Apollo, AWS, Relay... 都是用 JavaScript。 自己写的话,可以用 Node.js,或者Client-side JavaScript。

试一个简单的query:

query {
  official { features { enabled } }
}

用Client-side JavaScript 长的是这个样子:

<html><body>
<div id="result"></div>
<script>
// https://server.matters.news/graphql/details?method=post&query=query{official{features{enabled}}}
var data = "query=query{official {features {enabled}}}"
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://server.matters.news/graphql", true);
xhr.setRequestHeader('Content-Type', 'application/json', 'Content-Length': data.length);
xhr.send(data);
xhr.onload = function() {
  document.getElementById('result').innerHTML = xhr.response.json()
}
</script></body></html>

下一步就是自动生成报告,然后自动发送。

CC BY-NC-ND 2.0 授权

喜欢我的作品吗?别忘了给予支持与赞赏,让我知道在创作的路上有你陪伴,一起延续这份热忱!