Clean Relay's code, break schema cycles, introduce Node ID V2
## Motivation
This PR rewrites most of Relay to achieve the following:
- ~~fix a bug in which the same node id could refer to two different tables in the schema~~
- remove one of the few remaining uses of the source cache in the schema building code
In doing so, it also:
- simplifies the `BackendSchema` class by removing `node` from it,
- makes it much easier for other backends to support Relay,
- documents, re-organizes, and clarifies the code.
## Description
This PR introduces a new `NodeId` version ~~, and adapts the Postgres code to always generate this V2 version~~. This new id contains the source name, in addition to the table name, in order to disambiguate similar table names across different sources (which is now possible with source customization). In doing so, it now explicitly handles that case for V1 node ids, and returns an explicit error message instead of running the risk of _silently returning the wrong information_.
Furthermore, it adapts `nodeField` to support multiple backends; most of the code was trivial to generalize, and as a result it lowers the cost of entry for other backends, that now only need to support `AFNodeId` in their translation layer.
Finally, it removes one more cycle in the schema building code, by using the same trick we used for remote relationships instead of using the memoization trick of #4576.
## Remaining work
- ~~[ ]write a Changelog entry~~
- ~~[x] adapt all tests that were asserting on an old node id~~
## Future work
This PR was adapted from its original form to avoid a breaking change: while it introduces a Node ID V2, we keep generating V1 IDs and the parser rejects V2 IDs. It will be easy to make the switch at a later data in a subsequent PR.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4593
GitOrigin-RevId: 88e5cb91e8b0646900547fa8c7c0e1463de267a1
2022-06-07 16:35:26 +03:00
|
|
|
{-# LANGUAGE TemplateHaskellQuotes #-}
|
|
|
|
|
|
|
|
-- | This module only exposes one function, 'nodeField', which is used at the
|
|
|
|
-- root level of the schema to create the 'node' field in the Relay API schema.
|
|
|
|
module Hasura.GraphQL.Schema.Relay
|
|
|
|
( nodeInterface,
|
|
|
|
nodeField,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Control.Lens hiding (index)
|
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.Aeson.Extended qualified as J
|
2022-07-06 10:54:57 +03:00
|
|
|
import Data.Aeson.Types qualified as J
|
Clean Relay's code, break schema cycles, introduce Node ID V2
## Motivation
This PR rewrites most of Relay to achieve the following:
- ~~fix a bug in which the same node id could refer to two different tables in the schema~~
- remove one of the few remaining uses of the source cache in the schema building code
In doing so, it also:
- simplifies the `BackendSchema` class by removing `node` from it,
- makes it much easier for other backends to support Relay,
- documents, re-organizes, and clarifies the code.
## Description
This PR introduces a new `NodeId` version ~~, and adapts the Postgres code to always generate this V2 version~~. This new id contains the source name, in addition to the table name, in order to disambiguate similar table names across different sources (which is now possible with source customization). In doing so, it now explicitly handles that case for V1 node ids, and returns an explicit error message instead of running the risk of _silently returning the wrong information_.
Furthermore, it adapts `nodeField` to support multiple backends; most of the code was trivial to generalize, and as a result it lowers the cost of entry for other backends, that now only need to support `AFNodeId` in their translation layer.
Finally, it removes one more cycle in the schema building code, by using the same trick we used for remote relationships instead of using the memoization trick of #4576.
## Remaining work
- ~~[ ]write a Changelog entry~~
- ~~[x] adapt all tests that were asserting on an old node id~~
## Future work
This PR was adapted from its original form to avoid a breaking change: while it introduces a Node ID V2, we keep generating V1 IDs and the parser rejects V2 IDs. It will be easy to make the switch at a later data in a subsequent PR.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4593
GitOrigin-RevId: 88e5cb91e8b0646900547fa8c7c0e1463de267a1
2022-06-07 16:35:26 +03:00
|
|
|
import Data.Align (align)
|
|
|
|
import Data.Has
|
|
|
|
import Data.HashMap.Strict.Extended qualified as Map
|
|
|
|
import Data.Sequence.NonEmpty qualified as NESeq
|
|
|
|
import Data.Text qualified as T
|
|
|
|
import Data.Text.Extended
|
|
|
|
import Data.These (partitionThese)
|
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.GraphQL.Schema.Backend
|
|
|
|
import Hasura.GraphQL.Schema.Common
|
|
|
|
import Hasura.GraphQL.Schema.Instances ()
|
|
|
|
import Hasura.GraphQL.Schema.Node
|
server: Metadata origin for definitions (type parameter version v2)
The code that builds the GraphQL schema, and `buildGQLContext` in particular, is partial: not every value of `(ServerConfigCtx, GraphQLQueryType, SourceCache, HashMap RemoteSchemaName (RemoteSchemaCtx, MetadataObject), ActionCache, AnnotatedCustomTypes)` results in a valid GraphQL schema. When it fails, we want to be able to return better error messages than we currently do.
The key thing that is missing is a way to trace back GraphQL type information to their origin from the Hasura metadata. Currently, we have a number of correctness checks of our GraphQL schema. But these correctness checks only have access to pure GraphQL type information, and hence can only report errors in terms of that. Possibly the worst is the "conflicting definitions" error, which, in practice, can only be debugged by Hasura engineers. This is terrible DX for customers.
This PR allows us to print better error messages, by adding a field to the `Definition` type that traces the GraphQL type to its origin in the metadata. So the idea is simple: just add `MetadataObjId`, or `Maybe` that, or some other sum type of that, to `Definition`.
However, we want to avoid having to import a `Hasura.RQL` module from `Hasura.GraphQL.Parser`. So we instead define this additional field of `Definition` through a new type parameter, which is threaded through in `Hasura.GraphQL.Parser`. We then define type synonyms in `Hasura.GraphQL.Schema.Parser` that fill in this type parameter, so that it is not visible for the majority of the codebase.
The idea of associating metadata information to `Definition`s really comes to fruition when combined with hasura/graphql-engine-mono#4517. Their combination would allow us to use the API of fatal errors (just like the current `MonadError QErr`) to report _inconsistencies_ in the metadata. Such inconsistencies are then _automatically_ ignored. So no ad-hoc decisions need to be made on how to cut out inconsistent metadata from the GraphQL schema. This will allow us to report much better errors, as well as improve the likelihood of a successful HGE startup.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4770
Co-authored-by: Samir Talwar <47582+SamirTalwar@users.noreply.github.com>
GitOrigin-RevId: 728402b0cae83ae8e83463a826ceeb609001acae
2022-06-28 18:52:26 +03:00
|
|
|
import Hasura.GraphQL.Schema.Parser (Kind (..), Parser, memoizeOn)
|
|
|
|
import Hasura.GraphQL.Schema.Parser qualified as P
|
Clean Relay's code, break schema cycles, introduce Node ID V2
## Motivation
This PR rewrites most of Relay to achieve the following:
- ~~fix a bug in which the same node id could refer to two different tables in the schema~~
- remove one of the few remaining uses of the source cache in the schema building code
In doing so, it also:
- simplifies the `BackendSchema` class by removing `node` from it,
- makes it much easier for other backends to support Relay,
- documents, re-organizes, and clarifies the code.
## Description
This PR introduces a new `NodeId` version ~~, and adapts the Postgres code to always generate this V2 version~~. This new id contains the source name, in addition to the table name, in order to disambiguate similar table names across different sources (which is now possible with source customization). In doing so, it now explicitly handles that case for V1 node ids, and returns an explicit error message instead of running the risk of _silently returning the wrong information_.
Furthermore, it adapts `nodeField` to support multiple backends; most of the code was trivial to generalize, and as a result it lowers the cost of entry for other backends, that now only need to support `AFNodeId` in their translation layer.
Finally, it removes one more cycle in the schema building code, by using the same trick we used for remote relationships instead of using the memoization trick of #4576.
## Remaining work
- ~~[ ]write a Changelog entry~~
- ~~[x] adapt all tests that were asserting on an old node id~~
## Future work
This PR was adapted from its original form to avoid a breaking change: while it introduces a Node ID V2, we keep generating V1 IDs and the parser rejects V2 IDs. It will be easy to make the switch at a later data in a subsequent PR.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4593
GitOrigin-RevId: 88e5cb91e8b0646900547fa8c7c0e1463de267a1
2022-06-07 16:35:26 +03:00
|
|
|
import Hasura.GraphQL.Schema.Select
|
|
|
|
import Hasura.GraphQL.Schema.Table
|
2022-07-12 17:00:15 +03:00
|
|
|
import Hasura.GraphQL.Schema.Typename (withTypenameCustomization)
|
2022-06-23 12:14:24 +03:00
|
|
|
import Hasura.Name qualified as Name
|
Clean Relay's code, break schema cycles, introduce Node ID V2
## Motivation
This PR rewrites most of Relay to achieve the following:
- ~~fix a bug in which the same node id could refer to two different tables in the schema~~
- remove one of the few remaining uses of the source cache in the schema building code
In doing so, it also:
- simplifies the `BackendSchema` class by removing `node` from it,
- makes it much easier for other backends to support Relay,
- documents, re-organizes, and clarifies the code.
## Description
This PR introduces a new `NodeId` version ~~, and adapts the Postgres code to always generate this V2 version~~. This new id contains the source name, in addition to the table name, in order to disambiguate similar table names across different sources (which is now possible with source customization). In doing so, it now explicitly handles that case for V1 node ids, and returns an explicit error message instead of running the risk of _silently returning the wrong information_.
Furthermore, it adapts `nodeField` to support multiple backends; most of the code was trivial to generalize, and as a result it lowers the cost of entry for other backends, that now only need to support `AFNodeId` in their translation layer.
Finally, it removes one more cycle in the schema building code, by using the same trick we used for remote relationships instead of using the memoization trick of #4576.
## Remaining work
- ~~[ ]write a Changelog entry~~
- ~~[x] adapt all tests that were asserting on an old node id~~
## Future work
This PR was adapted from its original form to avoid a breaking change: while it introduces a Node ID V2, we keep generating V1 IDs and the parser rejects V2 IDs. It will be easy to make the switch at a later data in a subsequent PR.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4593
GitOrigin-RevId: 88e5cb91e8b0646900547fa8c7c0e1463de267a1
2022-06-07 16:35:26 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.IR qualified as IR
|
|
|
|
import Hasura.RQL.Types.Backend
|
|
|
|
import Hasura.RQL.Types.Column
|
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.RQL.Types.SchemaCache hiding (askTableInfo)
|
|
|
|
import Hasura.RQL.Types.Source
|
|
|
|
import Hasura.RQL.Types.SourceCustomization
|
|
|
|
import Hasura.RQL.Types.Table
|
|
|
|
import Hasura.SQL.AnyBackend qualified as AB
|
|
|
|
import Hasura.SQL.Backend
|
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
|
|
|
|
|
|
|
-- | Constructs the parser for the node interface.
|
|
|
|
--
|
|
|
|
-- As mentioned in Note [Internal Relay HashMap], this function must parse an
|
|
|
|
-- incoming query for ANY potential matching table. Its resulting parser returns
|
|
|
|
-- a 'NodeMap': a container that, to a source name and a table name, associates
|
|
|
|
-- both the parsed fields and all the relevant table information required to
|
|
|
|
-- craft a request.
|
|
|
|
nodeInterface :: SourceCache -> NodeInterfaceParserBuilder
|
|
|
|
nodeInterface sourceCache = NodeInterfaceParserBuilder $ memoizeOn 'nodeInterface () do
|
|
|
|
let idDescription = G.Description "A globally unique identifier"
|
2022-06-23 12:14:24 +03:00
|
|
|
idField = P.selection_ Name._id (Just idDescription) P.identifier
|
Clean Relay's code, break schema cycles, introduce Node ID V2
## Motivation
This PR rewrites most of Relay to achieve the following:
- ~~fix a bug in which the same node id could refer to two different tables in the schema~~
- remove one of the few remaining uses of the source cache in the schema building code
In doing so, it also:
- simplifies the `BackendSchema` class by removing `node` from it,
- makes it much easier for other backends to support Relay,
- documents, re-organizes, and clarifies the code.
## Description
This PR introduces a new `NodeId` version ~~, and adapts the Postgres code to always generate this V2 version~~. This new id contains the source name, in addition to the table name, in order to disambiguate similar table names across different sources (which is now possible with source customization). In doing so, it now explicitly handles that case for V1 node ids, and returns an explicit error message instead of running the risk of _silently returning the wrong information_.
Furthermore, it adapts `nodeField` to support multiple backends; most of the code was trivial to generalize, and as a result it lowers the cost of entry for other backends, that now only need to support `AFNodeId` in their translation layer.
Finally, it removes one more cycle in the schema building code, by using the same trick we used for remote relationships instead of using the memoization trick of #4576.
## Remaining work
- ~~[ ]write a Changelog entry~~
- ~~[x] adapt all tests that were asserting on an old node id~~
## Future work
This PR was adapted from its original form to avoid a breaking change: while it introduces a Node ID V2, we keep generating V1 IDs and the parser rejects V2 IDs. It will be easy to make the switch at a later data in a subsequent PR.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4593
GitOrigin-RevId: 88e5cb91e8b0646900547fa8c7c0e1463de267a1
2022-06-07 16:35:26 +03:00
|
|
|
nodeInterfaceDescription = G.Description "An object with globally unique ID"
|
|
|
|
tCase <- asks getter
|
|
|
|
tables :: [Parser 'Output n (SourceName, AB.AnyBackend TableMap)] <-
|
|
|
|
catMaybes . concat <$> for (Map.toList sourceCache) \(sourceName, anySourceInfo) ->
|
2022-06-30 18:22:19 +03:00
|
|
|
AB.dispatchAnyBackendWithTwoConstraints @BackendSchema @BackendTableSelectSchema
|
|
|
|
anySourceInfo
|
|
|
|
\(sourceInfo :: SourceInfo b) ->
|
|
|
|
for (Map.toList $ takeValidTables $ _siTables sourceInfo) \(tableName, tableInfo) -> runMaybeT do
|
|
|
|
tablePkeyColumns <- hoistMaybe $ tableInfo ^? tiCoreInfo . tciPrimaryKey . _Just . pkColumns
|
|
|
|
selectPermissions <- MaybeT $ tableSelectPermissions tableInfo
|
|
|
|
annotatedFieldsParser <-
|
|
|
|
MaybeT $
|
2022-07-12 17:00:15 +03:00
|
|
|
withTypenameCustomization
|
2022-06-30 18:22:19 +03:00
|
|
|
(mkCustomizedTypename (_scTypeNames $ _siCustomization sourceInfo) tCase)
|
|
|
|
(tableSelectionSet sourceInfo tableInfo)
|
|
|
|
pure $
|
|
|
|
annotatedFieldsParser <&> \fields ->
|
|
|
|
( sourceName,
|
|
|
|
AB.mkAnyBackend $
|
|
|
|
TableMap $
|
|
|
|
Map.singleton tableName $
|
|
|
|
NodeInfo (_siConfiguration sourceInfo) selectPermissions tablePkeyColumns fields
|
|
|
|
)
|
Clean Relay's code, break schema cycles, introduce Node ID V2
## Motivation
This PR rewrites most of Relay to achieve the following:
- ~~fix a bug in which the same node id could refer to two different tables in the schema~~
- remove one of the few remaining uses of the source cache in the schema building code
In doing so, it also:
- simplifies the `BackendSchema` class by removing `node` from it,
- makes it much easier for other backends to support Relay,
- documents, re-organizes, and clarifies the code.
## Description
This PR introduces a new `NodeId` version ~~, and adapts the Postgres code to always generate this V2 version~~. This new id contains the source name, in addition to the table name, in order to disambiguate similar table names across different sources (which is now possible with source customization). In doing so, it now explicitly handles that case for V1 node ids, and returns an explicit error message instead of running the risk of _silently returning the wrong information_.
Furthermore, it adapts `nodeField` to support multiple backends; most of the code was trivial to generalize, and as a result it lowers the cost of entry for other backends, that now only need to support `AFNodeId` in their translation layer.
Finally, it removes one more cycle in the schema building code, by using the same trick we used for remote relationships instead of using the memoization trick of #4576.
## Remaining work
- ~~[ ]write a Changelog entry~~
- ~~[x] adapt all tests that were asserting on an old node id~~
## Future work
This PR was adapted from its original form to avoid a breaking change: while it introduces a Node ID V2, we keep generating V1 IDs and the parser rejects V2 IDs. It will be easy to make the switch at a later data in a subsequent PR.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4593
GitOrigin-RevId: 88e5cb91e8b0646900547fa8c7c0e1463de267a1
2022-06-07 16:35:26 +03:00
|
|
|
pure $
|
|
|
|
Map.fromListWith fuseAnyMaps
|
|
|
|
<$> P.selectionSetInterface
|
2022-06-23 12:14:24 +03:00
|
|
|
Name._Node
|
Clean Relay's code, break schema cycles, introduce Node ID V2
## Motivation
This PR rewrites most of Relay to achieve the following:
- ~~fix a bug in which the same node id could refer to two different tables in the schema~~
- remove one of the few remaining uses of the source cache in the schema building code
In doing so, it also:
- simplifies the `BackendSchema` class by removing `node` from it,
- makes it much easier for other backends to support Relay,
- documents, re-organizes, and clarifies the code.
## Description
This PR introduces a new `NodeId` version ~~, and adapts the Postgres code to always generate this V2 version~~. This new id contains the source name, in addition to the table name, in order to disambiguate similar table names across different sources (which is now possible with source customization). In doing so, it now explicitly handles that case for V1 node ids, and returns an explicit error message instead of running the risk of _silently returning the wrong information_.
Furthermore, it adapts `nodeField` to support multiple backends; most of the code was trivial to generalize, and as a result it lowers the cost of entry for other backends, that now only need to support `AFNodeId` in their translation layer.
Finally, it removes one more cycle in the schema building code, by using the same trick we used for remote relationships instead of using the memoization trick of #4576.
## Remaining work
- ~~[ ]write a Changelog entry~~
- ~~[x] adapt all tests that were asserting on an old node id~~
## Future work
This PR was adapted from its original form to avoid a breaking change: while it introduces a Node ID V2, we keep generating V1 IDs and the parser rejects V2 IDs. It will be easy to make the switch at a later data in a subsequent PR.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4593
GitOrigin-RevId: 88e5cb91e8b0646900547fa8c7c0e1463de267a1
2022-06-07 16:35:26 +03:00
|
|
|
(Just nodeInterfaceDescription)
|
|
|
|
[idField]
|
|
|
|
tables
|
|
|
|
where
|
|
|
|
-- this can only ever fail if somehow, within the same source, we ran into
|
|
|
|
-- two tables of a different type b; in other words, it is impossible.
|
|
|
|
fuseAnyMaps :: AB.AnyBackend TableMap -> AB.AnyBackend TableMap -> AB.AnyBackend TableMap
|
|
|
|
fuseAnyMaps m1 m2 =
|
|
|
|
AB.composeAnyBackend @Backend fuseMaps m1 m2 $
|
|
|
|
error "panic: two tables of a different backend type within the same source"
|
|
|
|
|
|
|
|
fuseMaps :: forall b. Backend b => TableMap b -> TableMap b -> AB.AnyBackend TableMap
|
|
|
|
fuseMaps (TableMap m1) (TableMap m2) = AB.mkAnyBackend @b $ TableMap $ Map.union m1 m2
|
|
|
|
|
|
|
|
-- | Creates a field parser for the top-level "node" field in the QueryRoot.
|
|
|
|
--
|
|
|
|
-- It exepcts one argument, the node id. It looks for the targeted table in the
|
|
|
|
-- 'NodeMap' returned by 'nodeInterface', and, if successful, attempts to craft
|
|
|
|
-- a corresponding 'QueryRootField' that will extract the requested row.
|
|
|
|
nodeField ::
|
|
|
|
forall m n r.
|
|
|
|
SourceCache ->
|
|
|
|
MonadBuildSchemaBase r m n =>
|
|
|
|
m (P.FieldParser n (IR.QueryRootField IR.UnpreparedValue))
|
|
|
|
nodeField sourceCache = do
|
|
|
|
let idDescription = G.Description "A globally unique id"
|
2022-06-23 12:14:24 +03:00
|
|
|
idArgument = P.field Name._id (Just idDescription) P.identifier
|
Clean Relay's code, break schema cycles, introduce Node ID V2
## Motivation
This PR rewrites most of Relay to achieve the following:
- ~~fix a bug in which the same node id could refer to two different tables in the schema~~
- remove one of the few remaining uses of the source cache in the schema building code
In doing so, it also:
- simplifies the `BackendSchema` class by removing `node` from it,
- makes it much easier for other backends to support Relay,
- documents, re-organizes, and clarifies the code.
## Description
This PR introduces a new `NodeId` version ~~, and adapts the Postgres code to always generate this V2 version~~. This new id contains the source name, in addition to the table name, in order to disambiguate similar table names across different sources (which is now possible with source customization). In doing so, it now explicitly handles that case for V1 node ids, and returns an explicit error message instead of running the risk of _silently returning the wrong information_.
Furthermore, it adapts `nodeField` to support multiple backends; most of the code was trivial to generalize, and as a result it lowers the cost of entry for other backends, that now only need to support `AFNodeId` in their translation layer.
Finally, it removes one more cycle in the schema building code, by using the same trick we used for remote relationships instead of using the memoization trick of #4576.
## Remaining work
- ~~[ ]write a Changelog entry~~
- ~~[x] adapt all tests that were asserting on an old node id~~
## Future work
This PR was adapted from its original form to avoid a breaking change: while it introduces a Node ID V2, we keep generating V1 IDs and the parser rejects V2 IDs. It will be easy to make the switch at a later data in a subsequent PR.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4593
GitOrigin-RevId: 88e5cb91e8b0646900547fa8c7c0e1463de267a1
2022-06-07 16:35:26 +03:00
|
|
|
stringifyNum <- retrieve soStringifyNum
|
|
|
|
nodeObject <-
|
|
|
|
retrieve scSchemaKind >>= \case
|
|
|
|
HasuraSchema -> throw500 "internal error: the node field should only be built for the Relay schema"
|
|
|
|
RelaySchema nodeBuilder -> runNodeBuilder nodeBuilder
|
|
|
|
pure $
|
2022-06-23 12:14:24 +03:00
|
|
|
P.subselection Name._node Nothing idArgument nodeObject `P.bindField` \(ident, parseds) -> do
|
Clean Relay's code, break schema cycles, introduce Node ID V2
## Motivation
This PR rewrites most of Relay to achieve the following:
- ~~fix a bug in which the same node id could refer to two different tables in the schema~~
- remove one of the few remaining uses of the source cache in the schema building code
In doing so, it also:
- simplifies the `BackendSchema` class by removing `node` from it,
- makes it much easier for other backends to support Relay,
- documents, re-organizes, and clarifies the code.
## Description
This PR introduces a new `NodeId` version ~~, and adapts the Postgres code to always generate this V2 version~~. This new id contains the source name, in addition to the table name, in order to disambiguate similar table names across different sources (which is now possible with source customization). In doing so, it now explicitly handles that case for V1 node ids, and returns an explicit error message instead of running the risk of _silently returning the wrong information_.
Furthermore, it adapts `nodeField` to support multiple backends; most of the code was trivial to generalize, and as a result it lowers the cost of entry for other backends, that now only need to support `AFNodeId` in their translation layer.
Finally, it removes one more cycle in the schema building code, by using the same trick we used for remote relationships instead of using the memoization trick of #4576.
## Remaining work
- ~~[ ]write a Changelog entry~~
- ~~[x] adapt all tests that were asserting on an old node id~~
## Future work
This PR was adapted from its original form to avoid a breaking change: while it introduces a Node ID V2, we keep generating V1 IDs and the parser rejects V2 IDs. It will be easy to make the switch at a later data in a subsequent PR.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4593
GitOrigin-RevId: 88e5cb91e8b0646900547fa8c7c0e1463de267a1
2022-06-07 16:35:26 +03:00
|
|
|
nodeId <- parseNodeId ident
|
|
|
|
case nodeId of
|
|
|
|
NodeIdV1 (V1NodeId tableName pKeys) -> do
|
|
|
|
-- Node id V1.
|
|
|
|
--
|
|
|
|
-- We don't have the source name in a V1 node; we attempt all of them
|
|
|
|
-- and pick the first one we find; there is a risk we might pick the
|
|
|
|
-- wrong one if two tables with the same name exist in different
|
|
|
|
-- sources! It is, however, unlikely; the engine emits V2 IDs, meaning
|
|
|
|
-- if ever encounter a V1 ID it means it has been manually entered bya
|
|
|
|
-- user, saved from an older version of the engine?
|
|
|
|
let matchingTables = flip mapMaybe (Map.keys sourceCache) \sourceName ->
|
|
|
|
(sourceName,) <$> findNode @('Postgres 'Vanilla) sourceName tableName parseds
|
|
|
|
case matchingTables of
|
|
|
|
[(sourceName, nodeValue)] -> createRootField stringifyNum sourceName tableName nodeValue pKeys
|
|
|
|
[] -> throwInvalidNodeId $ "no such table found: " <>> tableName
|
|
|
|
l ->
|
|
|
|
throwInvalidNodeId $
|
|
|
|
"this V1 node id matches more than one table across different sources: " <> tableName
|
|
|
|
<<> " exists in sources "
|
|
|
|
<> commaSeparated (fst <$> l)
|
|
|
|
NodeIdV2 nodev2 ->
|
|
|
|
-- Node id V2.
|
|
|
|
--
|
|
|
|
-- We have the source name and table name, we can extract the relevant
|
|
|
|
-- info directly.
|
|
|
|
AB.dispatchAnyBackend @Backend nodev2 \(V2NodeId sourceName tableName pKeys :: V2NodeId b) -> do
|
|
|
|
nodeValue <-
|
|
|
|
findNode @b sourceName tableName parseds
|
|
|
|
`onNothing` throwInvalidNodeId ("no table " <> tableName <<> " found in source " <>> sourceName)
|
|
|
|
createRootField stringifyNum sourceName tableName nodeValue pKeys
|
|
|
|
where
|
|
|
|
throwInvalidNodeId :: Text -> n a
|
2022-07-06 10:54:57 +03:00
|
|
|
throwInvalidNodeId t = P.withKey (J.Key "args") $ P.withKey (J.Key "id") $ P.parseError $ "invalid node id: " <> t
|
Clean Relay's code, break schema cycles, introduce Node ID V2
## Motivation
This PR rewrites most of Relay to achieve the following:
- ~~fix a bug in which the same node id could refer to two different tables in the schema~~
- remove one of the few remaining uses of the source cache in the schema building code
In doing so, it also:
- simplifies the `BackendSchema` class by removing `node` from it,
- makes it much easier for other backends to support Relay,
- documents, re-organizes, and clarifies the code.
## Description
This PR introduces a new `NodeId` version ~~, and adapts the Postgres code to always generate this V2 version~~. This new id contains the source name, in addition to the table name, in order to disambiguate similar table names across different sources (which is now possible with source customization). In doing so, it now explicitly handles that case for V1 node ids, and returns an explicit error message instead of running the risk of _silently returning the wrong information_.
Furthermore, it adapts `nodeField` to support multiple backends; most of the code was trivial to generalize, and as a result it lowers the cost of entry for other backends, that now only need to support `AFNodeId` in their translation layer.
Finally, it removes one more cycle in the schema building code, by using the same trick we used for remote relationships instead of using the memoization trick of #4576.
## Remaining work
- ~~[ ]write a Changelog entry~~
- ~~[x] adapt all tests that were asserting on an old node id~~
## Future work
This PR was adapted from its original form to avoid a breaking change: while it introduces a Node ID V2, we keep generating V1 IDs and the parser rejects V2 IDs. It will be easy to make the switch at a later data in a subsequent PR.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4593
GitOrigin-RevId: 88e5cb91e8b0646900547fa8c7c0e1463de267a1
2022-06-07 16:35:26 +03:00
|
|
|
|
|
|
|
parseNodeId :: Text -> n NodeId
|
|
|
|
parseNodeId = either (throwInvalidNodeId . T.pack) pure . J.eitherDecode . base64Decode
|
|
|
|
|
|
|
|
-- Given all the node id information about a table, and the extracted
|
|
|
|
-- 'NodeInfo', craft the top-level query. This relies on the assumption
|
|
|
|
-- that all backends that support relay use the same IR for single row
|
|
|
|
-- selection.
|
|
|
|
createRootField ::
|
|
|
|
Backend b =>
|
|
|
|
StringifyNumbers ->
|
|
|
|
SourceName ->
|
|
|
|
TableName b ->
|
|
|
|
NodeInfo b ->
|
|
|
|
NESeq.NESeq J.Value ->
|
|
|
|
n (IR.QueryRootField IR.UnpreparedValue)
|
|
|
|
createRootField stringifyNum sourceName tableName (NodeInfo sourceConfig perms pKeys fields) columnValues = do
|
|
|
|
whereExp <- buildNodeIdBoolExp columnValues pKeys
|
|
|
|
pure $
|
|
|
|
IR.RFDB sourceName $
|
|
|
|
AB.mkAnyBackend $
|
|
|
|
IR.SourceConfigWith sourceConfig Nothing $
|
|
|
|
IR.QDBR $
|
|
|
|
IR.QDBSingleRow $
|
|
|
|
IR.AnnSelectG
|
|
|
|
{ IR._asnFields = fields,
|
|
|
|
IR._asnFrom = IR.FromTable tableName,
|
|
|
|
IR._asnPerm = tablePermissionsInfo perms,
|
|
|
|
IR._asnArgs =
|
|
|
|
IR.SelectArgs
|
|
|
|
{ IR._saWhere = Just whereExp,
|
|
|
|
IR._saOrderBy = Nothing,
|
|
|
|
IR._saLimit = Nothing,
|
|
|
|
IR._saOffset = Nothing,
|
|
|
|
IR._saDistinct = Nothing
|
|
|
|
},
|
|
|
|
IR._asnStrfyNum = stringifyNum
|
|
|
|
}
|
|
|
|
|
|
|
|
-- Craft the 'where' condition of the query by making an `AEQ` entry for
|
|
|
|
-- each primary key. This might fail if the given node id doesn't exactly
|
|
|
|
-- have a valid entry for each primary key.
|
|
|
|
buildNodeIdBoolExp ::
|
|
|
|
Backend b =>
|
|
|
|
NESeq.NESeq J.Value ->
|
|
|
|
NESeq.NESeq (ColumnInfo b) ->
|
|
|
|
n (IR.AnnBoolExp b (IR.UnpreparedValue b))
|
|
|
|
buildNodeIdBoolExp columnValues pkeyColumns = do
|
|
|
|
let firstPkColumn NESeq.:<|| remainingPkColumns = pkeyColumns
|
|
|
|
firstColumnValue NESeq.:<|| remainingColumns = columnValues
|
|
|
|
(nonAlignedPkColumns, nonAlignedColumnValues, alignedTuples) =
|
|
|
|
partitionThese $ toList $ align remainingPkColumns remainingColumns
|
|
|
|
|
|
|
|
unless (null nonAlignedPkColumns) $
|
|
|
|
throwInvalidNodeId $
|
|
|
|
"primary key columns " <> dquoteList (map ciColumn nonAlignedPkColumns) <> " are missing"
|
|
|
|
|
|
|
|
unless (null nonAlignedColumnValues) $
|
|
|
|
throwInvalidNodeId $
|
|
|
|
"unexpected column values " <> J.encodeToStrictText nonAlignedColumnValues
|
|
|
|
|
|
|
|
let allTuples = (firstPkColumn, firstColumnValue) : alignedTuples
|
|
|
|
IR.BoolAnd <$> for allTuples \(columnInfo, columnValue) -> do
|
|
|
|
let columnType = ciType columnInfo
|
|
|
|
parsedValue <-
|
|
|
|
parseScalarValueColumnType columnType columnValue `onLeft` \e ->
|
server: Metadata origin for definitions (type parameter version v2)
The code that builds the GraphQL schema, and `buildGQLContext` in particular, is partial: not every value of `(ServerConfigCtx, GraphQLQueryType, SourceCache, HashMap RemoteSchemaName (RemoteSchemaCtx, MetadataObject), ActionCache, AnnotatedCustomTypes)` results in a valid GraphQL schema. When it fails, we want to be able to return better error messages than we currently do.
The key thing that is missing is a way to trace back GraphQL type information to their origin from the Hasura metadata. Currently, we have a number of correctness checks of our GraphQL schema. But these correctness checks only have access to pure GraphQL type information, and hence can only report errors in terms of that. Possibly the worst is the "conflicting definitions" error, which, in practice, can only be debugged by Hasura engineers. This is terrible DX for customers.
This PR allows us to print better error messages, by adding a field to the `Definition` type that traces the GraphQL type to its origin in the metadata. So the idea is simple: just add `MetadataObjId`, or `Maybe` that, or some other sum type of that, to `Definition`.
However, we want to avoid having to import a `Hasura.RQL` module from `Hasura.GraphQL.Parser`. So we instead define this additional field of `Definition` through a new type parameter, which is threaded through in `Hasura.GraphQL.Parser`. We then define type synonyms in `Hasura.GraphQL.Schema.Parser` that fill in this type parameter, so that it is not visible for the majority of the codebase.
The idea of associating metadata information to `Definition`s really comes to fruition when combined with hasura/graphql-engine-mono#4517. Their combination would allow us to use the API of fatal errors (just like the current `MonadError QErr`) to report _inconsistencies_ in the metadata. Such inconsistencies are then _automatically_ ignored. So no ad-hoc decisions need to be made on how to cut out inconsistent metadata from the GraphQL schema. This will allow us to report much better errors, as well as improve the likelihood of a successful HGE startup.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4770
Co-authored-by: Samir Talwar <47582+SamirTalwar@users.noreply.github.com>
GitOrigin-RevId: 728402b0cae83ae8e83463a826ceeb609001acae
2022-06-28 18:52:26 +03:00
|
|
|
P.parseErrorWith ParseFailed $ "value of column " <> ciColumn columnInfo <<> " in node id: " <> qeError e
|
Clean Relay's code, break schema cycles, introduce Node ID V2
## Motivation
This PR rewrites most of Relay to achieve the following:
- ~~fix a bug in which the same node id could refer to two different tables in the schema~~
- remove one of the few remaining uses of the source cache in the schema building code
In doing so, it also:
- simplifies the `BackendSchema` class by removing `node` from it,
- makes it much easier for other backends to support Relay,
- documents, re-organizes, and clarifies the code.
## Description
This PR introduces a new `NodeId` version ~~, and adapts the Postgres code to always generate this V2 version~~. This new id contains the source name, in addition to the table name, in order to disambiguate similar table names across different sources (which is now possible with source customization). In doing so, it now explicitly handles that case for V1 node ids, and returns an explicit error message instead of running the risk of _silently returning the wrong information_.
Furthermore, it adapts `nodeField` to support multiple backends; most of the code was trivial to generalize, and as a result it lowers the cost of entry for other backends, that now only need to support `AFNodeId` in their translation layer.
Finally, it removes one more cycle in the schema building code, by using the same trick we used for remote relationships instead of using the memoization trick of #4576.
## Remaining work
- ~~[ ]write a Changelog entry~~
- ~~[x] adapt all tests that were asserting on an old node id~~
## Future work
This PR was adapted from its original form to avoid a breaking change: while it introduces a Node ID V2, we keep generating V1 IDs and the parser rejects V2 IDs. It will be easy to make the switch at a later data in a subsequent PR.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4593
GitOrigin-RevId: 88e5cb91e8b0646900547fa8c7c0e1463de267a1
2022-06-07 16:35:26 +03:00
|
|
|
pure $
|
2022-07-12 12:25:22 +03:00
|
|
|
IR.BoolField $
|
Clean Relay's code, break schema cycles, introduce Node ID V2
## Motivation
This PR rewrites most of Relay to achieve the following:
- ~~fix a bug in which the same node id could refer to two different tables in the schema~~
- remove one of the few remaining uses of the source cache in the schema building code
In doing so, it also:
- simplifies the `BackendSchema` class by removing `node` from it,
- makes it much easier for other backends to support Relay,
- documents, re-organizes, and clarifies the code.
## Description
This PR introduces a new `NodeId` version ~~, and adapts the Postgres code to always generate this V2 version~~. This new id contains the source name, in addition to the table name, in order to disambiguate similar table names across different sources (which is now possible with source customization). In doing so, it now explicitly handles that case for V1 node ids, and returns an explicit error message instead of running the risk of _silently returning the wrong information_.
Furthermore, it adapts `nodeField` to support multiple backends; most of the code was trivial to generalize, and as a result it lowers the cost of entry for other backends, that now only need to support `AFNodeId` in their translation layer.
Finally, it removes one more cycle in the schema building code, by using the same trick we used for remote relationships instead of using the memoization trick of #4576.
## Remaining work
- ~~[ ]write a Changelog entry~~
- ~~[x] adapt all tests that were asserting on an old node id~~
## Future work
This PR was adapted from its original form to avoid a breaking change: while it introduces a Node ID V2, we keep generating V1 IDs and the parser rejects V2 IDs. It will be easy to make the switch at a later data in a subsequent PR.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4593
GitOrigin-RevId: 88e5cb91e8b0646900547fa8c7c0e1463de267a1
2022-06-07 16:35:26 +03:00
|
|
|
IR.AVColumn
|
|
|
|
columnInfo
|
|
|
|
[IR.AEQ True $ IR.UVParameter Nothing $ ColumnValue columnType parsedValue]
|