mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-17 04:24:35 +03:00
9bd5826020
* allow customizing GraphQL root field names, close #981 * document v2 track_table API in reference * support customising column field names in GraphQL schema * [docs] add custom column fields doc in API reference * add tests * rename 'ColField' to 'ColumnField' * embed column's graphql field in 'PGColumnInfo' -> Value constructor of 'PGCol' is not exposed -> Using 'parseJSON' to construct 'PGCol' in 'FromJSON' instances * avoid using 'Maybe TableConfig' * refactors & 'custom_column_fields' -> 'custom_column_names' * cli-test: add configuration field in metadata export test * update expected keys in `FromJSON` instance of `TableMeta` * use `buildSchemaCacheFor` to update configuration in v2 track_table * remove 'GraphQLName' type and use 'isValidName' exposed from parser lib * point graphql-parser-hs library git repo to hasura * support 'set_table_custom_fields' query API & added docs and tests
73 lines
1.9 KiB
Haskell
73 lines
1.9 KiB
Haskell
module Hasura.GraphQL.Utils
|
|
( showName
|
|
, showNamedTy
|
|
, throwVE
|
|
, getBaseTy
|
|
, mapFromL
|
|
, groupTuples
|
|
, groupListWith
|
|
, mkMapWith
|
|
, showNames
|
|
) where
|
|
|
|
import Hasura.Prelude
|
|
import Hasura.RQL.Types.Error
|
|
|
|
import qualified Data.HashMap.Strict as Map
|
|
import qualified Data.List.NonEmpty as NE
|
|
import qualified Data.Text as T
|
|
import qualified Language.GraphQL.Draft.Syntax as G
|
|
|
|
showName :: G.Name -> Text
|
|
showName name = "\"" <> G.unName name <> "\""
|
|
|
|
throwVE :: (MonadError QErr m) => Text -> m a
|
|
throwVE = throw400 ValidationFailed
|
|
|
|
showNamedTy :: G.NamedType -> Text
|
|
showNamedTy nt =
|
|
"'" <> G.showNT nt <> "'"
|
|
|
|
getBaseTy :: G.GType -> G.NamedType
|
|
getBaseTy = \case
|
|
G.TypeNamed _ n -> n
|
|
G.TypeList _ lt -> getBaseTyL lt
|
|
where
|
|
getBaseTyL = getBaseTy . G.unListType
|
|
|
|
mapFromL :: (Eq k, Hashable k) => (a -> k) -> [a] -> Map.HashMap k a
|
|
mapFromL f l =
|
|
Map.fromList [(f v, v) | v <- l]
|
|
|
|
groupListWith
|
|
:: (Eq k, Hashable k, Foldable t, Functor t)
|
|
=> (v -> k) -> t v -> Map.HashMap k (NE.NonEmpty v)
|
|
groupListWith f l =
|
|
groupTuples $ fmap (\v -> (f v, v)) l
|
|
|
|
groupTuples
|
|
:: (Eq k, Hashable k, Foldable t)
|
|
=> t (k, v) -> Map.HashMap k (NE.NonEmpty v)
|
|
groupTuples =
|
|
foldr groupFlds Map.empty
|
|
where
|
|
groupFlds (k, v) m = case Map.lookup k m of
|
|
Nothing -> Map.insert k (v NE.:| []) m
|
|
Just s -> Map.insert k (v NE.<| s) m
|
|
|
|
-- either duplicate keys or the map
|
|
mkMapWith
|
|
:: (Eq k, Hashable k, Foldable t, Functor t)
|
|
=> (v -> k) -> t v -> Either (NE.NonEmpty k) (Map.HashMap k v)
|
|
mkMapWith f l =
|
|
case NE.nonEmpty dups of
|
|
Just dupsNE -> Left dupsNE
|
|
Nothing -> Right $ Map.map NE.head mapG
|
|
where
|
|
mapG = groupListWith f l
|
|
dups = Map.keys $ Map.filter ((> 1) . length) mapG
|
|
|
|
showNames :: (Foldable t) => t G.Name -> Text
|
|
showNames names =
|
|
T.intercalate ", " $ map G.unName $ toList names
|