2021-10-29 17:42:07 +03:00
|
|
|
module Hasura.GraphQL.Namespace
|
|
|
|
( RootFieldAlias (..),
|
|
|
|
mkUnNamespacedRootFieldAlias,
|
|
|
|
mkNamespacedRootFieldAlias,
|
|
|
|
RootFieldMap,
|
|
|
|
NamespacedField (..),
|
|
|
|
namespacedField,
|
|
|
|
NamespacedFieldMap,
|
|
|
|
flattenNamespaces,
|
|
|
|
unflattenNamespaces,
|
2021-11-30 03:37:14 +03:00
|
|
|
customizeNamespace,
|
2021-10-29 17:42:07 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Data.Aeson qualified as J
|
2023-04-27 10:41:55 +03:00
|
|
|
import Data.HashMap.Strict.InsOrd qualified as InsOrdHashMap
|
2021-10-29 17:42:07 +03:00
|
|
|
import Data.Text.Extended
|
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 as P
|
2022-07-12 17:00:15 +03:00
|
|
|
import Hasura.GraphQL.Schema.Typename
|
2021-10-29 17:42:07 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
|
|
|
|
|
|
|
data RootFieldAlias = RootFieldAlias
|
|
|
|
{ _rfaNamespace :: !(Maybe G.Name),
|
|
|
|
_rfaAlias :: !G.Name
|
|
|
|
}
|
|
|
|
deriving (Show, Eq, Generic)
|
|
|
|
|
|
|
|
instance Hashable RootFieldAlias
|
|
|
|
|
|
|
|
instance ToTxt RootFieldAlias where
|
|
|
|
toTxt RootFieldAlias {..} = case _rfaNamespace of
|
|
|
|
Nothing -> G.unName _rfaAlias
|
|
|
|
Just ns -> G.unName ns <> "." <> G.unName _rfaAlias
|
|
|
|
|
|
|
|
-- | This ToJSON instance is used in responses to the explain API
|
|
|
|
-- (via the ToJSON instance for ExplainPlan).
|
|
|
|
-- It will use dot separator for namespaces fields, i.e. "namespace.fieldname"
|
|
|
|
-- TODO: We need to decide if this dotted notation is what we want to use for explain responses.
|
|
|
|
instance J.ToJSON RootFieldAlias where
|
|
|
|
toJSON = J.toJSON . toTxt
|
|
|
|
|
|
|
|
mkUnNamespacedRootFieldAlias :: G.Name -> RootFieldAlias
|
|
|
|
mkUnNamespacedRootFieldAlias = RootFieldAlias Nothing
|
|
|
|
|
|
|
|
mkNamespacedRootFieldAlias :: G.Name -> G.Name -> RootFieldAlias
|
|
|
|
mkNamespacedRootFieldAlias = RootFieldAlias . Just
|
|
|
|
|
|
|
|
type RootFieldMap = InsOrdHashMap RootFieldAlias
|
|
|
|
|
|
|
|
data NamespacedField a
|
|
|
|
= -- | Normal field
|
|
|
|
NotNamespaced a
|
|
|
|
| -- | Namespace field with other fields nested within
|
|
|
|
Namespaced (InsOrdHashMap G.Name a)
|
|
|
|
deriving (Eq, Show, Functor)
|
|
|
|
|
|
|
|
namespacedField :: (a -> b) -> (InsOrdHashMap G.Name a -> b) -> NamespacedField a -> b
|
|
|
|
namespacedField f g = \case
|
|
|
|
NotNamespaced a -> f a
|
|
|
|
Namespaced m -> g m
|
|
|
|
|
|
|
|
type NamespacedFieldMap a = InsOrdHashMap G.Name (NamespacedField a)
|
|
|
|
|
|
|
|
flattenNamespaces :: forall a. NamespacedFieldMap a -> RootFieldMap a
|
2023-04-27 10:41:55 +03:00
|
|
|
flattenNamespaces = InsOrdHashMap.foldMapWithKey flattenNamespace
|
2021-10-29 17:42:07 +03:00
|
|
|
where
|
|
|
|
flattenNamespace :: G.Name -> NamespacedField a -> RootFieldMap a
|
|
|
|
flattenNamespace fieldName =
|
|
|
|
namespacedField
|
2023-04-27 10:41:55 +03:00
|
|
|
(InsOrdHashMap.singleton $ mkUnNamespacedRootFieldAlias fieldName)
|
|
|
|
(InsOrdHashMap.mapKeys $ mkNamespacedRootFieldAlias fieldName)
|
2021-10-29 17:42:07 +03:00
|
|
|
|
|
|
|
unflattenNamespaces :: RootFieldMap a -> NamespacedFieldMap a
|
2023-04-27 10:41:55 +03:00
|
|
|
unflattenNamespaces = InsOrdHashMap.foldlWithKey' insert mempty
|
2021-10-29 17:42:07 +03:00
|
|
|
where
|
|
|
|
insert m RootFieldAlias {..} v = case _rfaNamespace of
|
2023-04-27 10:41:55 +03:00
|
|
|
Nothing -> InsOrdHashMap.insert _rfaAlias (NotNamespaced v) m
|
|
|
|
Just ns -> InsOrdHashMap.insertWith merge ns (Namespaced $ (InsOrdHashMap.singleton _rfaAlias v)) m
|
|
|
|
merge (Namespaced m) (Namespaced m') = Namespaced (InsOrdHashMap.union m' m) -- Note: order of arguments to InsOrdHashMap.union to preserve ordering
|
2021-10-29 17:42:07 +03:00
|
|
|
merge v _ = v
|
2021-11-30 03:37:14 +03:00
|
|
|
|
|
|
|
-- | Wrap the field parser results in @NamespacedField@
|
|
|
|
customizeNamespace ::
|
|
|
|
forall n a.
|
|
|
|
(MonadParse n) =>
|
|
|
|
Maybe G.Name ->
|
|
|
|
(G.Name -> P.ParsedSelection a -> a) ->
|
2022-07-12 17:00:15 +03:00
|
|
|
MkTypename ->
|
2021-11-30 03:37:14 +03:00
|
|
|
[FieldParser n a] ->
|
|
|
|
[FieldParser n (NamespacedField a)]
|
2022-07-06 08:40:16 +03:00
|
|
|
customizeNamespace (Just _) _ _ [] = [] -- The nampespace doesn't contain any Field parsers, so returning empty list
|
2021-11-30 03:37:14 +03:00
|
|
|
customizeNamespace (Just namespace) fromParsedSelection mkNamespaceTypename fieldParsers =
|
|
|
|
-- Source or remote schema has a namespace field so wrap the parsers
|
|
|
|
-- in a new namespace field parser.
|
|
|
|
[P.subselection_ namespace Nothing parser]
|
|
|
|
where
|
|
|
|
parser :: Parser 'Output n (NamespacedField a)
|
|
|
|
parser =
|
2023-05-24 16:51:56 +03:00
|
|
|
Namespaced
|
|
|
|
. InsOrdHashMap.mapWithKey fromParsedSelection
|
Refactor type name customization
Source typename customization (hasura/graphql-engine@aac64f2c81faa6a3aef4d0cf5fae97289ac4383e) introduced a mechanism to change certain names in the GraphQL schema that is exposed. In particular it allows last-minute modification of:
1. the names of some types, and
2. the names of some root fields.
The above two items are assigned distinct customization algorithms, and at times both algorithms are in scope. So a need to distinguish them is needed.
In the original design, this was addressed by introducing a newtype wrapper `Typename` around GraphQL `Name`s, dedicated to the names of types. However, in the majority of the codebase, type names are also represented by `Name`. For this reason, it was unavoidable to allow for easy conversion. This was supported by a `HasName Typename` instance, as well as by publishing the constructors of `Typename`.
This means that the type safety that newtypes can add is lost. In particular, it is now very easy to confuse type name customization with root field name customization.
This refactors the above design by instead introducing newtypes around the customization operations:
```haskell
newtype MkTypename = MkTypename {runMkTypename :: Name -> Name}
deriving (Semigroup, Monoid) via (Endo Name)
newtype MkRootFieldName = MkRootFieldName {runMkRootFieldName :: Name -> Name}
deriving (Semigroup, Monoid) via (Endo Name)
```
The `Monoid` instance allows easy composition of customization operations, piggybacking off of the type of `Endo`maps.
This design allows safe co-existence of the two customization algorithms, while avoiding the syntactic overhead of packing and unpacking newtypes.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/2989
GitOrigin-RevId: da3a353a9b003ee40c8d0a1e02872e99d2edd3ca
2021-11-30 12:51:46 +03:00
|
|
|
<$> P.selectionSet (runMkTypename mkNamespaceTypename namespace) Nothing fieldParsers
|
2021-11-30 03:37:14 +03:00
|
|
|
customizeNamespace Nothing _ _ fieldParsers =
|
|
|
|
-- No namespace so just wrap the field parser results in @NotNamespaced@.
|
|
|
|
fmap NotNamespaced <$> fieldParsers
|