mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-17 20:41:49 +03:00
d6a354b71d
Small PR with a few easy codecs Ticket: https://hasurahq.atlassian.net/browse/GDC-585 PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7325 GitOrigin-RevId: 2495ad1a17e86625ac037a681beebb99f377eb03
42 lines
1.4 KiB
Haskell
42 lines
1.4 KiB
Haskell
{-# LANGUAGE OverloadedLists #-}
|
|
|
|
module Hasura.GraphQL.Schema.NamingCase
|
|
( NamingCase (..),
|
|
isGraphqlCase,
|
|
parseNamingConventionFromText,
|
|
)
|
|
where
|
|
|
|
import Autodocodec qualified as AC
|
|
import Data.Aeson qualified as Aeson
|
|
import Hasura.Prelude
|
|
|
|
-- | Represents the different possible type cases for fields and types, i.e.
|
|
-- @HasuraCase@ and @GraphqlCase@ (@CamelCase@ fields and @PascalCase@ types).
|
|
data NamingCase = HasuraCase | GraphqlCase
|
|
deriving (Eq, Show, Generic)
|
|
|
|
instance AC.HasCodec NamingCase where
|
|
codec =
|
|
AC.named "NamingCase" $
|
|
AC.stringConstCodec [(HasuraCase, "hasura-default"), (GraphqlCase, "graphql-default")]
|
|
|
|
instance Aeson.ToJSON NamingCase where
|
|
toJSON HasuraCase = Aeson.String "hasura-default"
|
|
toJSON GraphqlCase = Aeson.String "graphql-default"
|
|
|
|
instance Aeson.FromJSON NamingCase where
|
|
parseJSON = Aeson.withText "NamingCase" $ \s -> case parseNamingConventionFromText s of
|
|
(Right nc) -> pure nc
|
|
(Left err) -> fail err
|
|
|
|
-- Used for both the environment variable and JSON.
|
|
parseNamingConventionFromText :: Text -> Either String NamingCase
|
|
parseNamingConventionFromText "hasura-default" = Right HasuraCase
|
|
parseNamingConventionFromText "graphql-default" = Right GraphqlCase
|
|
parseNamingConventionFromText _ = Left "naming_convention can either be \"hasura-default\" or \"graphql-default\""
|
|
|
|
isGraphqlCase :: NamingCase -> Bool
|
|
isGraphqlCase GraphqlCase = True
|
|
isGraphqlCase _ = False
|