mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-30 02:44:10 +03:00
8ccf7724ce
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
66 lines
2.3 KiB
Haskell
66 lines
2.3 KiB
Haskell
{-# LANGUAGE ViewPatterns #-}
|
|
|
|
-- | This module defines all functions that convert between different
|
|
-- representations of values in the schema; most commonly: GraphQL literals,
|
|
-- JSON values, and 'InputValue', a type that provides an abstraction above both
|
|
-- of those.
|
|
module Hasura.GraphQL.Parser.Internal.Convert
|
|
( jsonToGraphQL,
|
|
valueToJSON,
|
|
)
|
|
where
|
|
|
|
import Data.Aeson qualified as A
|
|
import Data.Aeson.Key qualified as K
|
|
import Data.Aeson.KeyMap qualified as KM
|
|
import Data.HashMap.Strict.Extended qualified as M
|
|
import Data.Int (Int64)
|
|
import Data.Scientific (toBoundedInteger)
|
|
import Data.Text.Extended
|
|
import Hasura.GraphQL.Parser.Class.Parse
|
|
import Hasura.GraphQL.Parser.Internal.TypeChecking
|
|
import Hasura.GraphQL.Parser.Variable
|
|
import Hasura.Prelude
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
|
|
|
valueToJSON :: MonadParse m => G.GType -> InputValue Variable -> m A.Value
|
|
valueToJSON expectedType inputVal = do
|
|
peeledVal <- peelVariable expectedType inputVal
|
|
pure $ valueToJSON' peeledVal
|
|
where
|
|
valueToJSON' :: InputValue Variable -> A.Value
|
|
valueToJSON' = \case
|
|
JSONValue j -> j
|
|
GraphQLValue g -> graphQLToJSON g
|
|
|
|
graphQLToJSON :: G.Value Variable -> A.Value
|
|
graphQLToJSON = \case
|
|
G.VNull -> A.Null
|
|
G.VInt i -> A.toJSON i
|
|
G.VFloat f -> A.toJSON f
|
|
G.VString t -> A.toJSON t
|
|
G.VBoolean b -> A.toJSON b
|
|
G.VEnum (G.EnumValue n) -> A.toJSON n
|
|
G.VList values -> A.toJSON $ graphQLToJSON <$> values
|
|
G.VObject objects -> A.toJSON $ graphQLToJSON <$> objects
|
|
G.VVariable variable -> valueToJSON' $ absurd <$> vValue variable
|
|
|
|
jsonToGraphQL :: MonadError Text m => A.Value -> m (G.Value Void)
|
|
jsonToGraphQL = \case
|
|
A.Null -> pure G.VNull
|
|
A.Bool val -> pure $ G.VBoolean val
|
|
A.String val -> pure $ G.VString val
|
|
A.Number val -> case toBoundedInteger val of
|
|
Just intVal -> pure $ G.VInt $ fromIntegral @Int64 intVal
|
|
Nothing -> pure $ G.VFloat val
|
|
A.Array vals -> G.VList <$> traverse jsonToGraphQL (toList vals)
|
|
A.Object vals ->
|
|
G.VObject . M.fromList <$> for (KM.toList vals) \(K.toText -> key, val) -> do
|
|
graphQLName <-
|
|
G.mkName key
|
|
`onNothing` throwError
|
|
( "variable value contains an object with key "
|
|
<> key <<> ", which is not a legal GraphQL name"
|
|
)
|
|
(graphQLName,) <$> jsonToGraphQL val
|