graphql-engine/server/src-lib/Hasura/GraphQL/Schema/Parser.hs
Auke Booij 8ccf7724ce 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 15:53:44 +00:00

109 lines
3.7 KiB
Haskell

{-# LANGUAGE PatternSynonyms #-}
-- | In 'Hasura.GraphQL.Parser', the 'Definition' type has a 'dOrigin' field
-- that allows to track where a fragment of GraphQL type information comes from.
-- This is useful for error reporting and internal repair mechanisms such as
-- inconsistency tracking.
--
-- Morally, within the HGE codebase, this origin is always 'MetadataObjId'.
-- However, in order to avoid an import of 'Hasura.RQL' from
-- 'Hasura.GraphQL.Parser', the 'dOrigin' has been defined through a type
-- parameter of 'Definition'. This type parameter then has to get threaded
-- through throughout the 'Hasura.GraphQL.Parser' module hierarchy, so that it
-- ends up in a lot of types. This is very noisy.
--
-- In order to avoid the noise of this type parameter, which really only has one
-- value, and is really only used in one type, this module erases the type
-- parameter by filling in the desired value, exporting type synonyms of the
-- now-fixed notion of "origin". So most modules in the HGE codebase should
-- import this module rather than 'Hasura.GraphQL.Parser'.
module Hasura.GraphQL.Schema.Parser
( -- The pattern is as follows:
-- 1. Export a type synonym which has the origin type parameter set to
-- 'MetadataObjId'
FieldParser,
-- 2. Export the constructor of the type. Note that despite the use of
-- 'PatternSynonyms', there is no pattern being defined. The reason for
-- using 'PatternSynonyms' is that that extension (and the 'pattern'
-- syntax) allows re-exporting a constructor of a type, without
-- re-exporting its original associated type. This is not possible in
-- plain Haskell2010.
pattern P.FieldParser,
InputFieldsParser,
pattern P.InputFieldsParser,
Parser,
pattern P.Parser,
Schema,
pattern P.Schema,
Definition,
pattern P.Definition,
Type,
Directive,
pattern P.Directive,
DirectiveInfo,
pattern P.DirectiveInfo,
FieldInfo,
pattern P.FieldInfo,
InputFieldInfo,
pattern P.InputFieldInfo,
HasTypeDefinitions,
SomeDefinitionTypeInfo,
pattern P.SomeDefinitionTypeInfo,
TypeDefinitionsWrapper,
pattern TypeDefinitionsWrapper,
module Hasura.GraphQL.Parser,
)
where
-- Re-export everything, except types whose type parameter we want to fill in in
-- this module.
import Hasura.GraphQL.Parser hiding
( Definition,
Directive,
DirectiveInfo,
FieldInfo,
FieldParser,
HasTypeDefinitions,
InputFieldInfo,
InputFieldsParser,
Parser,
Schema,
SomeDefinitionTypeInfo,
Type,
TypeDefinitionsWrapper,
)
import Hasura.GraphQL.Parser qualified as P
import Hasura.RQL.Types.Metadata.Object
type FieldParser = P.FieldParser MetadataObjId
type Parser = P.Parser MetadataObjId
type Schema = P.Schema MetadataObjId
type Type = P.Type MetadataObjId
type InputFieldsParser = P.InputFieldsParser MetadataObjId
type Definition = P.Definition MetadataObjId
type Directive = P.Directive MetadataObjId
type DirectiveInfo = P.DirectiveInfo MetadataObjId
type FieldInfo = P.FieldInfo MetadataObjId
type InputFieldInfo = P.InputFieldInfo MetadataObjId
type HasTypeDefinitions = P.HasTypeDefinitions MetadataObjId
type SomeDefinitionTypeInfo = P.SomeDefinitionTypeInfo MetadataObjId
type TypeDefinitionsWrapper = P.TypeDefinitionsWrapper MetadataObjId
-- | In order to aid type inference and type checking, we define this pattern
-- synonym (an actual one) which restricts 'P.TypeDefinitionsWrapper' to have
-- 'MetadataObjId' set for its origin type parameter.
pattern TypeDefinitionsWrapper :: () => forall a. HasTypeDefinitions a => a -> TypeDefinitionsWrapper
pattern TypeDefinitionsWrapper typeDef = P.TypeDefinitionsWrapper typeDef