mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-19 21:41:44 +03:00
e8e4f30dd6
Remote relationships are now supported on SQL Server and BigQuery. The major change though is the re-architecture of remote join execution logic. Prior to this PR, each backend is responsible for processing the remote relationships that are part of their AST. This is not ideal as there is nothing specific about a remote join's execution that ties it to a backend. The only backend specific part is whether or not the specification of the remote relationship is valid (i.e, we'll need to validate whether the scalars are compatible). The approach now changes to this: 1. Before delegating the AST to the backend, we traverse the AST, collect all the remote joins while modifying the AST to add necessary join fields where needed. 1. Once the remote joins are collected from the AST, the database call is made to fetch the response. The necessary data for the remote join(s) is collected from the database's response and one or more remote schema calls are constructed as necessary. 1. The remote schema calls are then executed and the data from the database and from the remote schemas is joined to produce the final response. ### Known issues 1. Ideally the traversal of the IR to collect remote joins should return an AST which does not include remote join fields. This operation can be type safe but isn't taken up as part of the PR. 1. There is a lot of code duplication between `Transport/HTTP.hs` and `Transport/Websocket.hs` which needs to be fixed ASAP. This too hasn't been taken up by this PR. 1. The type which represents the execution plan is only modified to handle our current remote joins and as such it will have to be changed to accommodate general remote joins. 1. Use of lenses would have reduced the boilerplate code to collect remote joins from the base AST. 1. The current remote join logic assumes that the join columns of a remote relationship appear with their names in the database response. This however is incorrect as they could be aliased. This can be taken up by anyone, I've left a comment in the code. ### Notes to the reviewers I think it is best reviewed commit by commit. 1. The first one is very straight forward. 1. The second one refactors the remote join execution logic but other than moving things around, it doesn't change the user facing functionality. This moves Postgres specific parts to `Backends/Postgres` module from `Execute`. Some IR related code to `Hasura.RQL.IR` module. Simplifies various type class function signatures as a backend doesn't have to handle remote joins anymore 1. The third one fixes partial case matches that for some weird reason weren't shown as warnings before this refactor 1. The fourth one generalizes the validation logic of remote relationships and implements `scalarTypeGraphQLName` function on SQL Server and BigQuery which is used by the validation logic. This enables remote relationships on BigQuery and SQL Server. https://github.com/hasura/graphql-engine-mono/pull/1497 GitOrigin-RevId: 77dd8eed326602b16e9a8496f52f46d22b795598
207 lines
7.4 KiB
Haskell
207 lines
7.4 KiB
Haskell
module Hasura.GraphQL.Execute.Remote
|
|
( buildExecStepRemote
|
|
, collectVariablesFromSelectionSet
|
|
, collectVariables
|
|
, resolveRemoteVariable
|
|
, resolveRemoteField
|
|
, runVariableCache
|
|
) where
|
|
|
|
import Hasura.Prelude
|
|
|
|
import qualified Data.Aeson as J
|
|
import qualified Data.HashMap.Strict as Map
|
|
import qualified Data.HashSet as Set
|
|
import qualified Data.Text as T
|
|
import qualified Language.GraphQL.Draft.Syntax as G
|
|
|
|
import Data.Text.Extended
|
|
|
|
import qualified Hasura.GraphQL.Transport.HTTP.Protocol as GH
|
|
|
|
import Hasura.Base.Error
|
|
import Hasura.GraphQL.Execute.Backend
|
|
import Hasura.GraphQL.Parser
|
|
import Hasura.GraphQL.Transport.HTTP.Protocol
|
|
import Hasura.RQL.Types
|
|
import Hasura.Session
|
|
|
|
|
|
mkVariableDefinitionAndValue :: Variable -> (G.VariableDefinition, (G.Name, J.Value))
|
|
mkVariableDefinitionAndValue var@(Variable varInfo gType varValue) =
|
|
(varDefn, (varName, varJSONValue))
|
|
where
|
|
varName = getName var
|
|
|
|
varDefn = G.VariableDefinition varName gType defaultVal
|
|
|
|
defaultVal =
|
|
case varInfo of
|
|
VIRequired _ -> Nothing
|
|
VIOptional _ val -> Just val
|
|
|
|
varJSONValue =
|
|
case varValue of
|
|
JSONValue v -> v
|
|
GraphQLValue val -> graphQLValueToJSON val
|
|
|
|
unresolveVariables
|
|
:: forall fragments
|
|
. Functor fragments
|
|
=> G.SelectionSet fragments Variable
|
|
-> G.SelectionSet fragments G.Name
|
|
unresolveVariables =
|
|
fmap (fmap (getName . vInfo))
|
|
|
|
collectVariables
|
|
:: forall fragments var
|
|
. (Foldable fragments, Hashable var, Eq var)
|
|
=> G.SelectionSet fragments var
|
|
-> Set.HashSet var
|
|
collectVariables =
|
|
Set.unions . fmap (foldMap Set.singleton)
|
|
|
|
collectVariablesFromSelectionSet
|
|
:: G.SelectionSet G.NoFragments Variable
|
|
-> [(G.VariableDefinition, (G.Name, J.Value))]
|
|
collectVariablesFromSelectionSet =
|
|
map mkVariableDefinitionAndValue . Set.toList . collectVariables
|
|
|
|
|
|
buildExecStepRemote
|
|
:: RemoteSchemaInfo
|
|
-> G.OperationType
|
|
-> G.SelectionSet G.NoFragments Variable
|
|
-> ExecutionStep
|
|
buildExecStepRemote remoteSchemaInfo tp selSet =
|
|
let unresolvedSelSet = unresolveVariables selSet
|
|
allVars = map mkVariableDefinitionAndValue $ Set.toList $ collectVariables selSet
|
|
varValues = Map.fromList $ map snd allVars
|
|
varValsM = bool (Just varValues) Nothing $ Map.null varValues
|
|
varDefs = map fst allVars
|
|
_grQuery = G.TypedOperationDefinition tp Nothing varDefs [] unresolvedSelSet
|
|
_grVariables = varValsM
|
|
in ExecStepRemote remoteSchemaInfo GH.GQLReq{_grOperationName = Nothing, ..}
|
|
|
|
|
|
-- | resolveRemoteVariable resolves a `RemoteSchemaVariable` into a GraphQL `Variable`. A
|
|
-- `RemoteSchemaVariable` can either be a query variable i.e. variable provided in the
|
|
-- query or it can be a `SessionPresetVariable` in which case we look up the value of the
|
|
-- session variable and coerce it into the appropriate type and then construct the GraphQL
|
|
-- `Variable`. *NOTE*: The session variable preset is a hard preset i.e. if the session
|
|
-- variable doesn't exist, an error will be thrown.
|
|
--
|
|
-- The name of the GraphQL variable generated will be a GraphQL-ized (replacing '-' by
|
|
-- '_') version of the session variable, since session variables are not valid GraphQL
|
|
-- names.
|
|
--
|
|
-- Additionally, we need to handle partially traversed JSON values; likewise, we create a
|
|
-- new variable out of thin air.
|
|
--
|
|
--
|
|
-- For example, considering the following schema for a role:
|
|
--
|
|
-- input UserName {
|
|
-- firstName : String! @preset(value:"Foo")
|
|
-- lastName : String!
|
|
-- }
|
|
--
|
|
-- type Query {
|
|
-- user(
|
|
-- user_id: Int! @preset(value:"x-hasura-user-id")
|
|
-- user_name: UserName!
|
|
-- ): User
|
|
-- }
|
|
--
|
|
-- and the incoming query to the graphql-engine is:
|
|
--
|
|
-- query($foo: UserName!) {
|
|
-- user(user_name: $foo) { id name }
|
|
-- }
|
|
--
|
|
-- with variables:
|
|
--
|
|
-- { "foo": {"lastName": "Bar"} }
|
|
--
|
|
--
|
|
-- After resolving the session argument presets, the query that will be sent to the remote
|
|
-- server will be:
|
|
--
|
|
-- query ($x_hasura_user_id: Int!, $hasura_json_var_1: String!) {
|
|
-- user (user_id: $x_hasura_user_id, user_name: {firstName: "Foo", lastName: $hasura_json_var_1}) {
|
|
-- id
|
|
-- name
|
|
-- }
|
|
-- }
|
|
--
|
|
resolveRemoteVariable
|
|
:: (MonadError QErr m)
|
|
=> UserInfo
|
|
-> RemoteSchemaVariable
|
|
-> StateT (HashMap J.Value Int) m Variable
|
|
resolveRemoteVariable userInfo = \case
|
|
SessionPresetVariable sessionVar typeName presetInfo -> do
|
|
sessionVarVal <- onNothing (getSessionVariableValue sessionVar $ _uiSession userInfo)
|
|
$ throw400 NotFound $ sessionVar <<> " session variable expected, but not found"
|
|
let varName = sessionVariableToGraphQLName sessionVar
|
|
coercedValue <-
|
|
case presetInfo of
|
|
SessionArgumentPresetScalar ->
|
|
case G.unName typeName of
|
|
"Int" ->
|
|
case readMaybe $ T.unpack sessionVarVal of
|
|
Nothing -> throw400 CoercionError $ sessionVarVal <<> " cannot be coerced into an Int value"
|
|
Just i -> pure $ G.VInt i
|
|
"Boolean" ->
|
|
if | sessionVarVal `elem` ["true", "false"] ->
|
|
pure $ G.VBoolean $ "true" == sessionVarVal
|
|
| otherwise ->
|
|
throw400 CoercionError $ sessionVarVal <<> " cannot be coerced into a Boolean value"
|
|
"Float" ->
|
|
case readMaybe $ T.unpack sessionVarVal of
|
|
Nothing ->
|
|
throw400 CoercionError $ sessionVarVal <<> " cannot be coerced into a Float value"
|
|
Just i -> pure $ G.VFloat i
|
|
-- The `String`,`ID` and the default case all use the same code. But,
|
|
-- it will be better to not merge all of them into the default case
|
|
-- because it will be helpful to know how all the built-in scalars
|
|
-- are handled
|
|
"String" -> pure $ G.VString sessionVarVal
|
|
"ID" -> pure $ G.VString sessionVarVal
|
|
-- When we encounter a custom scalar, we just pass it as a string
|
|
_ -> pure $ G.VString sessionVarVal
|
|
SessionArgumentPresetEnum enumVals -> do
|
|
sessionVarEnumVal <-
|
|
G.EnumValue <$>
|
|
onNothing
|
|
(G.mkName sessionVarVal)
|
|
(throw400 CoercionError $ sessionVarVal <<> " is not a valid GraphQL name")
|
|
case sessionVarEnumVal `Set.member` enumVals of
|
|
True -> pure $ G.VEnum sessionVarEnumVal
|
|
False -> throw400 CoercionError $ sessionVarEnumVal <<> " is not one of the valid enum values"
|
|
-- nullability is false, because we treat presets as hard presets
|
|
let variableGType = G.TypeNamed (G.Nullability False) typeName
|
|
pure $ Variable (VIRequired varName) variableGType (GraphQLValue coercedValue)
|
|
RemoteJSONValue gtype jsonValue -> do
|
|
cache <- get
|
|
index <- Map.lookup jsonValue cache `onNothing` do
|
|
let i = Map.size cache + 1
|
|
put $ Map.insert jsonValue i cache
|
|
pure i
|
|
let varName = G.unsafeMkName $ "hasura_json_var_" <> tshow index
|
|
pure $ Variable (VIRequired varName) gtype $ JSONValue jsonValue
|
|
QueryVariable variable -> pure variable
|
|
|
|
resolveRemoteField
|
|
:: (MonadError QErr m)
|
|
=> UserInfo
|
|
-> RemoteField
|
|
-> StateT (HashMap J.Value Int) m (RemoteFieldG Variable)
|
|
resolveRemoteField userInfo = traverse (resolveRemoteVariable userInfo)
|
|
|
|
runVariableCache
|
|
:: Monad m
|
|
=> StateT (HashMap J.Value Int) m a
|
|
-> m a
|
|
runVariableCache = flip evalStateT mempty
|