Hello all, does an easy way in the graphql api to ...
# ask-community
m
Hello all, does an easy way in the graphql api to cancel all submitted, but not currently running flows exist? If so, where in the docs can I read more about this?
k
Hi @Matthew Blau! I don’t think there is an easy way. You need to fetch them by querying for flow_run_ids with state = Scheduled, then cancel them. Let me try to write the query.
Copy code
import requests
import json

query1 = """query {
  flow_run (where: {state: {_eq: "Scheduled"}}){
    	id
    	state
    }
  }
"""

def cancel_mutation(flow_run_id):
    query2 = """
        mutation {
        cancel_flow_run(input: { 
        flow_run_id: \"""" + str(flow_run_id) + """\", 
        }) {
            state
            }
        }
    """
    return query2

url = '<https://api.prefect.io>'
r = <http://requests.post|requests.post>(url, json={'query': query1}, headers={"authorization": "Bearer API_TOKEN"})

print(r.status_code)

data = r.json()['data']
for flow in data['flow_run']:
    flow_id = flow['id']
    _r = <http://requests.post|requests.post>(url, json={'query': cancel_mutation(flow_id)}, headers={"authorization": "Bearer API_TOKEN"})
use you API token in the header
Just making sure you know you can do this with
Copy code
import prefect
client = prefect.Client()
client.graphql(query)
and this will authenticate.