2023-01-12 23:55:45 +03:00
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
|
|
|
2022-07-12 17:00:15 +03:00
|
|
|
module Hasura.GraphQL.Schema.NamingCase
|
|
|
|
( NamingCase (..),
|
2022-07-19 09:55:42 +03:00
|
|
|
isGraphqlCase,
|
2022-07-12 17:00:15 +03:00
|
|
|
parseNamingConventionFromText,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2023-01-12 23:55:45 +03:00
|
|
|
import Autodocodec qualified as AC
|
2022-07-12 17:00:15 +03:00
|
|
|
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)
|
|
|
|
|
2023-01-12 23:55:45 +03:00
|
|
|
instance AC.HasCodec NamingCase where
|
|
|
|
codec =
|
|
|
|
AC.named "NamingCase" $
|
|
|
|
AC.stringConstCodec [(HasuraCase, "hasura-default"), (GraphqlCase, "graphql-default")]
|
|
|
|
|
2022-07-12 17:00:15 +03:00
|
|
|
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\""
|
2022-07-19 09:55:42 +03:00
|
|
|
|
|
|
|
isGraphqlCase :: NamingCase -> Bool
|
|
|
|
isGraphqlCase GraphqlCase = True
|
|
|
|
isGraphqlCase _ = False
|