如何将多个查询批处理为对 GitHub 的 GraphQL API 的单个请求?
例如,您如何将这 2 个查询批处理为单个请求并接收单个响应?这种技术是否适用于更多的查询(比如 200)?
{
repositoryOwner(login:"rails") {
repository(name:"rails") {
description
homepageURL
}
}
}
{
repositoryOwner(login:"github") {
repository(name:"graphql-client") {
description
homepageURL
}
}
}
(GitHub GraphQL API 可以在 https://developer.github.com/early-access/graphql/explorer/ 进行试验)
最佳答案
您需要在一个查询中包含对两个字段的调用:
{
repositoryOwner(login:"rails") {
repository(name:"rails") {
description
homepageURL
}
}
repositoryOwner(login:"github") {
repository(name:"graphql-client") {
description
homepageURL
}
}
}
但这仍然会失败,因为现在输出中有两个具有相同名称的字段(repositoryOwner),因此您需要为它们设置别名:
{
rails: repositoryOwner(login:"rails") {
repository(name:"rails") {
description
homepageURL
}
}
graphql_client: repositoryOwner(login:"github") {
repository(name:"graphql-client") {
description
homepageURL
}
}
}
见 this explanation 。
如果您可以为每个查询生成唯一的别名,那么此技术应该可以正常工作。
关于github-api - 如何批量处理 GitHub GraphQL API 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39666940/