伙计们,我有一个问题:我在spring graphql上有微服务,它可以正常工作,这里的请求示例:
输入图片描述
但是,如果我需要传递对象列表,则不清楚如何编写客户机。我试图使用GraphqlTemplate(实现'com.github.americanexpress:nodes:0.5.0'),但我没有找到传递列表请求的任何示例。也许使用其他库更糟糕。
有人用过类似的东西吗?
@Service
public class PersonService {
private final GraphQLTemplate graphQLTemplate = new GraphQLTemplate();
private final String url = "http://localhost:8084/graphql";
public List<Person> getPersonsByIds() {
GraphQLRequestEntity requestEntity;
try {
requestEntity = GraphQLRequestEntity.Builder()
.url(url)
.requestMethod(GraphQLTemplate.GraphQLMethod.QUERY)
.request("query($personIds: [BigInteger]) {n" +
" getPersonsByIds(personIds : $personIds ) {n" +
" firstNamen" +
" middleNamen" +
" lastNamen" +
" birthDtn" +
" }n" +
"}"
)
.variables(new Variable<>("personId", "2477142261427744786")) // just enable to pass only one id and got 1 person
.build();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
return graphQLTemplate.query(requestEntity, ResponseGetPersonsByIds.class).getResponse().getGetPersonsByIds();
}
}
我知道如何只传递1 id,但不清楚如何传递数组
假设您为Person定义了这样一个模式:
type Person{
firstName: String
// other fields...
}
然后您可以使用@SchemaMapping
返回该对象的列表:
@SchemaMapping(typeName = "Query",value = "personList")
public List<Person> getPersonsByIds() {
return personService.getPersonsByIds();
}
您可以将DataFetchingEnvironment对象传递给解析器并调用getDocument()方法来中继文档/查询https://www.youtube.com/watch?v=_sILQm0axSw