```Good morning, I would like to know if there is ...
# prefect-ui
l
Copy code
Good morning, I would like to know if there is a way to return a json directly through the graphql api in prefect1.0-ui?
k
Not sure I understand the question. Are you talking about the interactive API tab?
But I need to retrieve the result by graphql from prefect.
k
So you have a Flow in a task that returns JSON. And using the API, you want to get it? You can just use the
get_task_run_result.run()
and it will be easier than constructing the GraphQL API query yourself?
l
@Kevin Kho Exactly, I will still need the return in graphQL. I'm using this flow for testing:
Copy code
from prefect import Flow, task
from prefect.engine.results import PrefectResult

@task
def abc(result=PrefectResult()):
    dict = {'Abc':'teste0','Aeiou':'teste2'}
    return dict

with Flow("sub") as flow:
    abc()

print(flow.serialize()) # this is how i get the slug
flow.register("databricks")
I need to retrieve the dict value in graphQL.
k
You can try something like this:
Copy code
query {
  task_run{
    name
    id
    state_result
  }
}
But
state_result
only gives a path. For
PrefectResult
, the path==value. But for other result types, you need to get that path and load it in yourself, which is why the
get_task_run_result.run()
is easier to use
l
@Kevin Kho in my case returned null this "property" state_result.
Copy code
{
  "data": {
    "task_run": [
      {
        "name": null,
        "id": "0ec743fc-9ddb-436b-b061-39cbb77d272a",
        "state_result": null
      },
      {
        "name": null,
        "id": "ddc5a3b8-7b42-4260-9262-f63a4b75354e",
        "state_result": null
      },
      {
        "name": null,
        "id": "f8a70303-7ac8-4a14-9e0f-c98166e560d7",
        "state_result": null
      },
      {
        "name": null,
        "id": "9b1f1c15-3246-4da6-9afc-5e9693134708",
        "state_result": null
      }
    ]
  }
}
k
How about like this?
Copy code
query {
  task_run_state{
    id
    task_run{
      name
      id
    }
    result
    }
  }
l
@Kevin Kho Returned null too.
Copy code
{
  "data": {
    "task_run_state": [
      {
        "id": "4c981969-e94e-4e08-b60f-b76e402fdfe0",
        "task_run": {
          "name": null,
          "id": "0ec743fc-9ddb-436b-b061-39cbb77d272a",
          "flow_run_id": "9d5ba1c3-63b1-415a-8971-355e0a3f1b0b"
        },
        "result": null
      },
      {
        "id": "3669c566-e3d2-4224-aa4c-f585aa84bb14",
        "task_run": {
          "name": null,
          "id": "0ec743fc-9ddb-436b-b061-39cbb77d272a",
          "flow_run_id": "9d5ba1c3-63b1-415a-8971-355e0a3f1b0b"
        },
        "result": null
      },
      {
        "id": "7129ebcf-56b1-4944-89bb-00e25b02e6f0",
        "task_run": {
          "name": null,
          "id": "0ec743fc-9ddb-436b-b061-39cbb77d272a",
          "flow_run_id": "9d5ba1c3-63b1-415a-8971-355e0a3f1b0b"
        },
        "result": null
      },
      {
        "id": "ccca18fa-09d5-4f42-827f-ed31f645162f",
        "task_run": {
          "name": null,
          "id": "ddc5a3b8-7b42-4260-9262-f63a4b75354e",
          "flow_run_id": "6193dd50-49b1-4952-bc0c-ecf560175347"
        },
        "result": null
      },
      {
        "id": "94b3e566-f304-41ec-8576-414f2710320b",
        "task_run": {
          "name": null,
          "id": "ddc5a3b8-7b42-4260-9262-f63a4b75354e",
          "flow_run_id": "6193dd50-49b1-4952-bc0c-ecf560175347"
        },
        "result": null
      },
      {
        "id": "335a77d3-f504-4f0e-8942-a767dacef681",
        "task_run": {
          "name": null,
          "id": "ddc5a3b8-7b42-4260-9262-f63a4b75354e",
          "flow_run_id": "6193dd50-49b1-4952-bc0c-ecf560175347"
        },
        "result": null
      },
      {
        "id": "8e4d870c-41fa-4fad-a4cf-322444362347",
        "task_run": {
          "name": null,
          "id": "f8a70303-7ac8-4a14-9e0f-c98166e560d7",
          "flow_run_id": "09ba29cc-a54e-47e1-93e6-440fed95a309"
        },
        "result": null
      },
      {
        "id": "410201ce-d442-45a2-89c7-fd4501855b36",
        "task_run": {
          "name": null,
          "id": "f8a70303-7ac8-4a14-9e0f-c98166e560d7",
          "flow_run_id": "09ba29cc-a54e-47e1-93e6-440fed95a309"
        },
        "result": null
      },
      {
        "id": "7634985d-f532-4fb5-aae4-6011df2c743e",
        "task_run": {
          "name": null,
          "id": "f8a70303-7ac8-4a14-9e0f-c98166e560d7",
          "flow_run_id": "09ba29cc-a54e-47e1-93e6-440fed95a309"
        },
        "result": null
      },
      {
        "id": "a26d5ead-4c72-4630-8200-79024724bb37",
        "task_run": {
          "name": null,
          "id": "9b1f1c15-3246-4da6-9afc-5e9693134708",
          "flow_run_id": "063c04fa-28fe-426b-a41d-de5e32bbe3b1"
        },
        "result": null
      },
      {
        "id": "aa7c4ba9-5d62-40d5-81d3-8212fe824453",
        "task_run": {
          "name": null,
          "id": "9b1f1c15-3246-4da6-9afc-5e9693134708",
          "flow_run_id": "063c04fa-28fe-426b-a41d-de5e32bbe3b1"
        },
        "result": null
      },
      {
        "id": "a7fe6a8f-f9c8-4bc8-9f53-1f2284e21f7f",
        "task_run": {
          "name": null,
          "id": "9b1f1c15-3246-4da6-9afc-5e9693134708",
          "flow_run_id": "063c04fa-28fe-426b-a41d-de5e32bbe3b1"
        },
        "result": null
      },
      {
        "id": "d18957c2-82e9-4858-9800-12ed825adcec",
        "task_run": {
          "name": null,
          "id": "d9211ad2-df6b-40f6-9114-b22629c54301",
          "flow_run_id": "b258a7d2-5ae7-4870-a905-a498f215e30e"
        },
        "result": null
      },
      {
        "id": "3bac5b0c-86f1-4cdd-a155-12900a21e9f8",
        "task_run": {
          "name": null,
          "id": "d9211ad2-df6b-40f6-9114-b22629c54301",
          "flow_run_id": "b258a7d2-5ae7-4870-a905-a498f215e30e"
        },
        "result": null
      },
      {
        "id": "a026fa19-132b-4372-854e-54974f73ccf0",
        "task_run": {
          "name": null,
          "id": "d9211ad2-df6b-40f6-9114-b22629c54301",
          "flow_run_id": "b258a7d2-5ae7-4870-a905-a498f215e30e"
        },
        "result": null
      },
      {
        "id": "033ec829-ecfc-4543-aac6-c6d369419b7b",
        "task_run": {
          "name": null,
          "id": "2cc8b939-0e26-4824-a894-444b320acb5c",
          "flow_run_id": "bc62bb16-6c30-405e-9585-baead958cb6d"
        },
        "result": null
      },
      {
        "id": "afbba881-5c15-4885-a82c-8d5cbe21b061",
        "task_run": {
          "name": null,
          "id": "2cc8b939-0e26-4824-a894-444b320acb5c",
          "flow_run_id": "bc62bb16-6c30-405e-9585-baead958cb6d"
        },
        "result": null
      },
      {
        "id": "395383f5-4714-4738-9f0d-650cca9af44e",
        "task_run": {
          "name": null,
          "id": "2cc8b939-0e26-4824-a894-444b320acb5c",
          "flow_run_id": "bc62bb16-6c30-405e-9585-baead958cb6d"
        },
        "result": null
      }
    ]
  }
}
k
Ah ok. I would suggest really just using
get_task_run_result.run()
instead then.
Ok I really found it this time:
Copy code
query {
  task_run_state{
    id
    task_run{
      name
      id
    }
    serialized_state
    }
  }
gives me
Copy code
"task_run_state": [
      {
        "id": "da55ab92-7198-40fd-94f0-cc9e37610f33",
        "task_run": {
          "name": null,
          "id": "e6d8c0f7-4c6b-45af-b9be-ed4f94ae5e39"
        },
        "serialized_state": {
          "type": "Success",
          "_result": {
            "type": "PrefectResult",
            "location": "1",
            "__version__": "0.15.7+3.g880d18ba7"
          },
          "context": {
            "tags": []
          },
          "message": "Task run succeeded.",
          "__version__": "0.15.7+3.g880d18ba7",
          "cached_inputs": {}
        }
      },
l
@Kevin Kho I used this query:
Copy code
query {
  flow_run(where: {id: {_eq: "57d80f17-7040-4e55-91d7-2e4bd544c80d"}}) {
    id
    state
    start_time
  }
  task_run {
    serialized_state
  }
}
I got this return:
Copy code
{
  "data": {
    "flow_run": [
      {
        "id": "57d80f17-7040-4e55-91d7-2e4bd544c80d",
        "state": "Success",
        "start_time": "2022-05-26T17:10:09.926597+00:00"
      }
    ],
    "task_run": [
      {
        "serialized_state": {
          "type": "Success",
          "_result": {
            "type": "PrefectResult",
            "location": "{\"Abc\": \"teste0\", \"Aeiou\": \"teste2\"}",
            "__version__": "1.2.0+10.gafda99411"
          },
          "context": {
            "tags": []
          },
          "message": "Task run succeeded.",
          "__version__": "1.2.0+10.gafda99411",
          "cached_inputs": {}
        }
      }
    ]
  }
}
The problem in this case is that the return is not good for those who consume it. I will explain my application. 1 - I start the flow in the prefect using the API with this query and retrieve the flow execution ID:
Copy code
mutation {
  create_flow_run(input: { flow_id: "36609af8-3cb7-4550-9dff-57e0c40f749d" }) {
    id
  }
}
2 - I need to retrieve the result that had, in this case it would be this dict:
Copy code
{'Abc': 'teste0', 'Aeiou': 'teste2'}
The problem is that through location there is not a good return, I would like the return to be separated, in this case in the same way as you showed through
get_task_run_result.run()
Copy code
"location": "{\"Abc\": \"teste0\", \"Aeiou\": \"teste2\"}"
k
I guess the question is where you would eventually run these GraphQL queries from? Is it still Python? You would need to consume the result and pull out the location
l
The idea is to provide curl as in this example:
Copy code
curl '<http://localhost:4200/>' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: <http://localhost:4200>' --data-binary '{"query":"mutation {\n  create_flow_run(input: { flow_id: \"d7bfb996-b8fe-4055-8d43-2c9f82a1e3c7\" }) {\n    id\n  }\n}"}' --compressed
And any integration with that API can use.
k
Oh god yeah unfortunately, I don’t think we can get it in that format through curl alone with the stuff Prefect already provides. You’d need to do extra work to expose that in some way