graphql-engine/server/src-test/Hasura/GraphQL/Parser/TestUtils.hs
Auke Booij caf9957aca Remove Unique from Definition
GraphQL types can refer to each other in a circular way. The PDV framework used to use values of type `Unique` to recognize two fragments of GraphQL schema as being the same instance. Internally, this is based on `Data.Unique` from the `base` package, which simply increases a counter on every creation of a `Unique` object.

**NB**: The `Unique` values are _not_ used for knot tying the schema combinators themselves (i.e. `Parser`s). The knot tying for `Parser`s is purely based on keys provided to `memoizeOn`. The `Unique` values are _only_ used to recognize two pieces of GraphQL _schema_ as being identical. Originally, the idea was that this would help us with a perfectly correct identification of GraphQL types. But this fully correct equality checking of GraphQL types was never implemented, and does not seem to be necessary to prevent bugs.

Specifically, these `Unique` values are stored as part of `data Definition a`, which specifies a part of our internal abstract syntax tree for the GraphQL types that we expose. The `Unique` values get initialized by the `SchemaT` effect.

In #2894 and #2895, we are experimenting with how (parts of) the GraphQL types can be hidden behind certain permission predicates. This would allow a single GraphQL schema in memory to serve all roles, implementing #2711. The permission predicates get evaluated at query parsing time when we know what role is doing a certain request, thus outputting the correct GraphQL types for that role.

If the approach of #2895 is followed, then the `Definition` objects, and thus the `Unique` values, would be hidden behind the permission predicates. Since the permission predicates are evaluated only after the schema is already supposed to be built, this means that the permission predicates would prevent us from initializing the `Unique` values, rendering them useless.

The simplest remedy to this is to remove our usage of `Unique` altogether from the GraphQL schema and schema combinators. It doesn't serve a functional purpose, doesn't prevent bugs, and requires extra bookkeeping.

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/2980
GitOrigin-RevId: 50d3f9e0b9fbf578ac49c8fc773ba64a94b1f43d
2021-12-01 16:21:35 +00:00

62 lines
1.9 KiB
Haskell

module Hasura.GraphQL.Parser.TestUtils
( TestMonad (..),
fakeScalar,
fakeInputFieldValue,
fakeDirective,
)
where
import Data.HashMap.Strict qualified as M
import Data.List.NonEmpty qualified as NE
import Data.Text qualified as T
import Hasura.GraphQL.Parser.Class.Parse
import Hasura.GraphQL.Parser.Schema
import Hasura.Prelude
import Language.GraphQL.Draft.Syntax qualified as G
-- test monad
newtype TestMonad a = TestMonad {runTest :: Either Text a}
deriving (Functor, Applicative, Monad)
instance MonadParse TestMonad where
withPath = const id
parseErrorWith = const $ TestMonad . Left
-- values generation
fakeScalar :: G.Name -> G.Value Variable
fakeScalar =
G.unName >>> \case
"Int" -> G.VInt 4242
"Boolean" -> G.VBoolean False
name -> error $ "no test value implemented for scalar " <> T.unpack name
fakeInputFieldValue :: InputFieldInfo -> G.Value Variable
fakeInputFieldValue = \case
IFOptional t _ -> fromT t
IFRequired nnt -> fromNNT nnt
where
fromT :: forall k. ('Input <: k) => Type k -> G.Value Variable
fromT = \case
NonNullable nnt -> fromNNT nnt
Nullable nnt -> fromNNT nnt
fromNNT :: forall k. ('Input <: k) => NonNullableType k -> G.Value Variable
fromNNT = \case
TList t -> G.VList [fromT t, fromT t]
TNamed (Definition name _ info) -> case info of
TIScalar -> fakeScalar name
TIEnum ei -> G.VEnum $ G.EnumValue $ dName $ NE.head ei
TIInputObject (InputObjectInfo oi) -> G.VObject $
M.fromList $ do
Definition fieldName _ fieldInfo <- oi
pure (fieldName, fakeInputFieldValue fieldInfo)
_ -> error "impossible"
fakeDirective :: DirectiveInfo -> G.Directive Variable
fakeDirective DirectiveInfo {..} =
G.Directive diName $
M.fromList $
diArguments <&> \(Definition argName _ argInfo) ->
(argName, fakeInputFieldValue argInfo)