graphql-engine/server/src-lib/Hasura/SQL
Jesse Hallett 84fd5910b0 server: polymorphic codec for metadata sources
This PR expands the OpenAPI specification generated for metadata to include separate definitions for `SourceMetadata` for each native database type, and for DataConnector.

For the most part the changes add `HasCodec` implementations, and don't modify existing code otherwise.

The generated OpenAPI spec can be used to generate TypeScript definitions that distinguish different source metadata types based on the value of the `kind` properly. There is a problem: because the specified `kind` value for a data connector source is any string, when TypeScript gets a source with a `kind` value of, say, `"postgres"`, it cannot unambiguously determine whether the source is postgres, or a data connector. For example,

```ts
function consumeSourceMetadata(source: SourceMetadata) {
    if (source.kind === "postgres" || source.kind === "pg") {
        // At this point TypeScript infers that `source` is either an instance
        // of `PostgresSourceMetadata`, or `DataconnectorSourceMetadata`. It
        // can't narrow further.
        source
    }
    if (source.kind === "something else") {
        // TypeScript infers that this `source` must be an instance of
        // `DataconnectorSourceMetadata` because `source.kind` does not match
        // any of the other options.
        source
    }
}
```

The simplest way I can think of to fix this would be to add a boolean property to the `SourceMetadata` type along the lines of `isNative` or `isDataConnector`. This could be a field that only exists in serialized data, like the metadata version field. The combination of one of the native database names for `kind`, and a true value for `isNative` would be enough for TypeScript to unambiguously distinguish the source kinds.

But note that in the current state TypeScript is able to reference the short `"pg"` name correctly!

~~Tests are not passing yet due to some discrepancies in DTO serialization vs existing Metadata serialization. I'm working on that.~~

The placeholders that I used for table and function metadata are not compatible with the ordered JSON serialization in use. I think the best solution is to write compatible codecs for those types in another PR. For now I have disabled some DTO tests for this PR.

Here are the generated [OpenAPI spec](https://github.com/hasura/graphql-engine-mono/files/9397333/openapi.tar.gz) based on these changes, and the generated [TypeScript client code](https://github.com/hasura/graphql-engine-mono/files/9397339/client-typescript.tar.gz) based on that spec.

Ticket: [MM-66](https://hasurahq.atlassian.net/browse/MM-66)

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5582
GitOrigin-RevId: e1446191c6c832879db04f129daa397a3be03f62
2022-08-25 18:36:02 +00:00
..
AnyBackend.hs server: add PostgresKind for CockroachDB 2022-08-19 14:21:09 +00:00
Backend.hs server: polymorphic codec for metadata sources 2022-08-25 18:36:02 +00:00
BackendMap.hs Introduce GDC Add/Remove Agent and List Source Kinds Metadata API Actions 2022-08-17 22:14:40 +00:00
GeoJSON.hs server: GHC 9.2 changes compatible with 8.10 (#3550) 2022-06-25 22:09:05 +00:00
Tag.hs server: add PostgresKind for CockroachDB 2022-08-19 14:21:09 +00:00
Time.hs Rewrite OpenAPI 2022-06-30 12:57:09 +00:00
Types.hs server: polymorphic codec for metadata sources 2022-08-25 18:36:02 +00:00
Value.hs server: add explicit export lists in OSS server and enforce with warning 2021-11-04 16:09:38 +00:00
WKT.hs server, pro: actually reformat the code-base using ormolu 2021-09-23 22:57:37 +00:00