mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-18 13:02:11 +03:00
11a454c2d6
This commit applies ormolu to the whole Haskell code base by running `make format`. For in-flight branches, simply merging changes from `main` will result in merge conflicts. To avoid this, update your branch using the following instructions. Replace `<format-commit>` by the hash of *this* commit. $ git checkout my-feature-branch $ git merge <format-commit>^ # and resolve conflicts normally $ make format $ git commit -a -m "reformat with ormolu" $ git merge -s ours post-ormolu https://github.com/hasura/graphql-engine-mono/pull/2404 GitOrigin-RevId: 75049f5c12f430c615eafb4c6b8e83e371e01c8e
58 lines
2.2 KiB
Haskell
58 lines
2.2 KiB
Haskell
-- | This module defines all functions that convert between different
|
|
-- representations of values in the schema; most commonly: GraphQL literals,
|
|
-- JSON values, and 'InputValue', a type that provides an abstraction above both
|
|
-- of those.
|
|
module Hasura.GraphQL.Parser.Internal.Convert where
|
|
|
|
import Data.Aeson qualified as A
|
|
import Data.HashMap.Strict.Extended qualified as M
|
|
import Data.Int (Int64)
|
|
import Data.Scientific (toBoundedInteger)
|
|
import Data.Text.Extended
|
|
import Hasura.GraphQL.Parser.Class.Parse
|
|
import Hasura.GraphQL.Parser.Internal.TypeChecking
|
|
import Hasura.GraphQL.Parser.Schema
|
|
import Hasura.Prelude
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
|
|
|
valueToJSON :: MonadParse m => G.GType -> InputValue Variable -> m A.Value
|
|
valueToJSON expectedType inputVal = do
|
|
peeledVal <- peelVariable expectedType inputVal
|
|
pure $ valueToJSON' peeledVal
|
|
where
|
|
valueToJSON' :: InputValue Variable -> A.Value
|
|
valueToJSON' = \case
|
|
JSONValue j -> j
|
|
GraphQLValue g -> graphQLToJSON g
|
|
|
|
graphQLToJSON :: G.Value Variable -> A.Value
|
|
graphQLToJSON = \case
|
|
G.VNull -> A.Null
|
|
G.VInt i -> A.toJSON i
|
|
G.VFloat f -> A.toJSON f
|
|
G.VString t -> A.toJSON t
|
|
G.VBoolean b -> A.toJSON b
|
|
G.VEnum (G.EnumValue n) -> A.toJSON n
|
|
G.VList values -> A.toJSON $ graphQLToJSON <$> values
|
|
G.VObject objects -> A.toJSON $ graphQLToJSON <$> objects
|
|
G.VVariable variable -> valueToJSON' $ absurd <$> vValue variable
|
|
|
|
jsonToGraphQL :: MonadError Text m => A.Value -> m (G.Value Void)
|
|
jsonToGraphQL = \case
|
|
A.Null -> pure G.VNull
|
|
A.Bool val -> pure $ G.VBoolean val
|
|
A.String val -> pure $ G.VString val
|
|
A.Number val -> case toBoundedInteger val of
|
|
Just intVal -> pure $ G.VInt $ fromIntegral @Int64 intVal
|
|
Nothing -> pure $ G.VFloat val
|
|
A.Array vals -> G.VList <$> traverse jsonToGraphQL (toList vals)
|
|
A.Object vals ->
|
|
G.VObject . M.fromList <$> for (M.toList vals) \(key, val) -> do
|
|
graphQLName <-
|
|
G.mkName key
|
|
`onNothing` throwError
|
|
( "variable value contains an object with key "
|
|
<> key <<> ", which is not a legal GraphQL name"
|
|
)
|
|
(graphQLName,) <$> jsonToGraphQL val
|