给定这个 GraphQL 示例,如何在 Javascript 中使用 JSON 执行类似的请求?
使用 GraphQL,示例中的查询为:
{
trip(
from: {place: "NSR:StopPlace:5533" },
to: {place:"NSR:StopPlace:5532"}
)
{
tripPatterns{duration}
}
}
根据文档,要查询的 URL https://api.entur.io/journey-planner/v2/graphql 。
以下是我在Javascript中尝试的内容:
var url = "https://api.entur.io/journey-planner/v2/graphql";
var tripquery =
{
trip:
{
__args: {
from : {place :"NSR:StopPlace:5533" },
to : {place :"NSR:StopPlace:5532" }
},
tripPatterns: {
duration : true
}
}
};
function jsonQuery(){
var qry = JSON.stringify(tripquery);
var url_qry = url + "?query=" + qry;
var xhttp = new XMLHttpRequest();
xhttp.open("POST", url_qry, true);
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.onreadystatechange = function(){
console.log("onreadystatechange");
if(xhttp.readyState === 4 && xhttp.status === 200){
console.log("json-query-OK");
console.log(xhttp.responseText);
}
else{
console.log("xhttp.status : " + xhttp.status);
console.log("xhttp.statusText : " + xhttp.statusText);
console.log("xhttp.readyState : " + xhttp.readyState);
console.log("xhttp.responseType: " + xhttp.responseType);
console.log("xhttp.responseText: " + xhttp.responseText);
console.log("xhttp.responseURL : " + xhttp.responseURL);
console.log("json-not-ok");
}
};
xhttp.send();
console.log("query sent");
}
上面的代码将在控制台中产生此输出:
query sent
api.entur.io/journey-planner/v2/graphql?query={%22trip%22:{%22__args%22:{%22from%22:{%22place%22:%22NSR:StopPlace:5533%22},%22to%22:{%22place%22:%22NSR:StopPlace:5532%22}},%22tripPatterns%22:{%22duration%22:true}}}:1 POST https://api.entur.io/journey-planner/v2/graphql?query={%22trip%22:{%22__args%22:{%22from%22:{%22place%22:%22NSR:StopPlace:5533%22},%22to%22:{%22place%22:%22NSR:StopPlace:5532%22}},%22tripPatterns%22:{%22duration%22:true}}} 400 (Bad Request)
query.js:29 onreadystatechange
query.js:35 xhttp.status : 400
query.js:36 xhttp.statusText : Bad Request
query.js:37 xhttp.readyState : 2
query.js:38 xhttp.responseType:
query.js:39 xhttp.responseText:
query.js:40 xhttp.responseURL : https://api.entur.io/journey-planner/v2/graphql?query={%22trip%22:{%22__args%22:{%22from%22:{%22place%22:%22NSR:StopPlace:5533%22},%22to%22:{%22place%22:%22NSR:StopPlace:5532%22}},%22tripPatterns%22:{%22duration%22:true}}}
query.js:41 json-not-ok
query.js:29 onreadystatechange
query.js:35 xhttp.status : 400
query.js:36 xhttp.statusText : Bad Request
query.js:37 xhttp.readyState : 3
query.js:38 xhttp.responseType:
query.js:39 xhttp.responseText: No query found in body
query.js:40 xhttp.responseURL : https://api.entur.io/journey-planner/v2/graphql?query={%22trip%22:{%22__args%22:{%22from%22:{%22place%22:%22NSR:StopPlace:5533%22},%22to%22:{%22place%22:%22NSR:StopPlace:5532%22}},%22tripPatterns%22:{%22duration%22:true}}}
query.js:41 json-not-ok
query.js:29 onreadystatechange
query.js:35 xhttp.status : 400
query.js:36 xhttp.statusText : Bad Request
query.js:37 xhttp.readyState : 4
query.js:38 xhttp.responseType:
query.js:39 xhttp.responseText: No query found in body
query.js:40 xhttp.responseURL : https://api.entur.io/journey-planner/v2/graphql?query={%22trip%22:{%22__args%22:{%22from%22:{%22place%22:%22NSR:StopPlace:5533%22},%22to%22:{%22place%22:%22NSR:StopPlace:5532%22}},%22tripPatterns%22:{%22duration%22:true}}}
query.js:41 json-not-ok
Json 对象中的__args
是我从在线示例中得到的,但我还没有真正理解它。
也许我不确定要搜索什么,但我找不到一些关于如何将这个 GraphQL 查询转换为 JSON 对象的好解释。
我遇到了同样的问题,我是这样做的:
{
c_con_tic_PTF(dz: CR, docmanId: 123) {
docmanId
dz
data
}
}
我尝试在OS X中将此请求作为curl命令发送 如何在OS X中使用CURL:
curl
-X POST
-H "Content-Type: application/json"
--data '{ "query": "{ c_con_tic_PTF(docmanId: 123, dz: CR) { docmanId, dz, data }}" }'
*my-graphicQL endpoint url*
我得到了我想要的回应。
所以你想从你的 graphQL 查询中制作这样的东西:
{ "query": "{ cz_atlascon_etic_PTF(docmanId: 123, dz: CR) { docmanId, dz, data }}" }
现在只需使用 JS 发送请求。如果它以任何方式对您有所帮助,这就是我的请求在 Java 中的样子:
HttpRequest mainRequest =
HttpRequest.newBuilder()
.uri(URI.create("my graphQL endpoint"))
.POST(HttpRequest.BodyPublishers.ofString("{ "query": "{ c_con_tic_PTF(docmanId: 123, dz: CR) { docmanId, dz, data }}" }"))
.build();
在Javascript中做到这一点的一种方法是使用fetch api。像这样的事情是我过去的做法。您可以通过复制下面的代码,然后将其粘贴到 Chrome 开发工具中并运行它来对其进行测试。
async function makeGraphQlQuery(urlToResource) {
const queryObject = {
query:
'{ trip( from: {place: "NSR:StopPlace:5533" }, to: {place:"NSR:StopPlace:5532"}) {tripPatterns{duration}} }',
};
const response = await fetch(urlToResource, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(queryObject),
});
const json = response.json();
return json;
}
async function sendAsync() {
const res = await makeGraphQlQuery('https://api.entur.io/journey-planner/v2/graphql');
console.log(res);
}
sendAsync().catch(err => console.log('Error in query', err));