How to get a flow_id given a project name and flow name using a GraphQL API query?

View in #prefect-community on Slack

@Kevin_Weiler: is it possible to use the python API to get a flow_id given a project name and flow name?

Kevin_Kho @Kevin_Kho: You need to use the GraphQL API. You can query like this:

query {
  flow (where: {name: {_eq: "artifact_test"},
                project: {name: {_eq: "databricks"}}}){
    name
    id
    project {
      name
    }
  }
}

Wrap it as a string and then use

from prefect.client import Client
client = Client()
client.graphql(query)

@Kevin_Weiler: thanks @Kevin_Kho

Kevin_Kho @Kevin_Kho: If you have many versions, you may get multiple flows returned. Just add archived=False in the GraphQL query

@Kevin_Weiler: @Kevin_Kho - any ideas on how to use literal string interpolation when constructing one of these graphql queries? I have this:

    query = """
        query {
            flow (
                where: {
                    name: {_eq: "test"},
                    project: {name: {_eq: "default"}},
                    archived: {_eq: false}
                }
            )
            {
                id
            }
        }
    """

but I want “test” to be substituted with a variable called flow_name

Kevin_Kho @Kevin_Kho: To be honest with you I just do:

    query = """
        query {
            flow (
                where: {
                    name: {_eq: + """ + str(flow_name) + """},
                    project: {name: {_eq: "default"}},
                    archived: {_eq: false}
                }
            )
            {
                id
            }
        }
    """

and it works for me
But if you find a better way, I’d like to know lol. I think multi line f strings are tricky

@Kevin_Weiler: I think you can just use the modulo operator
will paste when I get it (in a meeting)
yeah - here you go @Kevin_Kho

query = """
    query {
        flow (
            where: {
                name: {_eq: "%s"},
                project: {name: {_eq: "default"}},
                archived: {_eq: false}
            }
        )
        {
            id
        }
    }
"""

result = client.graphql(query%"test")

Kevin_Kho @Kevin_Kho: Oh I see, nice!