graphql-engine icon indicating copy to clipboard operation
graphql-engine copied to clipboard

Remote relationships v2

Open lirishatayal opened this issue 5 years ago • 3 comments
trafficstars

module Hasura.GraphQL.Remote.Input ( substituteVariables , SubstituteError(..) , RemoteArguments(..) ) where

import Data.Aeson (FromJSON(..),ToJSON(..)) import qualified Data.Aeson as Aeson import qualified Data.Aeson.Types as Aeson import Data.Foldable import Data.HashMap.Strict (HashMap) import qualified Data.HashMap.Strict as HM import Data.Map.Strict (Map) import qualified Data.Map.Strict as M import Data.Scientific (floatingOrInteger) import Data.Text (Text) import qualified Data.Text as T import Data.Validation import qualified Language.GraphQL.Draft.Syntax as GraphQL import Language.Haskell.TH.Syntax (Lift) import Prelude


-- Type definitions

newtype RemoteArguments = RemoteArguments { getRemoteArguments :: [GraphQL.ObjectFieldG GraphQL.Value] } deriving (Show, Eq, Lift)

instance ToJSON RemoteArguments where toJSON = error "TODO: ToJSON: RemoteArguments"

instance FromJSON RemoteArguments where parseJSON = parseRemoteArguments

-- | An error substituting variables into the argument list. data SubstituteError = ValueNotProvided !GraphQL.Variable deriving (Show, Eq)


-- Operations

-- | Substitute values in the argument list. substituteVariables :: Map GraphQL.Variable GraphQL.ValueConst -- ^ Values to use. -> [GraphQL.ObjectFieldG GraphQL.Value] -- ^ A template. -> Validation [SubstituteError] [GraphQL.ObjectFieldG GraphQL.ValueConst] substituteVariables values = traverse (traverse go) where go = \case GraphQL.VVariable variable -> case M.lookup variable values of Nothing -> Failure [ValueNotProvided variable] Just valueConst -> pure valueConst GraphQL.VInt int32 -> pure (GraphQL.VCInt int32) GraphQL.VFloat double -> pure (GraphQL.VCFloat double) GraphQL.VString stringValue -> pure (GraphQL.VCString stringValue) GraphQL.VBoolean bool -> pure (GraphQL.VCBoolean bool) GraphQL.VNull -> pure GraphQL.VCNull GraphQL.VEnum enumValue -> pure (GraphQL.VCEnum enumValue) GraphQL.VList (GraphQL.ListValueG listValue) -> fmap (GraphQL.VCList . GraphQL.ListValueG) (traverse go listValue) GraphQL.VObject (GraphQL.ObjectValueG objectValue) -> fmap (GraphQL.VCObject . GraphQL.ObjectValueG) (traverse (traverse go) objectValue)


-- Parsing GraphQL input arguments from JSON

parseRemoteArguments :: Aeson.Value -> Aeson.Parser RemoteArguments parseRemoteArguments j = case j of Aeson.Object hashMap -> fmap RemoteArguments (parseObjectFields hashMap) _ -> fail "Remote arguments should be an object of keys."

parseObjectFields :: HashMap Text Aeson.Value -> Aeson.Parser [GraphQL.ObjectFieldG GraphQL.Value] parseObjectFields hashMap = traverse ((key, value) -> do name <- parseJSON (Aeson.String key) parsedValue <- parseValue value pure GraphQL.ObjectFieldG {_ofName = name, _ofValue = parsedValue}) (HM.toList hashMap)

parseValue :: Aeson.Value -> Aeson.Parser GraphQL.Value parseValue = \case Aeson.Object object -> fmap (GraphQL.VObject . GraphQL.ObjectValueG) (parseObjectFields object) Aeson.Array array -> fmap (GraphQL.VList . GraphQL.ListValueG . toList) (traverse parseValue array) Aeson.String text -> case T.uncons text of Just ('$', rest) | T.null rest -> fail "Invalid variable name." | otherwise -> pure (GraphQL.VVariable (GraphQL.Variable (GraphQL.Name rest))) _ -> pure (GraphQL.VString (GraphQL.StringValue text)) Aeson.Number !scientific -> pure (either GraphQL.VFloat GraphQL.VInt (floatingOrInteger scientific)) Aeson.Bool !bool -> pure (GraphQL.VBoolean bool) Aeson.Null -> pure GraphQL.VNull

lirishatayal avatar Oct 22 '20 14:10 lirishatayal

Beep boop! :robot:

Hey @lirishatayal, thanks for your PR!

One of my human friends will review this PR and get back to you as soon as possible.

Stay awesome! :sunglasses:

hasura-bot avatar Oct 22 '20 14:10 hasura-bot

Deploy preview for hasura-docs ready!

Built with commit 3b78c7926c0f018ccc7f924ee67f59ea64af2aa7

https://deploy-preview-6060--hasura-docs.netlify.app

netlify[bot] avatar Oct 22 '20 14:10 netlify[bot]

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
6 out of 7 committers have signed the CLA.

:white_check_mark: jberryman
:white_check_mark: arvi3411301
:white_check_mark: tirumaraiselvan
:white_check_mark: rakeshkky
:white_check_mark: ecthiender
:white_check_mark: praveenweb
:x: Rishichandra Wawhal


Rishichandra Wawhal seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Jan 28 '25 16:01 CLAassistant