2021-11-04 19:08:33 +03:00
|
|
|
module Hasura.GraphQL.Parser.TestUtils
|
|
|
|
( TestMonad (..),
|
|
|
|
fakeScalar,
|
|
|
|
fakeInputFieldValue,
|
|
|
|
fakeDirective,
|
|
|
|
)
|
|
|
|
where
|
2021-05-20 13:03:02 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.HashMap.Strict qualified as M
|
|
|
|
import Data.List.NonEmpty qualified as NE
|
|
|
|
import Data.Text qualified as T
|
|
|
|
import Hasura.GraphQL.Parser.Class.Parse
|
|
|
|
import Hasura.GraphQL.Parser.Schema
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
2021-05-20 13:03:02 +03:00
|
|
|
|
|
|
|
-- test monad
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
newtype TestMonad a = TestMonad {runTest :: Either Text a}
|
2021-05-20 13:03:02 +03:00
|
|
|
deriving (Functor, Applicative, Monad)
|
|
|
|
|
|
|
|
instance MonadParse TestMonad where
|
|
|
|
withPath = const id
|
|
|
|
parseErrorWith = const $ TestMonad . Left
|
|
|
|
|
|
|
|
-- values generation
|
|
|
|
|
|
|
|
fakeScalar :: G.Name -> G.Value Variable
|
2021-09-24 01:56:37 +03:00
|
|
|
fakeScalar =
|
|
|
|
G.unName >>> \case
|
|
|
|
"Int" -> G.VInt 4242
|
|
|
|
"Boolean" -> G.VBoolean False
|
|
|
|
name -> error $ "no test value implemented for scalar " <> T.unpack name
|
2021-05-20 13:03:02 +03:00
|
|
|
|
|
|
|
fakeInputFieldValue :: InputFieldInfo -> G.Value Variable
|
|
|
|
fakeInputFieldValue = \case
|
|
|
|
IFOptional t _ -> fromT t
|
|
|
|
IFRequired nnt -> fromNNT nnt
|
|
|
|
where
|
|
|
|
fromT :: forall k. ('Input <: k) => Type k -> G.Value Variable
|
|
|
|
fromT = \case
|
|
|
|
NonNullable nnt -> fromNNT nnt
|
2021-09-24 01:56:37 +03:00
|
|
|
Nullable nnt -> fromNNT nnt
|
2021-05-20 13:03:02 +03:00
|
|
|
fromNNT :: forall k. ('Input <: k) => NonNullableType k -> G.Value Variable
|
|
|
|
fromNNT = \case
|
|
|
|
TList t -> G.VList [fromT t, fromT t]
|
|
|
|
TNamed (Definition name _ _ info) -> case info of
|
|
|
|
TIScalar -> fakeScalar name
|
|
|
|
TIEnum ei -> G.VEnum $ G.EnumValue $ dName $ NE.head ei
|
2021-09-24 01:56:37 +03:00
|
|
|
TIInputObject (InputObjectInfo oi) -> G.VObject $
|
|
|
|
M.fromList $ do
|
|
|
|
Definition fieldName _ _ fieldInfo <- oi
|
|
|
|
pure (fieldName, fakeInputFieldValue fieldInfo)
|
2021-05-20 13:03:02 +03:00
|
|
|
_ -> error "impossible"
|
|
|
|
|
|
|
|
fakeDirective :: DirectiveInfo -> G.Directive Variable
|
2021-09-24 01:56:37 +03:00
|
|
|
fakeDirective DirectiveInfo {..} =
|
|
|
|
G.Directive diName $
|
|
|
|
M.fromList $
|
|
|
|
diArguments <&> \(Definition argName _ _ argInfo) ->
|
|
|
|
(argName, fakeInputFieldValue argInfo)
|