https://prefect.io logo
Title
k

Kevin Weiler

04/21/2022, 3:00 PM
is it possible to use the python API to get a flow_id given a project name and flow name?
:discourse: 1
k

Kevin Kho

04/21/2022, 3:05 PM
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)
🙏 1
k

Kevin Weiler

04/21/2022, 3:07 PM
thanks @Kevin Kho
k

Kevin Kho

04/21/2022, 3:10 PM
If you have many versions, you may get multiple flows returned. Just add archived=False in the GraphQL query
k

Kevin Weiler

04/21/2022, 3:23 PM
@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
k

Kevin Kho

04/21/2022, 3:27 PM
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
k

Kevin Weiler

04/21/2022, 3:30 PM
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")
k

Kevin Kho

04/21/2022, 3:49 PM
Oh I see, nice!