graphql-engine/server/src-lib/Hasura/GraphQL/Schema/NamingCase.hs
Tom Harding 4885a3fd9a Import J, not Aeson, A, JSON, Yaml...
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8948
GitOrigin-RevId: d70c4a50b94ffe7d42a1fb1017051d351f236acc
2023-04-26 17:30:24 +00:00

65 lines
2.8 KiB
Haskell

{-# LANGUAGE OverloadedLists #-}
module Hasura.GraphQL.Schema.NamingCase
( NamingCase (..),
isGraphqlCase,
parseNamingConventionFromText,
hasNamingConventionChanged,
)
where
import Autodocodec qualified as AC
import Data.Aeson qualified as J
import Data.HashSet qualified as Set
import Hasura.Prelude
import Hasura.Server.Types (ExperimentalFeature (..))
-- | 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 J.ToJSON NamingCase where
toJSON HasuraCase = J.String "hasura-default"
toJSON GraphqlCase = J.String "graphql-default"
instance J.FromJSON NamingCase where
parseJSON = J.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
-- | Check if naming convention has changed
-- The value of naming convention depends on whether the naming convention is enabled
-- in experimental features and what the default naming convention
-- (`HASURA_GRAPHQL_DEFAULT_NAMING_CONVENTION`) is hence use both these values to
-- decide if naming convention has changed
hasNamingConventionChanged :: (Set.HashSet ExperimentalFeature, NamingCase) -> (Set.HashSet ExperimentalFeature, NamingCase) -> Bool
hasNamingConventionChanged (prevExperimentalFeatures, prevDefaultNamingCase) (currExperimentalFeatures, currDefaultNamingCase) =
case ((EFNamingConventions `elem` prevExperimentalFeatures, prevDefaultNamingCase), (EFNamingConventions `elem` currExperimentalFeatures, currDefaultNamingCase)) of
-- naming convention has been enabled, and the default naming convention is not
-- HasuraCase then the naming convention has changed
((False, _), (True, GraphqlCase)) -> True
-- naming is enabled but the default naming convention changes, then the naming
-- convention has changed
((True, GraphqlCase), (True, HasuraCase)) -> True
((True, HasuraCase), (True, GraphqlCase)) -> True
-- graphql-case naming convention has been disabled, then the naming convention
-- changes
((True, GraphqlCase), (False, _)) -> True
_ -> False