mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-17 04:24:35 +03:00
2f3ae93ab0
This implements an initial set of DTO types that represent serialized metadata. These new types come with codecs using autodocodec which are used to derive both JSON serialization, and OpenAPI documentation. This ensures that we can automatically generate API documentation that is guaranteed to match JSON produced by the server. For the moment the new types are not used for anything except to generate an early version of an OpenAPI document. Because this is early work the DTO types for each metadata format version list top-level properties only with placeholders for the types of each top-level property. This early iteration demonstrates using a sum type in Haskell that maps to a tagged union in OpenAPI (using the `version` field value as a tag). This work is experimental and incomplete! Please do not incorporate the generated OpenAPI documentation into essential workflows at this time. PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4801 GitOrigin-RevId: d2f110a6237b73520cdba24667333ef14e8cdd3d
33 lines
1.2 KiB
Haskell
33 lines
1.2 KiB
Haskell
-- | Utility functions for use defining autodocodec codecs.
|
|
module Hasura.Metadata.DTO.Utils (versionField, optionalVersionField) where
|
|
|
|
import Autodocodec
|
|
( Codec (EqCodec),
|
|
ObjectCodec,
|
|
optionalFieldWith',
|
|
requiredFieldWith',
|
|
scientificCodec,
|
|
(.=),
|
|
)
|
|
import Data.Scientific (Scientific)
|
|
import Hasura.Prelude
|
|
|
|
-- | Defines a required object field named @version@ that must have the given
|
|
-- integer value. On serialization the field will have the given value
|
|
-- automatically. On deserialization parsing will fail unless the field has the
|
|
-- exact given value.
|
|
versionField :: Integer -> ObjectCodec a Scientific
|
|
versionField v = requiredFieldWith' "version" (EqCodec n scientificCodec) .= const n
|
|
where
|
|
n = fromInteger v
|
|
|
|
-- | Defines an optional object field named @version@ that must have the given
|
|
-- integer value if the field is present. On serialization the field will have
|
|
-- the given value automatically. On deserialization parsing will fail unless
|
|
-- the field has the exact given value, or is absent.
|
|
optionalVersionField :: Integer -> ObjectCodec a (Maybe Scientific)
|
|
optionalVersionField v =
|
|
optionalFieldWith' "version" (EqCodec n scientificCodec) .= const (Just n)
|
|
where
|
|
n = fromInteger v
|