2022-05-26 17:05:13 +03:00
|
|
|
-- | Internal helper module. Some things re-exported by
|
|
|
|
-- 'Test.Parser.Expectation'.
|
|
|
|
module Test.Parser.Internal
|
|
|
|
( mkTable,
|
|
|
|
ColumnInfoBuilder (..),
|
|
|
|
mkColumnInfo,
|
|
|
|
mkParser,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Data.HashMap.Strict qualified as HM
|
|
|
|
import Data.HashSet qualified as HS
|
|
|
|
import Data.Text.Casing qualified as C
|
|
|
|
import Hasura.Backends.Postgres.Instances.Schema (updateOperators)
|
|
|
|
import Hasura.Backends.Postgres.SQL.Types (QualifiedObject (..), QualifiedTable, TableName (..), unsafePGCol)
|
|
|
|
import Hasura.Backends.Postgres.Types.Update (BackendUpdate (..))
|
|
|
|
import Hasura.GraphQL.Schema.Build qualified as Build
|
2022-05-31 17:41:09 +03:00
|
|
|
import Hasura.GraphQL.Schema.Common (Scenario (Frontend))
|
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.Schema.Parser (FieldParser, InputFieldsParser)
|
2022-05-26 17:05:13 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.IR.BoolExp (AnnBoolExpFld (..), GBoolExp (..), PartialSQLExp (..))
|
|
|
|
import Hasura.RQL.IR.Root (RemoteRelationshipField)
|
|
|
|
import Hasura.RQL.IR.Update (AnnotatedUpdateG (..))
|
2022-05-31 01:07:02 +03:00
|
|
|
import Hasura.RQL.IR.Value (UnpreparedValue (..))
|
2022-05-26 17:05:13 +03:00
|
|
|
import Hasura.RQL.Types.Column (ColumnInfo (..), ColumnMutability (..), ColumnType (..))
|
Remove circular dependency in schema building code
### Description
The main goal of this PR is, as stated, to remove the circular dependency in the schema building code. This cycle arises from the existence of remote relationships: when we build the schema for a source A, a remote relationship might force us to jump to the schema of a source B, or some remote schema. As a result, we end up having to do a dispatch from a "leaf" of the schema, similar to the one done at the root. In turn, this forces us to carry along in the schema a lot of information required for that dispatch, AND it forces us to import the instances in scope, creating an import loop.
As discussed in #4489, this PR implements the "dependency injection" solution: we pass to the schema a function to call to do the dispatch, and to get a generated field for a remote relationship. That way, this function can be chosen at the root level, and the leaves need not be aware of the overall context.
This PR grew a bit bigger than that, however; in an attempt to try and remove the `SourceCache` from the schema altogether, it changed a lot of functions across the schema building code, to thread along the `SourceInfo b` of the source being built. This avoids having to do cache lookups within a given source. A few cases remain, such as relay, that we might try to tackle in a subsequent PR.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4557
GitOrigin-RevId: 9388e48372877520a72a9fd1677005df9f7b2d72
2022-05-27 20:21:22 +03:00
|
|
|
import Hasura.RQL.Types.Common (Comment (..), FieldName (..), SystemDefined (..))
|
2022-05-26 17:05:13 +03:00
|
|
|
import Hasura.RQL.Types.Instances ()
|
2022-06-07 08:32:08 +03:00
|
|
|
import Hasura.RQL.Types.Permission (AllowedRootFields (..))
|
Remove circular dependency in schema building code
### Description
The main goal of this PR is, as stated, to remove the circular dependency in the schema building code. This cycle arises from the existence of remote relationships: when we build the schema for a source A, a remote relationship might force us to jump to the schema of a source B, or some remote schema. As a result, we end up having to do a dispatch from a "leaf" of the schema, similar to the one done at the root. In turn, this forces us to carry along in the schema a lot of information required for that dispatch, AND it forces us to import the instances in scope, creating an import loop.
As discussed in #4489, this PR implements the "dependency injection" solution: we pass to the schema a function to call to do the dispatch, and to get a generated field for a remote relationship. That way, this function can be chosen at the root level, and the leaves need not be aware of the overall context.
This PR grew a bit bigger than that, however; in an attempt to try and remove the `SourceCache` from the schema altogether, it changed a lot of functions across the schema building code, to thread along the `SourceInfo b` of the source being built. This avoids having to do cache lookups within a given source. A few cases remain, such as relay, that we might try to tackle in a subsequent PR.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4557
GitOrigin-RevId: 9388e48372877520a72a9fd1677005df9f7b2d72
2022-05-27 20:21:22 +03:00
|
|
|
import Hasura.RQL.Types.Source (SourceInfo)
|
2022-05-26 17:05:13 +03:00
|
|
|
import Hasura.RQL.Types.Table (CustomRootField (..), FieldInfo (..), RolePermInfo (..), SelPermInfo (..), TableConfig (..), TableCoreInfoG (..), TableCustomRootFields (..), TableInfo (..), UpdPermInfo (..))
|
|
|
|
import Hasura.SQL.Backend (BackendType (Postgres), PostgresKind (Vanilla))
|
|
|
|
import Language.GraphQL.Draft.Syntax (unsafeMkName)
|
|
|
|
import Test.Parser.Monad
|
|
|
|
|
|
|
|
type PG = 'Postgres 'Vanilla
|
|
|
|
|
|
|
|
type Parser = FieldParser ParserTestT (AnnotatedUpdateG PG (RemoteRelationshipField UnpreparedValue) (UnpreparedValue PG))
|
|
|
|
|
|
|
|
-- | Create a table by its name, using the public schema.
|
|
|
|
mkTable :: Text -> QualifiedTable
|
|
|
|
mkTable name =
|
|
|
|
QualifiedObject
|
|
|
|
{ qSchema = "public",
|
|
|
|
qName = TableName name
|
|
|
|
}
|
|
|
|
|
|
|
|
-- | Build a column, see 'mkColumnInfo'.
|
|
|
|
data ColumnInfoBuilder = ColumnInfoBuilder
|
|
|
|
{ -- | name of the column
|
|
|
|
cibName :: Text,
|
|
|
|
-- | Column type, e.g.
|
|
|
|
--
|
|
|
|
-- > ColumnScalar PGText
|
|
|
|
cibType :: ColumnType PG,
|
|
|
|
-- | whether the column is nullable or not
|
|
|
|
cibNullable :: Bool
|
|
|
|
}
|
|
|
|
|
|
|
|
-- | Create a column using the provided 'ColumnInfoBuilder' and defaults.
|
|
|
|
--
|
|
|
|
-- Note that all permissions are enabled by default.
|
|
|
|
mkColumnInfo :: ColumnInfoBuilder -> ColumnInfo PG
|
|
|
|
mkColumnInfo ColumnInfoBuilder {..} =
|
|
|
|
ColumnInfo
|
|
|
|
{ ciColumn = unsafePGCol cibName,
|
|
|
|
ciName = unsafeMkName cibName,
|
|
|
|
ciPosition = 0,
|
|
|
|
ciType = cibType,
|
|
|
|
ciIsNullable = cibNullable,
|
|
|
|
ciDescription = Nothing,
|
|
|
|
ciMutability = columnMutability
|
|
|
|
}
|
|
|
|
where
|
|
|
|
columnMutability :: ColumnMutability
|
|
|
|
columnMutability =
|
|
|
|
ColumnMutability
|
|
|
|
{ _cmIsInsertable = True,
|
|
|
|
_cmIsUpdatable = True
|
|
|
|
}
|
|
|
|
|
|
|
|
-- | Create a parser for the provided table and columns.
|
|
|
|
--
|
|
|
|
-- No special permissions, required headers, filters, etc., are set.
|
|
|
|
--
|
|
|
|
-- This will not work for inserts and deletes (see @rolePermInfo@ below).
|
|
|
|
mkParser :: QualifiedTable -> [ColumnInfoBuilder] -> SchemaTestT [Parser]
|
|
|
|
mkParser table cib =
|
|
|
|
Build.buildTableUpdateMutationFields
|
|
|
|
backendUpdateParser
|
2022-05-31 17:41:09 +03:00
|
|
|
Frontend
|
Remove circular dependency in schema building code
### Description
The main goal of this PR is, as stated, to remove the circular dependency in the schema building code. This cycle arises from the existence of remote relationships: when we build the schema for a source A, a remote relationship might force us to jump to the schema of a source B, or some remote schema. As a result, we end up having to do a dispatch from a "leaf" of the schema, similar to the one done at the root. In turn, this forces us to carry along in the schema a lot of information required for that dispatch, AND it forces us to import the instances in scope, creating an import loop.
As discussed in #4489, this PR implements the "dependency injection" solution: we pass to the schema a function to call to do the dispatch, and to get a generated field for a remote relationship. That way, this function can be chosen at the root level, and the leaves need not be aware of the overall context.
This PR grew a bit bigger than that, however; in an attempt to try and remove the `SourceCache` from the schema altogether, it changed a lot of functions across the schema building code, to thread along the `SourceInfo b` of the source being built. This avoids having to do cache lookups within a given source. A few cases remain, such as relay, that we might try to tackle in a subsequent PR.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4557
GitOrigin-RevId: 9388e48372877520a72a9fd1677005df9f7b2d72
2022-05-27 20:21:22 +03:00
|
|
|
sourceInfo
|
2022-05-26 17:05:13 +03:00
|
|
|
table
|
|
|
|
tableInfo
|
|
|
|
name
|
|
|
|
where
|
|
|
|
backendUpdateParser ::
|
|
|
|
TableInfo PG ->
|
|
|
|
SchemaTestT (InputFieldsParser ParserTestT (BackendUpdate (UnpreparedValue PG)))
|
|
|
|
backendUpdateParser ti =
|
|
|
|
fmap BackendUpdate <$> updateOperators ti updPermInfo
|
|
|
|
|
|
|
|
updPermInfo :: UpdPermInfo PG
|
|
|
|
updPermInfo =
|
|
|
|
UpdPermInfo
|
|
|
|
{ upiCols = HS.fromList . fmap (unsafePGCol . cibName) $ cib,
|
|
|
|
upiTable = table,
|
|
|
|
upiFilter = upiFilter,
|
|
|
|
upiCheck = Nothing,
|
|
|
|
upiSet = mempty,
|
2022-05-31 17:41:09 +03:00
|
|
|
upiBackendOnly = False,
|
2022-05-26 17:05:13 +03:00
|
|
|
upiRequiredHeaders = mempty
|
|
|
|
}
|
|
|
|
|
|
|
|
columnInfos :: [ColumnInfo PG]
|
|
|
|
columnInfos = mkColumnInfo <$> cib
|
|
|
|
|
|
|
|
upiFilter :: GBoolExp PG (AnnBoolExpFld PG (PartialSQLExp PG))
|
|
|
|
upiFilter = BoolAnd $ fmap (\ci -> BoolFld $ AVColumn ci []) columnInfos
|
|
|
|
|
|
|
|
------------------------------------------
|
Remove circular dependency in schema building code
### Description
The main goal of this PR is, as stated, to remove the circular dependency in the schema building code. This cycle arises from the existence of remote relationships: when we build the schema for a source A, a remote relationship might force us to jump to the schema of a source B, or some remote schema. As a result, we end up having to do a dispatch from a "leaf" of the schema, similar to the one done at the root. In turn, this forces us to carry along in the schema a lot of information required for that dispatch, AND it forces us to import the instances in scope, creating an import loop.
As discussed in #4489, this PR implements the "dependency injection" solution: we pass to the schema a function to call to do the dispatch, and to get a generated field for a remote relationship. That way, this function can be chosen at the root level, and the leaves need not be aware of the overall context.
This PR grew a bit bigger than that, however; in an attempt to try and remove the `SourceCache` from the schema altogether, it changed a lot of functions across the schema building code, to thread along the `SourceInfo b` of the source being built. This avoids having to do cache lookups within a given source. A few cases remain, such as relay, that we might try to tackle in a subsequent PR.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4557
GitOrigin-RevId: 9388e48372877520a72a9fd1677005df9f7b2d72
2022-05-27 20:21:22 +03:00
|
|
|
sourceInfo :: SourceInfo PG
|
|
|
|
sourceInfo = undefined
|
2022-05-26 17:05:13 +03:00
|
|
|
|
|
|
|
------------------------------------------
|
|
|
|
tableInfo :: TableInfo PG
|
|
|
|
tableInfo =
|
|
|
|
TableInfo
|
|
|
|
{ _tiCoreInfo = tableCoreInfo,
|
|
|
|
_tiRolePermInfoMap = mempty,
|
|
|
|
_tiEventTriggerInfoMap = mempty,
|
|
|
|
_tiAdminRolePermInfo = rolePermInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
tableCoreInfo :: TableCoreInfoG PG (FieldInfo PG) (ColumnInfo PG)
|
|
|
|
tableCoreInfo =
|
|
|
|
TableCoreInfo
|
|
|
|
{ _tciName = table,
|
|
|
|
_tciDescription = Nothing,
|
|
|
|
_tciSystemDefined = SystemDefined False,
|
|
|
|
_tciFieldInfoMap = fieldInfoMap,
|
|
|
|
_tciPrimaryKey = Nothing,
|
|
|
|
_tciUniqueConstraints = mempty,
|
|
|
|
_tciForeignKeys = mempty,
|
|
|
|
_tciViewInfo = Nothing,
|
|
|
|
_tciEnumValues = Nothing,
|
|
|
|
_tciCustomConfig = tableConfig,
|
|
|
|
_tciExtraTableMetadata = ()
|
|
|
|
}
|
|
|
|
|
|
|
|
rolePermInfo :: RolePermInfo PG
|
|
|
|
rolePermInfo =
|
|
|
|
RolePermInfo
|
|
|
|
{ _permIns = Nothing,
|
|
|
|
_permSel = Just selPermInfo,
|
|
|
|
_permUpd = Just updPermInfo,
|
|
|
|
_permDel = Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldInfoMap :: HM.HashMap FieldName (FieldInfo PG)
|
|
|
|
fieldInfoMap =
|
|
|
|
HM.fromList
|
|
|
|
. fmap toHashPair
|
|
|
|
$ cib
|
|
|
|
|
|
|
|
tableConfig :: TableConfig PG
|
|
|
|
tableConfig =
|
|
|
|
TableConfig
|
|
|
|
{ _tcCustomRootFields = tableCustomRootFields,
|
|
|
|
_tcColumnConfig = mempty,
|
|
|
|
_tcCustomName = Nothing,
|
|
|
|
_tcComment = Automatic
|
|
|
|
}
|
|
|
|
|
|
|
|
selPermInfo :: SelPermInfo PG
|
|
|
|
selPermInfo =
|
|
|
|
SelPermInfo
|
|
|
|
{ spiCols = HM.fromList . fmap ((,Nothing) . unsafePGCol . cibName) $ cib,
|
2022-06-06 10:23:00 +03:00
|
|
|
spiComputedFields = mempty,
|
2022-05-26 17:05:13 +03:00
|
|
|
spiFilter = upiFilter,
|
|
|
|
spiLimit = Nothing,
|
|
|
|
spiAllowAgg = True,
|
2022-06-07 08:32:08 +03:00
|
|
|
spiRequiredHeaders = mempty,
|
|
|
|
spiAllowedQueryRootFields = ARFAllowAllRootFields,
|
|
|
|
spiAllowedSubscriptionRootFields = ARFAllowAllRootFields
|
2022-05-26 17:05:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tableCustomRootFields :: TableCustomRootFields
|
|
|
|
tableCustomRootFields =
|
|
|
|
TableCustomRootFields
|
|
|
|
{ _tcrfSelect = customRootField,
|
|
|
|
_tcrfSelectByPk = customRootField,
|
|
|
|
_tcrfSelectAggregate = customRootField,
|
|
|
|
_tcrfInsert = customRootField,
|
|
|
|
_tcrfInsertOne = customRootField,
|
|
|
|
_tcrfUpdate = customRootField,
|
|
|
|
_tcrfUpdateByPk = customRootField,
|
|
|
|
_tcrfDelete = customRootField,
|
|
|
|
_tcrfDeleteByPk = customRootField
|
|
|
|
}
|
|
|
|
|
|
|
|
customRootField :: CustomRootField
|
|
|
|
customRootField =
|
|
|
|
CustomRootField
|
|
|
|
{ _crfName = Nothing,
|
|
|
|
_crfComment = Automatic
|
|
|
|
}
|
|
|
|
------------------------------------------
|
|
|
|
name :: C.GQLNameIdentifier
|
|
|
|
name = C.fromName $ unsafeMkName "test"
|
|
|
|
|
|
|
|
toHashPair :: ColumnInfoBuilder -> (FieldName, FieldInfo PG)
|
|
|
|
toHashPair cib = (coerce $ cibName cib, FIColumn $ mkColumnInfo cib)
|