-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Common.hs
637 lines (554 loc) · 26 KB
/
Common.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}
-- ghc 9.6 seems to be doing something screwy with...
{-# OPTIONS_GHC -Wno-redundant-constraints #-}
module Hasura.GraphQL.Schema.Common
( SchemaContext (..),
SchemaSampledFeatureFlags (..),
sampleFeatureFlags,
WithSchemaSampledFeatureFlags,
withSchemaSampledFeatureFlags,
SchemaKind (..),
RemoteRelationshipParserBuilder (..),
NodeInterfaceParserBuilder (..),
MonadBuildSchemaBase,
retrieve,
SchemaT (..),
MonadBuildSourceSchema,
MonadBuildRemoteSchema,
MonadBuildActionSchema,
runSourceSchema,
runRemoteSchema,
runActionSchema,
ignoreRemoteRelationship,
isHasuraSchema,
AggSelectExp,
AnnotatedField,
AnnotatedFields,
ConnectionFields,
ConnectionSelectExp,
AnnotatedActionField,
AnnotatedActionFields,
AnnotatedNestedObjectSelect,
AnnotatedNestedArraySelect,
EdgeFields,
Scenario (..),
SelectArgs,
SelectStreamArgs,
SelectExp,
StreamSelectExp,
TablePerms,
getTableRoles,
getLogicalModelRoles,
askScalarTypeParsingContext,
askTableInfo,
askLogicalModelInfo,
askNativeQueryInfo,
comparisonAggOperators,
mapField,
mkDescriptionWith,
numericAggOperators,
optionalFieldParser,
parsedSelectionsToFields,
partialSQLExpToUnpreparedValue,
getRedactionExprForColumn,
getRedactionExprForComputedField,
requiredFieldParser,
takeValidNativeQueries,
takeValidStoredProcedures,
takeValidFunctions,
takeValidTables,
textToName,
textToGQLIdentifier,
RemoteSchemaParser (..),
mkEnumTypeName,
addEnumSuffix,
peelWithOrigin,
getIntrospectionResult,
tablePermissionsInfo,
)
where
import Control.Monad.Trans.Control
import Data.Either (isRight)
import Data.Has
import Data.HashMap.Strict qualified as HashMap
import Data.HashMap.Strict.InsOrd qualified as InsOrdHashMap
import Data.List (uncons)
import Data.Text qualified as T
import Data.Text.Casing (GQLNameIdentifier)
import Data.Text.Casing qualified as C
import Data.Text.Extended
import Hasura.Backends.Postgres.SQL.Types qualified as Postgres
import Hasura.Base.Error
import Hasura.Function.Cache
import Hasura.GraphQL.Namespace (NamespacedField)
import Hasura.GraphQL.Parser.Internal.TypeChecking qualified as P
import Hasura.GraphQL.Schema.Node
import Hasura.GraphQL.Schema.Parser qualified as P
import Hasura.GraphQL.Schema.Typename
import Hasura.LogicalModel.Cache (LogicalModelInfo (_lmiPermissions))
import Hasura.LogicalModel.Types (LogicalModelName)
import Hasura.NativeQuery.Cache (NativeQueryCache, NativeQueryInfo (..))
import Hasura.NativeQuery.Types (NativeQueryName)
import Hasura.Prelude
import Hasura.RQL.IR qualified as IR
import Hasura.RQL.IR.BoolExp
import Hasura.RQL.Types.Backend
import Hasura.RQL.Types.Common
import Hasura.RQL.Types.ComputedField.Name (ComputedFieldName)
import Hasura.RQL.Types.Relationships.Remote
import Hasura.RQL.Types.Roles (RoleName, adminRoleName)
import Hasura.RQL.Types.Schema.Options (SchemaOptions)
import Hasura.RQL.Types.Schema.Options qualified as Options
import Hasura.RQL.Types.SchemaCache hiding (askTableInfo)
import Hasura.RQL.Types.Source
import Hasura.RQL.Types.SourceCustomization
import Hasura.RemoteSchema.SchemaCache.Types
import Hasura.SQL.AnyBackend qualified as AB
import Hasura.Server.Init.FeatureFlag qualified as FF
import Hasura.StoredProcedure.Cache (StoredProcedureCache)
import Hasura.Table.Cache (SelPermInfo (..))
import Language.GraphQL.Draft.Syntax qualified as G
-------------------------------------------------------------------------------
-- | Aggregation of contextual information required to build the schema.
data SchemaContext = SchemaContext
{ -- | the kind of schema being built
scSchemaKind :: SchemaKind,
-- | how to process remote relationships
scRemoteRelationshipParserBuilder :: RemoteRelationshipParserBuilder,
-- | the role for which the schema is being built
scRole :: RoleName,
scSampledFeatureFlags :: SchemaSampledFeatureFlags
}
-- | We want to be able to probe feature flags in the schema parsers, but we also
-- want to be able to run schema actions without requiring IO, in part because we
-- want to be able to run them in tests and in part because we want some assurance
-- that the schema we parse doesn't change without our knowledge, as that precludes
-- safely caching schema introspection.
newtype SchemaSampledFeatureFlags = SchemaSampledFeatureFlags [(FF.FeatureFlag, Bool)]
deriving (Eq, Show)
sampleFeatureFlags :: FF.CheckFeatureFlag -> IO SchemaSampledFeatureFlags
sampleFeatureFlags checkFeatureFlag = do
let ffs = map fst $ FF.listKnownFeatureFlags checkFeatureFlag
SchemaSampledFeatureFlags <$> mapM (\ff -> (ff,) <$> FF.runCheckFeatureFlag checkFeatureFlag ff) ffs
-- | Monad transformer that lets you use the sampled feature flags from a reader environment.
-- This is necessary because we want 'ReaderT r m` to be transparent wrt. 'HasFeatureFlagChecker m'.
newtype WithSchemaSampledFeatureFlags m a = WithSchemaSampledFeatureFlags {unWithSchemaSampledFeatureFlags :: ReaderT SchemaSampledFeatureFlags m a}
deriving (Functor, Applicative, Monad, MonadIO)
deriving instance (MonadBase IO m) => MonadBase IO (WithSchemaSampledFeatureFlags m)
deriving instance (MonadBaseControl IO m, MonadBase IO m) => MonadBaseControl IO (WithSchemaSampledFeatureFlags m)
instance MonadTrans WithSchemaSampledFeatureFlags where
lift = WithSchemaSampledFeatureFlags . lift
instance (MonadResolveSource m) => MonadResolveSource (WithSchemaSampledFeatureFlags m) where
getPGSourceResolver = lift getPGSourceResolver
getMSSQLSourceResolver = lift getMSSQLSourceResolver
withSchemaSampledFeatureFlags :: SchemaSampledFeatureFlags -> WithSchemaSampledFeatureFlags m a -> m a
withSchemaSampledFeatureFlags ffs = flip runReaderT ffs . unWithSchemaSampledFeatureFlags
instance (Monad m) => FF.HasFeatureFlagChecker (WithSchemaSampledFeatureFlags m) where
checkFlag ff = do
flags <- WithSchemaSampledFeatureFlags $ ask
pure $ sampledCheckFlag flags ff
sampledCheckFlag :: SchemaSampledFeatureFlags -> FF.FeatureFlag -> Bool
sampledCheckFlag (SchemaSampledFeatureFlags flags) ff = fromMaybe False (lookup ff flags)
-- | The kind of schema we're building, and its associated options.
data SchemaKind
= HasuraSchema
| RelaySchema NodeInterfaceParserBuilder
isHasuraSchema :: SchemaKind -> Bool
isHasuraSchema = \case
HasuraSchema -> True
RelaySchema _ -> False
-- | The set of common constraints required to build the schema.
type MonadBuildSchemaBase m n =
( MonadError QErr m,
P.MonadMemoize m,
P.MonadParse n
)
-- | How a remote relationship field should be processed when building a
-- schema. Injecting this function from the top level avoids having to know how
-- to do top-level dispatch from deep within the schema code.
--
-- Note: the inner function type uses an existential qualifier: it is expected
-- that the given function will work for _any_ monad @m@ that has the relevant
-- constraints. This prevents us from passing a function that is specfic to the
-- monad in which the schema construction will run, but avoids having to
-- propagate type annotations to each call site.
newtype RemoteRelationshipParserBuilder
= RemoteRelationshipParserBuilder
( forall lhsJoinField r n m.
(MonadBuildSchemaBase m n) =>
RemoteFieldInfo lhsJoinField ->
SchemaT r m (Maybe [P.FieldParser n (IR.RemoteRelationshipField IR.UnpreparedValue)])
)
-- | A 'RemoteRelationshipParserBuilder' that ignores the field altogether, that can
-- be used in tests or to build a source or remote schema in isolation.
ignoreRemoteRelationship :: RemoteRelationshipParserBuilder
ignoreRemoteRelationship = RemoteRelationshipParserBuilder $ const $ pure Nothing
-- | How to build the 'Relay' node.
--
-- Similarly to what we do for remote relationships, we pass in the context the
-- builder function required to build the 'Node' interface, in order to avoid
-- the cross-sources cycles it creates otherwise.
newtype NodeInterfaceParserBuilder = NodeInterfaceParserBuilder
{ runNodeBuilder ::
( forall m n.
(MonadBuildSchemaBase m n) =>
SchemaContext ->
SchemaOptions ->
m (P.Parser 'P.Output n NodeMap)
)
}
-- TODO: move this to Prelude?
retrieve ::
(MonadReader r m, Has a r) =>
(a -> b) ->
m b
retrieve f = asks $ f . getter
-------------------------------------------------------------------------------
{- Note [SchemaT and stacking]
The schema is explicitly built in `SchemaT`, rather than in an arbitrary monad
`m` that happens to have the desired properties (`MonadReader`, `MonadMemoize`,
`MonadError`, and so on). The main reason why we do this is that we want to
avoid a specific performance issue that arises out of two specific constraints:
- we want to build each part of the schema (such as sources and remote
schemas) with its own dedicated minimal reader context (i.e. not using a
shared reader context that is the union of all the information required);
- we want to be able to process remote-relationships, which means "altering"
the reader context when jumping from one "part" of the schema to another.
What that means, in practice, is that we have to call `runReaderT` (or an
equivalent) every time we build a part of the schema (at the root level or as
part of a remote relationship) so that the part we build has access to its
context. When processing a remote relationship, the calling code is *already* in
a monad stack that contains a `ReaderT`, since we were processing a given part
of the schema. If we directly call `runReaderT` to process the RHS of the remote
relationship, we implicitly make it so that the monad stack of the LHS is the
base underneath the `ReaderT` of the RHS; in other terms, we stack another
reader on top of the existing monad stack.
As the schema is built in a "depth-first" way, in a complicated schema with a
lot of remote relationships we would end up with several readers stacked upon
one another. A manually run benchmark showed that this could significantly
impact performance in complicated schemas. We do now have a benchmark set to
replicate this specific case (see the "deep_schema" benchmark set for more
information).
To prevent this stacking, we need to be able to "bring back" the result of the
`runReaderT` back into the calling monad, rather than defaulting to having the
calling monad be the base of the reader. The simplest way of doing this is to
enforce that we are always building the schema in a monad stack that has the
reader on top of some arbitrary *shared* base. This gives us the guarantee that
the LHS of any remote relationship, the calling context for `runReaderT`, is
itself a `ReaderT` on top og that known shared base, meaning that after a call
to `runReaderT` on another part of the schema, we can always go back to the
calling monad with a simple `lift`, as demonstrated in
'remoteRelationshipField'.
-}
-- | The monad in which the schema is built.
--
-- The implementation of 'SchemaT' is intended to be opaque: running a
-- computation in 'SchemaT' is intended to be done via calls to
-- 'runSourceSchema' and 'runRemoteSchema', which also enforce what the @r@
-- parameter should be in each case.
--
-- The reason why we want to enforce that the schema is built in a reader on top
-- of an arbitrary base monad is for performance: see Note [SchemaT and
-- stacking] for more information.
--
-- In the future, we might monomorphize this further to make `MemoizeT` explicit.
newtype SchemaT r m a = SchemaT {runSchemaT :: ReaderT r m a}
deriving newtype (Functor, Applicative, Monad, MonadReader r, P.MonadMemoize, MonadTrans, MonadError e)
instance (Has SchemaContext r, Monad m) => FF.HasFeatureFlagChecker (SchemaT r m) where
checkFlag ff = do
flags <- asks (scSampledFeatureFlags . getter)
pure $ sampledCheckFlag flags ff
type MonadBuildSourceSchema b r m n =
( MonadBuildSchemaBase m n,
Has SchemaContext r,
Has SchemaOptions r,
Has (SourceInfo b) r
)
-- | Runs a schema-building computation with all the context required to build a source.
runSourceSchema ::
forall b m a.
SchemaContext ->
SchemaOptions ->
SourceInfo b ->
SchemaT
( SchemaContext,
SchemaOptions,
SourceInfo b
)
m
a ->
m a
runSourceSchema context options sourceInfo (SchemaT action) = runReaderT action (context, options, sourceInfo)
type MonadBuildRemoteSchema r m n =
( MonadBuildSchemaBase m n,
Has SchemaContext r,
Has Options.RemoteNullForwardingPolicy r,
Has CustomizeRemoteFieldName r,
Has MkTypename r
)
-- | Runs a schema-building computation with all the context required to build a remote schema.
runRemoteSchema ::
SchemaContext ->
Options.RemoteNullForwardingPolicy ->
SchemaT
( SchemaContext,
Options.RemoteNullForwardingPolicy,
MkTypename,
CustomizeRemoteFieldName
)
m
a ->
m a
runRemoteSchema context nullForwarding (SchemaT action) = runReaderT action (context, nullForwarding, mempty, mempty)
type MonadBuildActionSchema r m n =
( MonadBuildSchemaBase m n,
Has SchemaContext r,
Has SchemaOptions r
)
-- | Runs a schema-building computation with all the context required to build actions.
runActionSchema ::
SchemaContext ->
SchemaOptions ->
SchemaT
( SchemaContext,
SchemaOptions
)
m
a ->
m a
runActionSchema context options (SchemaT action) = runReaderT action (context, options)
-------------------------------------------------------------------------------
type SelectExp b = IR.AnnSimpleSelectG b (IR.RemoteRelationshipField IR.UnpreparedValue) (IR.UnpreparedValue b)
type StreamSelectExp b = IR.AnnSimpleStreamSelectG b (IR.RemoteRelationshipField IR.UnpreparedValue) (IR.UnpreparedValue b)
type AggSelectExp b = IR.AnnAggregateSelectG b (IR.RemoteRelationshipField IR.UnpreparedValue) (IR.UnpreparedValue b)
type ConnectionSelectExp b = IR.ConnectionSelect b (IR.RemoteRelationshipField IR.UnpreparedValue) (IR.UnpreparedValue b)
type SelectArgs b = IR.SelectArgsG b (IR.UnpreparedValue b)
type SelectStreamArgs b = IR.SelectStreamArgsG b (IR.UnpreparedValue b)
type TablePerms b = IR.TablePermG b (IR.UnpreparedValue b)
type AnnotatedFields b = IR.AnnFieldsG b (IR.RemoteRelationshipField IR.UnpreparedValue) (IR.UnpreparedValue b)
type AnnotatedField b = IR.AnnFieldG b (IR.RemoteRelationshipField IR.UnpreparedValue) (IR.UnpreparedValue b)
type ConnectionFields b = IR.ConnectionFields b (IR.RemoteRelationshipField IR.UnpreparedValue) (IR.UnpreparedValue b)
type EdgeFields b = IR.EdgeFields b (IR.RemoteRelationshipField IR.UnpreparedValue) (IR.UnpreparedValue b)
type AnnotatedActionFields = IR.ActionFieldsG (IR.RemoteRelationshipField IR.UnpreparedValue)
type AnnotatedActionField = IR.ActionFieldG (IR.RemoteRelationshipField IR.UnpreparedValue)
type AnnotatedNestedObjectSelect b = IR.AnnNestedObjectSelectG b (IR.RemoteRelationshipField IR.UnpreparedValue) (IR.UnpreparedValue b)
type AnnotatedNestedArraySelect b = IR.AnnNestedArraySelectG b (IR.RemoteRelationshipField IR.UnpreparedValue) (IR.UnpreparedValue b)
-------------------------------------------------------------------------------
data RemoteSchemaParser n = RemoteSchemaParser
{ piQuery :: [P.FieldParser n (NamespacedField (IR.RemoteSchemaRootField (IR.RemoteRelationshipField IR.UnpreparedValue) RemoteSchemaVariable))],
piMutation :: Maybe [P.FieldParser n (NamespacedField (IR.RemoteSchemaRootField (IR.RemoteRelationshipField IR.UnpreparedValue) RemoteSchemaVariable))],
piSubscription :: Maybe [P.FieldParser n (NamespacedField (IR.RemoteSchemaRootField (IR.RemoteRelationshipField IR.UnpreparedValue) RemoteSchemaVariable))]
}
getTableRoles :: BackendSourceInfo -> [RoleName]
getTableRoles bsi = AB.dispatchAnyBackend @Backend bsi go
where
go si = HashMap.keys . _tiRolePermInfoMap =<< HashMap.elems (_siTables si)
getLogicalModelRoles :: BackendSourceInfo -> [RoleName]
getLogicalModelRoles bsi = AB.dispatchAnyBackend @Backend bsi go
where
go si =
let namedLogicalModelRoles = HashMap.keys . _lmiPermissions =<< HashMap.elems (_siLogicalModels si)
inlineLogicalModelRoles = HashMap.keys . _lmiPermissions . _nqiReturns =<< HashMap.elems (_siNativeQueries si)
in namedLogicalModelRoles <> inlineLogicalModelRoles
askScalarTypeParsingContext ::
forall b r m.
(MonadReader r m, Has (SourceInfo b) r, Has (ScalarTypeParsingContext b) (SourceConfig b)) =>
m (ScalarTypeParsingContext b)
askScalarTypeParsingContext = asks (getter . _siConfiguration @b . getter)
-- | Looks up table information for the given table name. This function
-- should never fail, since the schema cache construction process is
-- supposed to ensure all dependencies are resolved.
-- TODO: deduplicate this with `CacheRM`.
askTableInfo ::
forall b r m.
(Backend b, MonadError QErr m, MonadReader r m, Has (SourceInfo b) r) =>
TableName b ->
m (TableInfo b)
askTableInfo tableName = do
SourceInfo {..} <- asks getter
HashMap.lookup tableName _siTables
`onNothing` throw500 ("askTableInfo: no info for table " <> dquote tableName <> " in source " <> dquote _siName)
-- | Looks up logical model information for the given logical model name. This function
-- should never fail, since the schema cache construction process is
-- supposed to ensure all dependencies are resolved.
-- TODO: deduplicate this with `CacheRM`.
askLogicalModelInfo ::
forall b r m.
(MonadError QErr m, MonadReader r m, Has (SourceInfo b) r) =>
LogicalModelName ->
m (LogicalModelInfo b)
askLogicalModelInfo logicalModelName = do
SourceInfo {..} <- asks getter
HashMap.lookup logicalModelName _siLogicalModels
`onNothing` throw500 ("askLogicalModelInfo: no info for logical model " <> dquote logicalModelName <> " in source " <> dquote _siName)
-- | Looks up native query information for the given native query name. This function
-- should never fail, since the schema cache construction process is
-- supposed to ensure all dependencies are resolved.
-- TODO: deduplicate this with `CacheRM`.
askNativeQueryInfo ::
forall b r m.
(MonadError QErr m, MonadReader r m, Has (SourceInfo b) r) =>
NativeQueryName ->
m (NativeQueryInfo b)
askNativeQueryInfo nativeQueryName = do
SourceInfo {..} <- asks getter
HashMap.lookup nativeQueryName _siNativeQueries
`onNothing` throw500 ("askNativeQueryInfo: no info for native query " <> dquote nativeQueryName <> " in source " <> dquote _siName)
-- | Whether the request is sent with `x-hasura-use-backend-only-permissions` set to `true`.
data Scenario = Backend | Frontend deriving (Enum, Show, Eq)
textToName :: (MonadError QErr m) => Text -> m G.Name
textToName textName =
G.mkName textName
`onNothing` throw400
ValidationFailed
( "cannot include "
<> textName
<<> " in the GraphQL schema because "
<> " it is not a valid GraphQL identifier"
)
textToGQLIdentifier :: (MonadError QErr m) => Text -> m GQLNameIdentifier
textToGQLIdentifier textName = do
let gqlIdents = do
(pref, suffs) <- uncons (C.fromSnake textName)
prefName <- G.mkName pref
suffNames <- traverse G.mkNameSuffix suffs
pure $ C.fromAutogeneratedTuple (prefName, suffNames)
gqlIdents
`onNothing` throw400
ValidationFailed
( "cannot include "
<> textName
<<> " in the GraphQL schema because "
<> " it is not a valid GraphQL identifier"
)
partialSQLExpToUnpreparedValue :: PartialSQLExp b -> IR.UnpreparedValue b
partialSQLExpToUnpreparedValue (PSESessVar pftype var) = IR.UVSessionVar pftype var
partialSQLExpToUnpreparedValue PSESession = IR.UVSession
partialSQLExpToUnpreparedValue (PSESQLExp sqlExp) = IR.UVLiteral sqlExp
getRedactionExprForColumn :: (Backend b) => SelPermInfo b -> Column b -> Maybe (IR.AnnRedactionExpUnpreparedValue b)
getRedactionExprForColumn selectPermissions columnName =
let redactionExp = HashMap.lookup columnName (spiCols selectPermissions)
in fmap partialSQLExpToUnpreparedValue <$> redactionExp
getRedactionExprForComputedField :: (Backend b) => SelPermInfo b -> ComputedFieldName -> Maybe (IR.AnnRedactionExpUnpreparedValue b)
getRedactionExprForComputedField selectPermissions cfName =
let redactionExp = HashMap.lookup cfName (spiComputedFields selectPermissions)
in fmap partialSQLExpToUnpreparedValue <$> redactionExp
mapField ::
(Functor m) =>
P.InputFieldsParser m (Maybe a) ->
(a -> b) ->
P.InputFieldsParser m (Maybe b)
mapField fp f = fmap (fmap f) fp
parsedSelectionsToFields ::
-- | how to handle @__typename@ fields
(Text -> a) ->
InsOrdHashMap.InsOrdHashMap G.Name (P.ParsedSelection a) ->
Fields a
parsedSelectionsToFields mkTypenameFromText =
InsOrdHashMap.toList
>>> map (FieldName . G.unName *** P.handleTypename (mkTypenameFromText . G.unName))
numericAggOperators :: [C.GQLNameIdentifier]
numericAggOperators =
[ C.fromAutogeneratedName $$(G.litName "sum"),
C.fromAutogeneratedName $$(G.litName "avg"),
C.fromAutogeneratedName $$(G.litName "stddev"),
C.fromAutogeneratedTuple $$(G.litGQLIdentifier ["stddev", "samp"]),
C.fromAutogeneratedTuple $$(G.litGQLIdentifier ["stddev", "pop"]),
C.fromAutogeneratedName $$(G.litName "variance"),
C.fromAutogeneratedTuple $$(G.litGQLIdentifier ["var", "samp"]),
C.fromAutogeneratedTuple $$(G.litGQLIdentifier ["var", "pop"])
]
comparisonAggOperators :: [C.GQLNameIdentifier]
comparisonAggOperators =
[ C.fromAutogeneratedName $$(G.litName "max"),
C.fromAutogeneratedName $$(G.litName "min")
]
mkDescriptionWith :: Maybe Postgres.PGDescription -> Text -> G.Description
mkDescriptionWith descM defaultTxt = G.Description $ case descM of
Nothing -> defaultTxt
Just (Postgres.PGDescription descTxt) -> T.unlines [descTxt, "\n", defaultTxt]
-- TODO why do we do these validations at this point? What does it mean to track
-- a function but not add it to the schema...?
-- Auke:
-- I believe the intention is simply to allow the console to do postgres data management
-- Karthikeyan: Yes, this is correct. We allowed this pre PDV but somehow
-- got removed in PDV. OTOH, I’m not sure how prevalent this feature
-- actually is
takeValidTables :: forall b. (Backend b) => TableCache b -> TableCache b
takeValidTables = HashMap.filterWithKey graphQLTableFilter
where
graphQLTableFilter tableName tableInfo =
-- either the table name should be GraphQL compliant
-- or it should have a GraphQL custom name set with it
isRight (tableGraphQLName @b tableName)
|| isJust (_tcCustomName $ _tciCustomConfig $ _tiCoreInfo tableInfo)
-- TODO and what about graphql-compliant function names here too?
takeValidFunctions :: forall b. FunctionCache b -> FunctionCache b
takeValidFunctions = HashMap.filter functionFilter
where
functionFilter = not . isSystemDefined . _fiSystemDefined
-- | @TODO: Currently we do no validation on native queries in schema. Should we?
takeValidNativeQueries :: forall b. NativeQueryCache b -> NativeQueryCache b
takeValidNativeQueries = id
-- | @TODO: Currently we do no validation on stored procedures in schema. Should we?
takeValidStoredProcedures :: forall b. StoredProcedureCache b -> StoredProcedureCache b
takeValidStoredProcedures = id
-- root field builder helpers
requiredFieldParser ::
(Functor n, Functor m) =>
(a -> b) ->
m (P.FieldParser n a) ->
m (Maybe (P.FieldParser n b))
requiredFieldParser f = fmap $ Just . fmap f
optionalFieldParser ::
(Functor n, Functor m) =>
(a -> b) ->
m (Maybe (P.FieldParser n a)) ->
m (Maybe (P.FieldParser n b))
optionalFieldParser = fmap . fmap . fmap
-- | Builds the type name for referenced enum tables.
mkEnumTypeName :: forall b r m. (Backend b, MonadError QErr m, Has (SourceInfo b) r) => TableName b -> Maybe G.Name -> SchemaT r m G.Name
mkEnumTypeName enumTableName enumTableCustomName = do
customization <- retrieve $ _siCustomization @b
enumTableGQLName <- getTableIdentifier @b enumTableName `onLeft` throwError
pure $ addEnumSuffix customization enumTableGQLName enumTableCustomName
addEnumSuffix :: ResolvedSourceCustomization -> GQLNameIdentifier -> Maybe G.Name -> G.Name
addEnumSuffix customization enumTableGQLName enumTableCustomName =
runMkTypename (_rscTypeNames customization)
$ applyTypeNameCaseIdentifier (_rscNamingConvention customization)
$ mkEnumTableTypeName enumTableGQLName enumTableCustomName
-- TODO: figure out what the purpose of this method is.
peelWithOrigin :: (P.MonadParse m) => P.Parser 'P.Both m a -> P.Parser 'P.Both m (IR.ValueWithOrigin a)
peelWithOrigin parser =
parser
{ P.pParser = \case
P.GraphQLValue (G.VVariable var@P.Variable {vInfo, vValue}) -> do
-- Check types c.f. 5.8.5 of the June 2018 GraphQL spec
P.typeCheck False (P.toGraphQLType $ P.pType parser) var
fmap (IR.ValueWithOrigin vInfo)
$ P.pParser parser
$ case vValue of
-- TODO: why is faking a null value here semantically correct? RE: GraphQL spec June 2018, section 2.9.5
Nothing -> P.GraphQLValue G.VNull
Just val -> absurd <$> val
value -> IR.ValueNoOrigin <$> P.pParser parser value
}
getIntrospectionResult :: Options.RemoteSchemaPermissions -> RoleName -> RemoteSchemaCtxG remoteFieldInfo -> Maybe IntrospectionResult
getIntrospectionResult remoteSchemaPermsCtx role remoteSchemaContext =
if
| -- admin doesn't have a custom annotated introspection, defaulting to the original one
role == adminRoleName ->
pure $ _rscIntroOriginal remoteSchemaContext
| -- if permissions are disabled, the role map will be empty, defaulting to the original one
remoteSchemaPermsCtx == Options.DisableRemoteSchemaPermissions ->
pure $ _rscIntroOriginal remoteSchemaContext
| -- otherwise, look the role up in the map; if we find nothing, then the role doesn't have access
otherwise ->
HashMap.lookup role (_rscPermissions remoteSchemaContext)
tablePermissionsInfo :: (Backend b) => SelPermInfo b -> TablePerms b
tablePermissionsInfo selectPermissions =
IR.TablePerm
{ IR._tpFilter = fmap partialSQLExpToUnpreparedValue <$> spiFilter selectPermissions,
IR._tpLimit = spiLimit selectPermissions
}