postgrest icon indicating copy to clipboard operation
postgrest copied to clipboard

Question: Specific response schema for a query

Open Manuelbaun opened this issue 3 years ago • 3 comments

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.

Manuelbaun avatar Jan 23 '22 20:01 Manuelbaun

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?

wolfgangwalther avatar Jan 24 '22 08:01 wolfgangwalther

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:

  • \queries?table=people&select=select=full_name,birth_date,address(*)
  • \people?select=full_name,birth_date,address(*)\json-doc

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.

Manuelbaun avatar Jan 26 '22 15:01 Manuelbaun

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

steve-chavez avatar Jan 26 '22 22:01 steve-chavez