Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remote relationships v2 #6060

Open
wants to merge 292 commits into
base: master
Choose a base branch
from
Open

Remote relationships v2 #6060

wants to merge 292 commits into from

Conversation

lirishatayal
Copy link

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

chrisdone and others added 30 commits June 11, 2019 16:50
…emote_rel_input_types

Update schema cache with remote rel input types
…phase

Remote relationship generation phase
Running http server on 127.0.0.1:5000
================================================= test session starts =================================================
platform linux -- Python 3.5.3, pytest-4.6.2, py-1.8.0, pluggy-0.12.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/chris/Work/hasura/graphql-engine/server/tests-py, inifile: pytest.ini
plugins: forked-1.0.2, xdist-1.28.0
[gw0] linux Python 3.5.3 cwd: /home/chris/Work/hasura/graphql-engine/server/tests-py
[gw0] Python 3.5.3 (default, Sep 27 2018, 17:25:39)  -- [GCC 6.3.0 20170516]
gw0 [6]
scheduling tests via LoadFileScheduling

test_remotes.py::TestTopLevelMixedFields::test_create_valid[http]
[gw0] [ 16%] PASSED test_remotes.py::TestTopLevelMixedFields::test_create_valid[http]
test_remotes.py::TestTopLevelMixedFields::test_create_valid[websocket]
[gw0] [ 33%] PASSED test_remotes.py::TestTopLevelMixedFields::test_create_valid[websocket]
test_remotes.py::TestTopLevelMixedFields::test_generation[http]
[gw0] [ 50%] PASSED test_remotes.py::TestTopLevelMixedFields::test_generation[http]
test_remotes.py::TestTopLevelMixedFields::test_generation[websocket]
[gw0] [ 66%] PASSED test_remotes.py::TestTopLevelMixedFields::test_generation[websocket]
test_remotes.py::TestTopLevelMixedFields::test_create_invalid[http]
[gw0] [ 83%] PASSED test_remotes.py::TestTopLevelMixedFields::test_create_invalid[http]
test_remotes.py::TestTopLevelMixedFields::test_create_invalid[websocket]
[gw0] [100%] PASSED test_remotes.py::TestTopLevelMixedFields::test_create_invalid[websocket]

================================================== warnings summary ===================================================
/usr/local/lib/python3.5/dist-packages/sqlalchemy/util/langhelpers.py:225
  /usr/local/lib/python3.5/dist-packages/sqlalchemy/util/langhelpers.py:225: SADeprecationWarning: The 'postgres' dialect name has been renamed to 'postgresql'
    loader = self.auto_fn(name)

-- Docs: https://docs.pytest.org/en/latest/warnings.html
======================================== 6 passed, 1 warnings in 9.24 seconds =========================================
Rishichandra Wawhal and others added 23 commits October 14, 2019 15:10
Limitations:-

-> Mixing of top level fields with remote schema and postgres queries
   is not allowed (currently enforced in master)
-> Remote joins are not possible via websockets.

Resolve Conflicts:
	.circleci/server-builder.dockerfile
	cli/commands/migrate_test.go
	scripts/dev.sh
	server/Makefile
	server/graphql-engine.cabal
	server/src-lib/Data/Aeson/Ordered.hs
	server/src-lib/Hasura/Db.hs
	server/src-lib/Hasura/EncJSON.hs
	server/src-lib/Hasura/GraphQL/Execute.hs
	server/src-lib/Hasura/GraphQL/Explain.hs
	server/src-lib/Hasura/GraphQL/RemoteServer.hs
	server/src-lib/Hasura/GraphQL/Resolve/BoolExp.hs
	server/src-lib/Hasura/GraphQL/Resolve/Context.hs
	server/src-lib/Hasura/GraphQL/Resolve/Select.hs
	server/src-lib/Hasura/GraphQL/Resolve/Types.hs
	server/src-lib/Hasura/GraphQL/Schema.hs
	server/src-lib/Hasura/GraphQL/Schema/BoolExp.hs
	server/src-lib/Hasura/GraphQL/Schema/Common.hs
	server/src-lib/Hasura/GraphQL/Schema/OrderBy.hs
	server/src-lib/Hasura/GraphQL/Schema/Select.hs
	server/src-lib/Hasura/GraphQL/Transport/HTTP.hs
	server/src-lib/Hasura/GraphQL/Transport/WebSocket.hs
	server/src-lib/Hasura/GraphQL/Utils.hs
	server/src-lib/Hasura/GraphQL/Validate/Types.hs
	server/src-lib/Hasura/Prelude.hs
	server/src-lib/Hasura/RQL/DDL/Metadata.hs
	server/src-lib/Hasura/RQL/DDL/Relationship.hs
	server/src-lib/Hasura/RQL/DDL/Relationship/Types.hs
	server/src-lib/Hasura/RQL/DDL/RemoteSchema.hs
	server/src-lib/Hasura/RQL/DDL/Schema/Cache.hs
	server/src-lib/Hasura/RQL/DDL/Schema/Table.hs
	server/src-lib/Hasura/RQL/DML/Select.hs
	server/src-lib/Hasura/RQL/DML/Select/Internal.hs
	server/src-lib/Hasura/RQL/GBoolExp.hs
	server/src-lib/Hasura/RQL/Types.hs
	server/src-lib/Hasura/RQL/Types/Catalog.hs
	server/src-lib/Hasura/RQL/Types/Metadata.hs
	server/src-lib/Hasura/RQL/Types/RemoteSchema.hs
	server/src-lib/Hasura/RQL/Types/SchemaCache.hs
	server/src-lib/Hasura/RQL/Types/SchemaCacheTypes.hs
	server/src-lib/Hasura/SQL/Types.hs
	server/src-lib/Hasura/Server/Migrate/Version.hs
	server/src-lib/Hasura/Server/Query.hs
	server/src-lib/Hasura/Server/Telemetry.hs
	server/src-rsr/catalog_metadata.sql
	server/src-rsr/hdb_metadata.yaml
	server/src-rsr/migrations/25_to_26.sql
	server/tests-py/queries/v1/metadata/clear_metadata.yaml
	server/tests-py/queries/v1/metadata/export_metadata.yaml
	server/tests-py/validate.py
Resolve Conflicts:
	server/src-lib/Hasura/Server/Migrate/Version.hs
…graphql-engine into remote-relationships-v2
@lirishatayal lirishatayal requested review from a team as code owners October 22, 2020 14:17
@hasura-bot
Copy link
Contributor

Beep boop! 🤖

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! 😎

@netlify
Copy link

netlify bot commented Oct 22, 2020

Deploy preview for hasura-docs ready!

Built with commit 3b78c79

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.