2022-03-01 19:03:23 +03:00
|
|
|
-- | Non-empty hash maps.
|
|
|
|
module Data.HashMap.Strict.NonEmpty
|
|
|
|
( -- * Type
|
|
|
|
NEHashMap,
|
|
|
|
|
|
|
|
-- * Construction and conversions
|
|
|
|
singleton,
|
|
|
|
fromHashMap,
|
|
|
|
fromList,
|
|
|
|
toHashMap,
|
|
|
|
|
|
|
|
-- * Basic interface
|
|
|
|
lookup,
|
|
|
|
(!?),
|
Enable remote joins from remote schemas in the execution engine.
### Description
This PR adds the ability to perform remote joins from remote schemas in the engine. To do so, we alter the definition of an `ExecutionStep` targeting a remote schema: the `ExecStepRemote` constructor now expects a `Maybe RemoteJoins`. This new argument is used when processing the execution step, in the transport layer (either `Transport.HTTP` or `Transport.WebSocket`).
For this `Maybe RemoteJoins` to be extracted from a parsed query, this PR also extends the `Execute.RemoteJoin.Collect` module, to implement "collection" from a selection set. Not only do those new functions extract the remote joins, but they also apply all necessary transformations to the selection sets (such as inserting the necessary "phantom" fields used as join keys).
Finally in `Execute.RemoteJoin.Join`, we make two changes. First, we now always look for nested remote joins, regardless of whether the join we just performed went to a source or a remote schema; and second we adapt our join tree logic according to the special cases that were added to deal with remote server edge cases.
Additionally, this PR refactors / cleans / documents `Execute.RemoteJoin.RemoteServer`. This is not required as part of this change and could be moved to a separate PR if needed (a similar cleanup of `Join` is done independently in #3894). It also introduces a draft of a new documentation page for this project, that will be refined in the release PR that ships the feature (either #3069 or a copy of it).
While this PR extends the engine, it doesn't plug such relationships in the schema, meaning that, as of this PR, the new code paths in `Join` are technically unreachable. Adding the corresponding schema code and, ultimately, enabling the metadata API will be done in subsequent PRs.
### Keeping track of concrete type names
The main change this PR makes to the existing `Join` code is to handle a new reserved field we sometimes use when targeting remote servers: the `__hasura_internal_typename` field. In short, a GraphQL selection set can sometimes "branch" based on the concrete "runtime type" of the object on which the selection happens:
```graphql
query {
author(id: 53478) {
... on Writer {
name
articles {
title
}
}
... on Artist {
name
articles {
title
}
}
}
}
```
If both of those `articles` are remote joins, we need to be able, when we get the answer, to differentiate between the two different cases. We do this by asking for `__typename`, to be able to decide if we're in the `Writer` or the `Artist` branch of the query.
To avoid further processing / customization of results, we only insert this `__hasura_internal_typename: __typename` field in the query in the case of unions of interfaces AND if we have the guarantee that we will processing the request as part of the remote joins "folding": that is, if there's any remote join in this branch in the tree. Otherwise, we don't insert the field, and we leave that part of the response untouched.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3810
GitOrigin-RevId: 89aaf16274d68e26ad3730b80c2d2fdc2896b96c
2022-03-09 06:17:28 +03:00
|
|
|
keys,
|
|
|
|
|
2022-03-10 05:12:53 +03:00
|
|
|
-- * Combine
|
Enable remote joins from remote schemas in the execution engine.
### Description
This PR adds the ability to perform remote joins from remote schemas in the engine. To do so, we alter the definition of an `ExecutionStep` targeting a remote schema: the `ExecStepRemote` constructor now expects a `Maybe RemoteJoins`. This new argument is used when processing the execution step, in the transport layer (either `Transport.HTTP` or `Transport.WebSocket`).
For this `Maybe RemoteJoins` to be extracted from a parsed query, this PR also extends the `Execute.RemoteJoin.Collect` module, to implement "collection" from a selection set. Not only do those new functions extract the remote joins, but they also apply all necessary transformations to the selection sets (such as inserting the necessary "phantom" fields used as join keys).
Finally in `Execute.RemoteJoin.Join`, we make two changes. First, we now always look for nested remote joins, regardless of whether the join we just performed went to a source or a remote schema; and second we adapt our join tree logic according to the special cases that were added to deal with remote server edge cases.
Additionally, this PR refactors / cleans / documents `Execute.RemoteJoin.RemoteServer`. This is not required as part of this change and could be moved to a separate PR if needed (a similar cleanup of `Join` is done independently in #3894). It also introduces a draft of a new documentation page for this project, that will be refined in the release PR that ships the feature (either #3069 or a copy of it).
While this PR extends the engine, it doesn't plug such relationships in the schema, meaning that, as of this PR, the new code paths in `Join` are technically unreachable. Adding the corresponding schema code and, ultimately, enabling the metadata API will be done in subsequent PRs.
### Keeping track of concrete type names
The main change this PR makes to the existing `Join` code is to handle a new reserved field we sometimes use when targeting remote servers: the `__hasura_internal_typename` field. In short, a GraphQL selection set can sometimes "branch" based on the concrete "runtime type" of the object on which the selection happens:
```graphql
query {
author(id: 53478) {
... on Writer {
name
articles {
title
}
}
... on Artist {
name
articles {
title
}
}
}
}
```
If both of those `articles` are remote joins, we need to be able, when we get the answer, to differentiate between the two different cases. We do this by asking for `__typename`, to be able to decide if we're in the `Writer` or the `Artist` branch of the query.
To avoid further processing / customization of results, we only insert this `__hasura_internal_typename: __typename` field in the query in the case of unions of interfaces AND if we have the guarantee that we will processing the request as part of the remote joins "folding": that is, if there's any remote join in this branch in the tree. Otherwise, we don't insert the field, and we leave that part of the response untouched.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3810
GitOrigin-RevId: 89aaf16274d68e26ad3730b80c2d2fdc2896b96c
2022-03-09 06:17:28 +03:00
|
|
|
unionWith,
|
2022-03-01 19:03:23 +03:00
|
|
|
|
|
|
|
-- * Transformations
|
|
|
|
mapKeys,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Data.HashMap.Strict (HashMap)
|
|
|
|
import Data.HashMap.Strict qualified as M
|
|
|
|
import Data.Hashable (Hashable)
|
|
|
|
import Prelude hiding (lookup)
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | A non-empty hashmap is a wrapper around a normal hashmap, that
|
|
|
|
-- only provides a restricted set of functionalities. It doesn't
|
|
|
|
-- provide a 'Monoid' instance, nor an 'empty' function.
|
|
|
|
newtype NEHashMap k v = NEHashMap {unNEHashMap :: HashMap k v}
|
|
|
|
deriving newtype (Show, Eq, Ord, Semigroup)
|
|
|
|
deriving stock (Functor, Foldable, Traversable)
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | Construct a non-empty map with a single element.
|
|
|
|
singleton :: Hashable k => k -> v -> NEHashMap k v
|
|
|
|
singleton k v = NEHashMap $ M.singleton k v
|
|
|
|
|
|
|
|
-- | Construct a non-empty map with the supplied mappings.
|
|
|
|
-- Returns 'Nothing' if the provided 'HashMap' is empty.
|
|
|
|
fromHashMap :: HashMap k v -> Maybe (NEHashMap k v)
|
|
|
|
fromHashMap m
|
|
|
|
| M.null m = Nothing
|
|
|
|
| otherwise = Just $ NEHashMap m
|
|
|
|
|
|
|
|
-- | Construct a non-empty map with the supplied mappings as follows:
|
|
|
|
--
|
|
|
|
-- * if the provided list contains duplicate mappings, the later mappings take
|
|
|
|
-- precedence;
|
|
|
|
-- * if the provided list is empty, returns 'Nothing'.
|
|
|
|
fromList :: (Eq k, Hashable k) => [(k, v)] -> Maybe (NEHashMap k v)
|
|
|
|
fromList [] = Nothing
|
|
|
|
fromList v = Just $ NEHashMap $ M.fromList v
|
|
|
|
|
|
|
|
-- | Convert a non-empty map to a 'HashMap'.
|
|
|
|
toHashMap :: NEHashMap k v -> HashMap k v
|
|
|
|
toHashMap = unNEHashMap
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | Return the value to which the specified key is mapped, or 'Nothing' if
|
|
|
|
-- this map contains no mapping for the key.
|
|
|
|
lookup :: (Eq k, Hashable k) => k -> NEHashMap k v -> Maybe v
|
|
|
|
lookup k (NEHashMap m) = M.lookup k m
|
|
|
|
|
|
|
|
-- | Return the value to which the specified key is mapped, or 'Nothing' if
|
|
|
|
-- this map contains no mapping for the key.
|
|
|
|
--
|
|
|
|
-- This is a flipped version of 'lookup'.
|
|
|
|
(!?) :: (Eq k, Hashable k) => NEHashMap k v -> k -> Maybe v
|
|
|
|
(!?) = flip lookup
|
|
|
|
|
Enable remote joins from remote schemas in the execution engine.
### Description
This PR adds the ability to perform remote joins from remote schemas in the engine. To do so, we alter the definition of an `ExecutionStep` targeting a remote schema: the `ExecStepRemote` constructor now expects a `Maybe RemoteJoins`. This new argument is used when processing the execution step, in the transport layer (either `Transport.HTTP` or `Transport.WebSocket`).
For this `Maybe RemoteJoins` to be extracted from a parsed query, this PR also extends the `Execute.RemoteJoin.Collect` module, to implement "collection" from a selection set. Not only do those new functions extract the remote joins, but they also apply all necessary transformations to the selection sets (such as inserting the necessary "phantom" fields used as join keys).
Finally in `Execute.RemoteJoin.Join`, we make two changes. First, we now always look for nested remote joins, regardless of whether the join we just performed went to a source or a remote schema; and second we adapt our join tree logic according to the special cases that were added to deal with remote server edge cases.
Additionally, this PR refactors / cleans / documents `Execute.RemoteJoin.RemoteServer`. This is not required as part of this change and could be moved to a separate PR if needed (a similar cleanup of `Join` is done independently in #3894). It also introduces a draft of a new documentation page for this project, that will be refined in the release PR that ships the feature (either #3069 or a copy of it).
While this PR extends the engine, it doesn't plug such relationships in the schema, meaning that, as of this PR, the new code paths in `Join` are technically unreachable. Adding the corresponding schema code and, ultimately, enabling the metadata API will be done in subsequent PRs.
### Keeping track of concrete type names
The main change this PR makes to the existing `Join` code is to handle a new reserved field we sometimes use when targeting remote servers: the `__hasura_internal_typename` field. In short, a GraphQL selection set can sometimes "branch" based on the concrete "runtime type" of the object on which the selection happens:
```graphql
query {
author(id: 53478) {
... on Writer {
name
articles {
title
}
}
... on Artist {
name
articles {
title
}
}
}
}
```
If both of those `articles` are remote joins, we need to be able, when we get the answer, to differentiate between the two different cases. We do this by asking for `__typename`, to be able to decide if we're in the `Writer` or the `Artist` branch of the query.
To avoid further processing / customization of results, we only insert this `__hasura_internal_typename: __typename` field in the query in the case of unions of interfaces AND if we have the guarantee that we will processing the request as part of the remote joins "folding": that is, if there's any remote join in this branch in the tree. Otherwise, we don't insert the field, and we leave that part of the response untouched.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3810
GitOrigin-RevId: 89aaf16274d68e26ad3730b80c2d2fdc2896b96c
2022-03-09 06:17:28 +03:00
|
|
|
-- | Return a list of this map's keys.
|
|
|
|
keys :: NEHashMap k v -> [k]
|
|
|
|
keys = M.keys . unNEHashMap
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | The union of two maps.
|
|
|
|
--
|
|
|
|
-- If a key occurs in both maps, the provided function (first argument) will be
|
|
|
|
-- used to compute the result.
|
|
|
|
unionWith :: (Eq k, Hashable k) => (v -> v -> v) -> NEHashMap k v -> NEHashMap k v -> NEHashMap k v
|
|
|
|
unionWith fun (NEHashMap m1) (NEHashMap m2) = NEHashMap $ M.unionWith fun m1 m2
|
|
|
|
|
2022-03-01 19:03:23 +03:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | @'mapKeys' f s@ is the map obtained by applying @f@ to each key of @s@.
|
|
|
|
--
|
|
|
|
-- The size of the result may be smaller if f maps two or more distinct keys to
|
|
|
|
-- the same new key. In this case there is no guarantee which of the associated
|
|
|
|
-- values is chosen for the conflicting key.
|
|
|
|
mapKeys :: (Eq k2, Hashable k2) => (k1 -> k2) -> NEHashMap k1 v -> NEHashMap k2 v
|
|
|
|
mapKeys fun (NEHashMap m) = NEHashMap $ M.mapKeys fun m
|