2022-05-26 17:05:13 +03:00
|
|
|
-- | QuasiQuoter for parsing GraphQL fields in tests. See 'field' for details.
|
|
|
|
module Test.Parser.Field (field) where
|
|
|
|
|
2022-09-07 14:20:53 +03:00
|
|
|
import Control.Monad.Trans.Except
|
2022-05-26 17:05:13 +03:00
|
|
|
import Data.Attoparsec.Text qualified as Parser
|
|
|
|
import Data.Text qualified as T
|
2022-09-07 14:20:53 +03:00
|
|
|
import Hasura.Base.Error (showQErr)
|
|
|
|
import Hasura.GraphQL.Execute.Inline (inlineField, runInlineM)
|
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.Parser.Variable
|
2022-05-26 17:05:13 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Language.GraphQL.Draft.Parser qualified as GraphQL
|
|
|
|
import Language.GraphQL.Draft.Syntax qualified as GraphQL
|
|
|
|
import Language.Haskell.TH.Lib (ExpQ)
|
|
|
|
import Language.Haskell.TH.Quote
|
|
|
|
import Language.Haskell.TH.Syntax qualified as TH
|
|
|
|
|
2022-09-07 14:20:53 +03:00
|
|
|
-- | Quasi-Quoter for GraphQL fields.
|
2022-05-26 17:05:13 +03:00
|
|
|
-- Example usage:
|
|
|
|
-- > [GQL.field|
|
|
|
|
-- > update_artist(
|
|
|
|
-- > where: { name: { _eq: "old name"}},
|
|
|
|
-- > _set: { name: "new name" }
|
|
|
|
-- > ) {
|
|
|
|
-- > affected_rows
|
|
|
|
-- > }
|
|
|
|
-- > |],
|
|
|
|
field :: QuasiQuoter
|
|
|
|
field =
|
|
|
|
QuasiQuoter
|
2022-09-07 14:20:53 +03:00
|
|
|
{ quoteExp = fieldExp,
|
2022-05-26 17:05:13 +03:00
|
|
|
quotePat = \_ -> fail "invalid",
|
|
|
|
quoteType = \_ -> fail "invalid",
|
|
|
|
quoteDec = \_ -> fail "invalid"
|
|
|
|
}
|
|
|
|
where
|
2022-09-07 14:20:53 +03:00
|
|
|
fieldExp :: String -> ExpQ
|
|
|
|
fieldExp input = do
|
|
|
|
either fail TH.lift $
|
|
|
|
runExcept $ do
|
|
|
|
parsed <- hoistEither $ Parser.parseOnly (Parser.skipSpace *> GraphQL.field @GraphQL.Name) . T.pack $ input
|
|
|
|
fixField parsed
|
2022-05-26 17:05:13 +03:00
|
|
|
|
2022-09-07 14:20:53 +03:00
|
|
|
-- A parsed field can contain both fragments and variables.
|
|
|
|
-- We support neither yet.
|
|
|
|
fixField :: GraphQL.Field GraphQL.FragmentSpread GraphQL.Name -> Except String (GraphQL.Field GraphQL.NoFragments Variable)
|
|
|
|
fixField f = do
|
|
|
|
x <- except $ mapLeft (T.unpack . showQErr) $ runInlineM mempty . inlineField $ f
|
|
|
|
traverse (throwE . ("Variables are not supported in tests yet: " ++) . show) x
|