作者:澄墙_168 | 来源:互联网 | 2022-12-17 11:51
GraphQL很新...
我将GraphQL查询发送到由第三方控制的外部服务器.
使用此GraphQL查询:
query Photos($first: Int!, $propertyId: String) {
viewer {
content {
contentLists(first: $first, property_id: $propertyId, contentListType: IMAGE, publishState: PUBLISHED, orderBy: originalPublishDate, orderByDirection: DESC) {
edges {
node {
id
title
caption
originalPublishDate
lastModifiedDate
contents(first: $first) {
edges {
node {
id
title
caption
type
... on Image {
asset {
createdDate
lastModifiedDate
id
url
title
height
width
}
}
}
}
}
}
}
}
}
}
}
查询变量:
{
"propertyId" : "23456832-7862-5679-4342-415f32385830",
"first": 20
}
使用此代码段,我可以向外部服务器发送查询:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer "+accessToken);
HttpEntity entity = new HttpEntity(request,headers);
String respOnse= restTemplate.postForObject(EXTERNAL_URL, entity, String.class);
收到以下回复:
{
"data": {
"viewer": {
"content": {
"contentLists": {
"edges": [
{
"node": {
"id": "3510e8f7-452s-f531-43b0-ac36ba979e5a9f4m",
"title": "Homer Simpson goes Camping",
"caption": "Homer Simpson goes Camping",
"originalPublishDate": "2018-02-18T03:31:56.352Z",
"lastModifiedDate": "2018-02-18T03:32:13.530Z",
"contents": {
"edges": [
{
"node": {
"id": "3506d951-1332-86fg-42bc-b11183db50394f40",
"title": "No Title",
"caption": "",
"type": "IMAGE",
"asset": {
"createdDate": "2018-02-18T03:31:38.406Z",
"lastModifiedDate": "2018-02-18T03:31:38.991Z",
"id": "65037262-7169-7065-7861-7035676a6f65323467617866",
"url": "",
"title": "No Title",
"height": null,
"width": null
}
}
}
]
}
}
}
]
}
}
使用这些Java库:
com.graphql-java
graphql-java
8.0
com.graphql-java
graphql-java-tools
5.0.0
如何编组对POJO的POJO和/或ArrayList的响应?
是否有更好的库可以帮助我使用Java解析/编组GraphQL?
顺便说一句,第三方服务器不提供GraphQL架构文件......