I am trying to use the Orchestration GraphQL API P...
# prefect-community
z
I am trying to use the Orchestration GraphQL API Python Client and it seems like there is barely any documentation on it. I used the interactive API on the prefect cloud website to build my query, but it has "where" statements, and the small snippet of code in the prefect docs about the python client don't give any details on how to use "where" statements with the python client. https://docs.prefect.io/orchestration/concepts/api.html#getting-started
k
Hello @Zach! Duly noted - the documentation doesn’t go into too much detail in terms of constructing GraphQL queries. Personally, I’ve used the Hasura documentation to help with my queries: I recommend checking out this doc on filtering that goes into detail about using the
where
clause. Hope that helps!
z
@kyle I know how to construct the graphql query to get my result, but the prefect graphql python client doesn't accept string graphql queries from what I understand, it looks like it accepts dictionaries and I don't understand how to incorporate a where clause into the dictionary
My problem isn't with graphql, its with the Prefect Python graphql client, and how it wants queries to be formatted
k
What specifically are you trying to accomplish with your query?
c
The python client does accept string graphql queries, so something else might be going on; I just ran:
Copy code
client.graphql("""
query{
project(where: {name: {_ilike: "%stick%"}}){
name
}
}""")
and it worked fine
🤞 1
z
@Chris White I hadn't actually tested using graphql strings, was just going by what the docs said where they use a dictionary.
@Kyle Moon-Wright Simple queries that work in the interactive API online don't work when passed as strings to the prefect graphql client, they are giving 500 errors. : Query:
Copy code
client.graphql("""query {
  flow_run_state(where: {
    state: {
      _eq: "Failed"
    }
  }) {
    message,
    state,
  }
}""")
error:
Copy code
prefect.utilities.exceptions.ClientError: 
[{
  'path': ['flow_run_state'], 
  'message': 'Response not successful: Received status code 500', 
  'extensions': {
    'code': 'INTERNAL_SERVER_ERROR', 
    'exception': {
      'name': 'ServerError', 
      'response': {
        'size': 0, 
        'timeout': 0
      }, 
      'statusCode': 500, 
      'result': {
        'errors': [
          {
            'extensions': {
              'path': '$', 
              'code': 'unexpected'
            }, 
            'message': 'database query error'
}]}}}}]
k
Hmm, the 500 error leads me to believe you are querying Prefect Server, but Server may not be running (just wanted to check). If you were hitting a valid server, you should at least be receiving 200 errors with an embedded message.
upvote 1
That query looks great btw, worked for me on my end. 👍
z
@Kyle Moon-Wright I want to be querying prefect cloud, how do I make sure that I am doing that?
Anytime I try to look at
flow_run_state->flow_run->task_runs
I get a 500 error. It seems to happen whenever I query the task runs, but I can't figure out why. If I take the the task runs part out it works fine.
k
Hmm, so a query through the client like this returns a 500?:
Copy code
import prefect
client = prefect.Client()

client.graphql(
    """
    query {
        flow_run_state {
            created
            flow_run {
                created,
                task_runs {
                    created
                }
            }
        }
    }
    """
)