https://prefect.io logo
f

Frederick Thomas

02/14/2022, 3:46 PM
Greetings Everyone, I'm attempting to use
prefect.Client
to query the GraphQL endpoint, however, I am getting errors that have me stumped. The relevant code:
Copy code
import prefect
import pandas as pd

client = prefect.Client()

client.graphql("""
    query {
        agent{
          flow_runs(limit:5, where:{state:{_eq: "Success"}start_time:{_gt:"2022-02-14"}}){
            start_time
            end_time
            state
            flow{
              name
              id
              tasks_aggregate{
                aggregate{
                  count
                }
              }
            }
      		
          }
          
        }

    } """)
The errors:
Copy code
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/prefect/client/client.py", line 465, in _request
    json_resp = response.json()
  File "/usr/local/lib/python3.8/site-packages/requests/models.py", line 898, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/local/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/workspaces/prefect/ETL/CDNY/GraphQL.py", line 7, in <module>
    client.graphql("""
  File "/usr/local/lib/python3.8/site-packages/prefect/client/client.py", line 298, in graphql
    result = <http://self.post|self.post>(
  File "/usr/local/lib/python3.8/site-packages/prefect/client/client.py", line 213, in post
    response = self._request(
  File "/usr/local/lib/python3.8/site-packages/prefect/client/client.py", line 468, in _request
    raise ClientError(
prefect.utilities.exceptions.ClientError: Malformed response received from Cloud - please ensure that you have an API token properly configured.
We're using Prefect core as our backend at the moment and the documentation for this is sparse when I google. Thanks
k

Kevin Kho

02/14/2022, 4:01 PM
Hey Fredrick, could you move the code and traceback to the thread when you get the chance to keep the main channel cleaner? This normally arises when you are not authenticated. How did you authenticate?
f

Frederick Thomas

02/14/2022, 4:04 PM
Hey @Kevin Kho sorry about that. I didn't authenticate, just used an old tab... What do you need me to do about moving the text?
k

Kevin Kho

02/14/2022, 4:05 PM
No worries! Here to the thread is fine. You need login locally with
prefect auth login --key API_KEY
to use the Client like this
f

Frederick Thomas

02/14/2022, 4:06 PM
Oh G_d, do I have to generate a key ???
k

Kevin Kho

02/14/2022, 4:06 PM
Yeah so that you can access the data attached to your account
Ah well, are you on Server or Cloud?
f

Frederick Thomas

02/14/2022, 4:07 PM
Server
We're moving to the Hybrid model soon
k

Kevin Kho

02/14/2022, 4:07 PM
You shouldn’t need an API_KEY. You need to just point to the Server with
prefect backend server
and then define your endpoint in the
config.toml
? or with the env var
PREFECT___SERVER___ENDPOINT
f

Frederick Thomas

02/14/2022, 4:09 PM
We use config.toml but I'm developing from a devcontainer, will that affect this as well?
k

Kevin Kho

02/14/2022, 4:13 PM
Yes if the endpoint is
localhost
, no if the endpoint is remote
some_ip_address:4200
and your container has internet connection?
Well have you been able to register flows? Cuz that hits the same API anyway so if that works, you can do
client.graphql()
as well
f

Frederick Thomas

02/14/2022, 4:30 PM
I ran :
prefect backend server
and modified my config.toml for the server section like so:
#### Server Setup
[server]
host = "<http://172.18.1.5>"
endpoint = "<http://172.18.1.5:4200>"
backend = "server"
and the error I'm receiving now:
requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='172.18.1.5', port=4200): Max retries exceeded with url: /graphql (Caused by ConnectTimeoutError
k

Kevin Kho

02/14/2022, 4:33 PM
You are still inside a container? It looks like it just can’t connect. Is it the same if you do something like starting an agent (
prefect agent local start
)?
f

Frederick Thomas

02/14/2022, 4:33 PM
I'll try it...
k

Kevin Kho

02/14/2022, 4:34 PM
It also might be the server setup though? How did you start your server and have you connected before successfully? Do you see the UI at http://172.18.1.5:8080 ?
f

Frederick Thomas

02/14/2022, 4:42 PM
Yes!! I am now running two(2) agents. How do I get the query results back from the call to
client
?
k

Kevin Kho

02/14/2022, 4:48 PM
It should return a dictionary? you can print it to see the shape
f

Frederick Thomas

02/14/2022, 4:53 PM
Thanks for you help, @Kevin Kho, I will attempt to not bother you for awhile 😇
k

Kevin Kho

02/14/2022, 4:54 PM
Of course!
s

Sagi Timinsky

02/20/2022, 4:50 PM
Hi @Frederick Thomas were you able to solve this issue? I have the same issue
k

Kevin Kho

02/20/2022, 4:55 PM
Are you on Server or Cloud?
f

Frederick Thomas

02/21/2022, 12:23 PM
Hi @Sagi Timinsky!! I was able to remedy the situation and I am using Server
4 Views