Hi, interested in using the GraphQL API to set off...
# ask-community
h
Hi, interested in using the GraphQL API to set off a flow run with parameters. If the following is the query in the post request to start off a flow run, how would I add in any parameters I’d like to specify? Thanks!
Copy code
query = """mutation {
    create_flow_run(input: { 
    flow_id: "5ac80adc-442a-4e53-bc01-d9d65bffd6aa"}) {
        id
    }
    }"""
n
Hi @Hugo Kitano - the
create_flow_run
mutation also takes parameters like this:
Copy code
query = """
mutation {
  create_flow_run(input: { 
    flow_id: "5ac80adc-442a-4e53-bc01-d9d65bffd6aa"
    parameters: "{}" # JSON-type parameters
  })
    {
      id
    }
}
"""
h
perfect, i’ll try that out. thanks!
👍 1
running into an error here
{"errors":[{"message":"Syntax Error: Expected Name, found String \"biosample_id\"","extensions":{"code":"GRAPHQL_PARSE_FAILED"}}]}
. The syntax is not straightforward to me. Here’s my query:
Copy code
query = """
        mutation {
            create_flow_run(input: { 
                flow_id: "9d8d0696-fafe-4cda-b02e-6afa1b00086a"
                parameters: {
                    "biosample_id": "testing_7_13_2021",
                }
            })
                {
                    id
                }
        }
    """
k
You might need to escape those parameters. Check out this other example query:
Copy code
query = """
mutation {
  set_flow_group_schedule (input: {
      flow_group_id: "795512b7-c9f7-4231-8d6b-c80dbd87231d", 
      cron_clocks: [{cron: "0 9 * * *", parameter_defaults: "{\\"x\\": 1}"}]}) {
    success
    error
  }
}
"""
Try something like. Will try also on my end.
Copy code
parameters: "{\\"biosample_id\\": \\"testing_7_13_2021\\"}"
n
You can always use something like the json python module to transform a python dict to a json string:
Copy code
import json

json.loads(parameters_dict)
upvote 1
h
how would i format the insertion of that output into my query @nicholas?
@Kevin Kho Looks like that escaping works on my end, would love to bypass it using nicholas’ method if possible
👍 1
n
You can probably do something like this (note the double curly braces since that's an f-string):
Copy code
import json

parameters = dict(biosample_id="testing_7_13_2021")

query = f"""
        mutation {{
            create_flow_run(input: {{ 
                flow_id: "9d8d0696-fafe-4cda-b02e-6afa1b00086a"
                parameters: {json.loads(parameters)}
            }})
                {{
                    id
                }}
        }}
    """
h
it’s annoying but I don’t think multi-line f-strings work that way
let me see though
Ah they do! Learn something new every day. But I think maybe you mean
json.dumps
rather than
json.loads
?
json.loads
doesn’t take in a dict as a parameter
n
Yes you're completely correct on json.dumps, mb!
h
And I’m actually running into the same issue where there is a parsing error due to the absence of backslashes
n
Try wrapping
json.dumps(json.dumps(parameters))
- I think that'll automatically escape the string for you
(not 100% sure on that)
h
wow that works great. Thanks @nicholas, it’s all working. Is there anywhere I can read up more on how this GraphQL syntax works?
k
I know you’re good but I was typing this up so I’ll just leave it here also:
Copy code
query = """
        mutation($flow_id: UUID!, $params: JSON!) {
            create_flow_run(input: { 
                flow_id: $flow_id
                parameters: $params
            })
                {
                    id
                }
        }
    """

client = Client()
client.graphql(query, variables={"flow_id": "589f4354-9595-463a-9a60-5ef36d8accea", "params": {"s": "testing"}})
upvote 1
n
I'd recommend using something like https://graphql.org/learn/queries/ - that'll teach you the basics of queries and mutations (the rest of that tutorial series isn't super relevant unless you're implementing a graphql server)
👍 1
h
If I am using the Prefect Client class I’d rather just use the below. Just trying to set up a working version of my code that doesn’t need the Prefect python package installed
Copy code
flow_id = client.create_flow_run(
    version_group_id=version_group_id, 
    run_name=name,
    parameters = params)
k
Ah gotcha you’re right!