How to query for the latest flow run triggered with specific parameter values using a GraphQL API and a Python client?

View in #prefect-community on Slack

@Vaikath_Job: GraphQL API python client syntax question:

I’m trying to query a flow run by filtering on the value of the parameters. I cannot figure out how to pass a json blob object to the query using python. When I use json.dumps method python produces a string and as a result I get a bad request error. Not sure how to create a jsonb type parameters object and I was hoping someone might be able to point me in the right direction.

Kevin_Kho @Kevin_Kho: I made this page today. I think it will show you

Prefect Community: Passing parameters as JSON to GraphQL API

Ah wait no this is a bit different. Can you show me your current code?
This is a good example of that

GitHub: Enable search by parameters in the UI · Issue #4609 · PrefectHQ/prefect

@Vaikath_Job: It looks like I had some syntax errors in my client.graphql call. Once I fixed that a regular python json string fixed the issue. Here’s the sample code for posterity.

client = prefect.Client()
query = """
        #Get latest flow run by flow name, project name combination, and parameters
        #params is a JSON object and the query does a contains check
        query($flow_name: String!, $project_name: String!, $params: jsonb!){
            flow_run(
                where: {
                    flow: {
                        name: {_eq: $flow_name}
                        project: {
                            name: {_eq: $project_name}
                        }
                    }
                    state: {_eq: "Success"},
                    parameters: {_contains: $params}
                } 
                order_by : {end_time: desc} 
                limit : 1
            ){
                id
                version
                end_time
                name
                state
                task_runs {
                    id
                    state
                    task {
                        id
                        name
                    }
                }
                parameters
            }
        }
"""
variables = {"flow_name": flow_name, "project_name": project_name, "params": params}
flow_run_query = client.graphql(query=query, variables=variables)

Similarly, if you try to do the same from the interactive query browser in the Prefect UI, here is an example of how you can do that:

query ($param_value: jsonb) {
  flow_run(
    where: {_and: [{flow_id: {_eq: "eb45ed0a-a76c-445b-b0b8-1fdc05c1f54c"}}, 
      {parameters: {_eq: $param_value}}]}
  ) {
    name
    state
    end_time
    start_time
    parameters
  }
}

passing query variables:

 {
  "param_value": {
    "x": 1
  }
}