https://prefect.io logo
j

james.lamb

07/20/2020, 3:19 PM
I've been trying to figure out how to get a
flow_group_id
from Prefect Cloud, given a project name and flow name. As far as I understand from this thread, the combination of project name, flow name, and the tenant I'm auth'd as should be enough to uniquely identify a
flow_group
. This is the first time I've ever used GraphQL so if anyone has done this or has a better recommendation, I'd welcome it! This was my solution:
Copy code
from prefect.client import Client

def get_flow_group_id(flow_name, project_name) -> str:
    """
    Get the `flow_group_id` for a flow with a given
    name, from a given Prefect Cloud project.
    """
    client = Client()
    query = """
        query {
          flow(
            where: {
                name: { _eq: "%s" }
            }
          ) {
            id
            name
            flow_group_id
            project_id
          }
          project(
              where: {
                name: { _eq: "%s" }
              }
          ) {
              id
              name
          }
        }
    """ % (flow_name, project_name)
    result = client.graphql(query)
    project_id = result["data"]["project"][0]["id"]
    flow_group_id = [
        flow for flow in
        result["data"]["flow"]
        if flow["project_id"] == project_id
    ][0]["flow_group_id"]
    return flow_group_id
n

nicholas

07/20/2020, 3:24 PM
Hi @james.lamb, some small changes:
Copy code
query {
  flow(where: {
   name: {_eq: "%s"}
   project: {  name : {_eq: "%s"}}
  }) {
    id
    flow_group_id
  }
}
Should get you the flow group id as you've described.
j

james.lamb

07/20/2020, 3:25 PM
😱 that's cool, I didn't know you could do that! It was kind of hard to figure out which entities I'd be able to reference in queries, but I imagine that's just because I'm super new to GraphQL. Thank you!!!
n

nicholas

07/20/2020, 3:25 PM
😄
j

james.lamb

07/20/2020, 3:26 PM
why did something like this not come up when I googled "GraphQL equivalent of inner join" 😂
🤔 1
n

nicholas

07/20/2020, 3:26 PM
The InteractiveAPI in the UI can help figure out syntax/playground your queries 🙂
j

james.lamb

07/20/2020, 3:27 PM
oh cool, cool
j

Jenny

07/20/2020, 3:32 PM
I find Hasura's docs pretty useful for figuring out how to write GraphQL queries etc too: https://hasura.io/docs/1.0/graphql/manual/queries/index.html
🙏 1
upvote 1
j

james.lamb

07/20/2020, 3:50 PM
oooooooooooooooooooo thanks!
3 Views