PostgREST / postgrest

REST API for any Postgres database
https://postgrest.org
MIT License
23.43k stars 1.03k forks source link

Question: Specific response schema for a query #2132

Open Manuelbaun opened 2 years ago

Manuelbaun commented 2 years ago

This is not a bug report, rather a question.

Is it possible to get a response json schema for a specific query? Assume this query: /people?select=full_name,birth_date. The expected response:

[{
    "full_name": "a name", 
    "birth_date": "a date string"
    },
   // more 
]

Imagine writing down the queries and then generate the response type as a json schema. From there, every language can generate their interfaces, classes, DTO etc. The above-mentioned example is rather simple. But if you would use embedded resources etc., this would be actually very nice feature. Since you already have implemented the query parser with all its intricacies.

I think GraphQl does something similar, where a generator generated the response type by a defined query.

wolfgangwalther commented 2 years ago

No, this is currently not possible, except for the OpenAPI output on the root endpoint. But this will describe the whole schema, not a single endpoint. And it will also not take the currently requested embedding into account.

How would you expect to query the server to get this result?

Manuelbaun commented 2 years ago

Hey, sorry for the late reply.

I though of an query endpoint, for sending a query, which analyse it and resolves the query return type by sending the structure, based on the schema loaded from postgres.

an post request to an endpoint /queries with a structure like

{
    "query": "\people?select=full_name,birth_date,address(*)
}

Alternatively:

would some of them work?

Unfortunately, I am not familiar with haskell at all. Its probably worth to ask the community, if such a feature is actually of interesst or just not relevant.

steve-chavez commented 2 years ago

Looks related to https://github.com/PostgREST/postgrest/issues/1421