2021-09-22 13:43:05 +03:00
|
|
|
module Hasura.GraphQL.Schema.RemoteSource
|
2021-09-24 01:56:37 +03:00
|
|
|
( remoteSourceField,
|
|
|
|
)
|
|
|
|
where
|
2021-09-22 13:43:05 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.GraphQL.Parser
|
|
|
|
import Hasura.GraphQL.Schema.Backend
|
|
|
|
import Hasura.GraphQL.Schema.Common
|
|
|
|
import Hasura.GraphQL.Schema.Instances ()
|
|
|
|
import Hasura.GraphQL.Schema.Select
|
|
|
|
import Hasura.GraphQL.Schema.Table
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.IR.Select qualified as IR
|
2021-12-01 07:53:34 +03:00
|
|
|
import Hasura.RQL.Types.Common (RelType (..), relNameToTxt)
|
|
|
|
import Hasura.RQL.Types.Relationships.FromSource
|
2021-10-29 17:42:07 +03:00
|
|
|
import Hasura.RQL.Types.SourceCustomization (mkCustomizedTypename)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.SQL.AnyBackend
|
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
2021-09-22 13:43:05 +03:00
|
|
|
|
|
|
|
-- | TODO(jkachmar): Documentation.
|
2021-09-24 01:56:37 +03:00
|
|
|
remoteSourceField ::
|
|
|
|
forall b r m n.
|
|
|
|
MonadBuildSchema b r m n =>
|
2021-12-01 07:53:34 +03:00
|
|
|
AnyBackend (RemoteSourceFieldInfo b) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m [FieldParser n (AnnotatedField b)]
|
2021-09-22 13:43:05 +03:00
|
|
|
remoteSourceField remoteDB = dispatchAnyBackend @BackendSchema remoteDB buildField
|
|
|
|
where
|
2021-09-24 01:56:37 +03:00
|
|
|
buildField ::
|
|
|
|
forall src tgt.
|
|
|
|
BackendSchema tgt =>
|
2021-12-01 07:53:34 +03:00
|
|
|
RemoteSourceFieldInfo src tgt ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m [FieldParser n (AnnotatedField src)]
|
2021-12-01 07:53:34 +03:00
|
|
|
buildField (RemoteSourceFieldInfo {..}) =
|
|
|
|
withTypenameCustomization (mkCustomizedTypename $ Just _rsfiSourceCustomization) do
|
|
|
|
tableInfo <- askTableInfo @tgt _rsfiSource _rsfiTable
|
|
|
|
fieldName <- textToName $ relNameToTxt _rsfiName
|
2021-10-29 17:42:07 +03:00
|
|
|
maybePerms <- tableSelectPermissions @tgt tableInfo
|
|
|
|
case maybePerms of
|
|
|
|
Nothing -> pure []
|
|
|
|
Just tablePerms -> do
|
2021-12-01 07:53:34 +03:00
|
|
|
parsers <- case _rsfiType of
|
2021-10-29 17:42:07 +03:00
|
|
|
ObjRel -> do
|
2021-12-01 07:53:34 +03:00
|
|
|
selectionSetParser <- tableSelectionSet _rsfiSource tableInfo tablePerms
|
2021-09-24 01:56:37 +03:00
|
|
|
pure $
|
2021-10-29 17:42:07 +03:00
|
|
|
pure $
|
|
|
|
subselection_ fieldName Nothing selectionSetParser <&> \fields ->
|
|
|
|
IR.SourceRelationshipObject $
|
2021-12-01 07:53:34 +03:00
|
|
|
IR.AnnObjectSelectG fields _rsfiTable $
|
2021-10-29 17:42:07 +03:00
|
|
|
IR._tpFilter $
|
|
|
|
tablePermissionsInfo tablePerms
|
|
|
|
ArrRel -> do
|
|
|
|
let aggFieldName = fieldName <> $$(G.litName "_aggregate")
|
2021-12-01 07:53:34 +03:00
|
|
|
selectionSetParser <- selectTable _rsfiSource tableInfo fieldName Nothing tablePerms
|
|
|
|
aggSelectionSetParser <- selectTableAggregate _rsfiSource tableInfo aggFieldName Nothing tablePerms
|
2021-10-29 17:42:07 +03:00
|
|
|
pure $
|
|
|
|
catMaybes
|
|
|
|
[ Just $ selectionSetParser <&> IR.SourceRelationshipArray,
|
|
|
|
aggSelectionSetParser <&> fmap IR.SourceRelationshipArrayAggregate
|
|
|
|
]
|
|
|
|
pure $
|
|
|
|
parsers <&> fmap \select ->
|
|
|
|
IR.AFRemote $
|
|
|
|
IR.RemoteSelectSource $
|
|
|
|
mkAnyBackend @tgt $
|
2021-12-01 07:53:34 +03:00
|
|
|
IR.RemoteSourceSelect _rsfiSource _rsfiSourceConfig select _rsfiMapping
|