如何将多个游标传递给具有多个节点 ID 的单个 GitHub GraphQL 查询?

How can I pass multiple cursors to a single GitHub GraphQL query with multiple node ids?

我正在尝试对我的 GraphQL 查询进行批处理,以使用相同的请求从多个存储库中检索我需要的数据,其中每个存储库都有不同的游标。

假设我有 3 个节点的 ID,只有 2 个游标具有以下映射:

NODE_ID_1 <-> CURSOR_X
NODE_ID_2 <-> Empty
Node_ID_3 <-> CURSOR_Y

如何填充 GraphQL 查询以便我可以查询所有 3 个节点,但传递它们各自的游标?

query {
  nodes(ids: ["NODE_ID_1", "NODE_ID_2", "NODE_ID_3"]) {
    ... on Repository {
      databaseId
      stargazers(first: 100, after:???) {
        pageInfo {
          endCursor
        }
        edges {
          starredAt
        }
      }
    }
  }
}

您可以通过遍历节点并将它们映射到像 nodeX 这样的字段名称来使用 aliases :

{
  node1: node(id: "MDEwOlJlcG9zaXRvcnkxMDQ4MzQ4") {
    ... on Repository {
      ...Repo
    }
  }
  node2: node(id: "MDEwOlJlcG9zaXRvcnkyMzE5NDk4") {
    ... on Repository {
      ...Repo
    }
  }
  node3: node(id: "MDEwOlJlcG9zaXRvcnkyMzI1Mjk4") {
    ... on Repository {
      ...Repo
    }
  }
}

fragment Repo on Repository {
  name
}

输出:

{
  "data": {
    "node1": {
      "name": "DROP5.0"
    },
    "node2": {
      "name": "subsurface"
    },
    "node3": {
      "name": "linux"
    }
  }
}

对于光标值,您只需将字段名称映射到节点 ID 和光标值:

{
  node1: node(id: "MDEwOlJlcG9zaXRvcnkyMzE5NDk4") {
    ... on Repository {
      name
      stargazers(first: 1, after:"Y3Vyc29yOnYyOpIAzR1p") {
        pageInfo {
          endCursor
        }
        edges {
          starredAt
        }
      }
    }
  }
  node2: node(id: "MDEwOlJlcG9zaXRvcnkxMDQ4MzQ4") {
    ... on Repository {
      name
      stargazers(first: 1, after: null) {
        pageInfo {
          endCursor
        }
        edges {
          starredAt
        }
      }
    }
  }
  node3: node(id: "MDEwOlJlcG9zaXRvcnkyMzI1Mjk4") {
    ... on Repository {
      name
      stargazers(first: 1, after:"Y3Vyc29yOnYyOpIAzQzi") {
        pageInfo {
          endCursor
        }
        edges {
          starredAt
        }
      }
    }
  }
}

在这种情况下,您的输入地图将如下所示:

input = {
    "node1": {
        "id": "MDEwOlJlcG9zaXRvcnkyMzE5NDk4",
        "cursor": "Y3Vyc29yOnYyOpIAzR1p"
    },
    "node2": {
        "id": "MDEwOlJlcG9zaXRvcnkxMDQ4MzQ4",
        "cursor": null
    },
    "node3": {
        "id": "MDEwOlJlcG9zaXRvcnkyMzI1Mjk4",
        "cursor": "Y3Vyc29yOnYyOpIAzQzi"
    }
}

有变量:

query ($id1: ID!, $cursor1: String, $id2: ID!, $cursor2: String, $id3: ID!, $cursor3: String) {
  node1: node(id: $id1) {
    ... on Repository {
      name
      stargazers(first: 1, after: $cursor1) {
        pageInfo {
          endCursor
        }
        edges {
          starredAt
        }
      }
    }
  }
  node2: node(id: $id2) {
    ... on Repository {
      name
      stargazers(first: 1, after: $cursor2) {
        pageInfo {
          endCursor
        }
        edges {
          starredAt
        }
      }
    }
  }
  node3: node(id: $id3) {
    ... on Repository {
      name
      stargazers(first: 1, after: $cursor3) {
        pageInfo {
          endCursor
        }
        edges {
          starredAt
        }
      }
    }
  }
}

变量:

{
  "id1": "MDEwOlJlcG9zaXRvcnkyMzE5NDk4",
  "cursor1": "Y3Vyc29yOnYyOpIAzR1p",
  "id2": "MDEwOlJlcG9zaXRvcnkxMDQ4MzQ4",
  "cursor2": null,
  "id3": "MDEwOlJlcG9zaXRvcnkyMzI1Mjk4",
  "cursor3": "Y3Vyc29yOnYyOpIAzQzi"
}