Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module provides building blocks for the GraphQL Schema that the GraphQL Engine presents.
The functions defined here are used to serve as default implementations for
their namesakes in the BackendSchema
type class.
When, for some backend, you want to implement a new feature that manifests itself visibly in the schema (e.g., if you're developing support for update mutations), this module is likely where your efforts should start.
Using these functions help us present a consistent GraphQL schema across different backends.
There is a bit of tension however, as sometimes we intentionally do want the GraphQL Schema relating to some backend to be different in some way.
It could be that a backend only has limited support for some common feature,
or, more interestingly, that some backend just does things differently (c.f.
MSSQL's MERGE
statement with PostgreSQL's INSERT .. ON CONFLICT
, which
are similar enough that we want to use the same overall upsert schema but
different enough that we want to use different field names)
When you want to implement new schema for a backend, there is overall three different ways do deal with this tension:
- You can duplicate existing code and implement the new behavior in the duplicate.
- You can infuse the new behavior into existing code and switch dynamically at runtime (or via type class instance dispatch, which is the same for our purposes)
- You can refactor the existing building blocks and compose them differently at use sites to get the desired behavior nuances.
Of these three, steps 1. and 2. are by far the easiest to execute, while 3. requires some critical thought. However, both 1. and 2. produce legacy code that is difficult to maintain and understand.
As a guideline, if you find yourself wanting add new behavior to some of these functions it's very likely that you should consider refactoring them instead, thus shifting the responsibility deciding on the correct behavior to use sites.
It an ongoing effort to adapt and refactor these building blocks such that they have the sizes and shapes that result in the most elegant uses of them that we can manage.
Synopsis
- setFieldNameCase :: NamingCase -> TableInfo b -> CustomRootField -> (GQLNameIdentifier -> GQLNameIdentifier) -> GQLNameIdentifier -> Name
- buildTableQueryAndSubscriptionFields :: forall b r m n. (MonadBuildSchema b r m n, AggregationPredicatesSchema b, BackendTableSelectSchema b) => MkRootFieldName -> SourceInfo b -> TableName b -> TableInfo b -> GQLNameIdentifier -> m ([FieldParser n (QueryDB b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))], [FieldParser n (QueryDB b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))], Maybe (Name, Parser 'Output n (ApolloFederationParserFunction n)))
- buildTableStreamingSubscriptionFields :: forall b r m n. (MonadBuildSchema b r m n, AggregationPredicatesSchema b, BackendTableSelectSchema b) => MkRootFieldName -> SourceInfo b -> TableName b -> TableInfo b -> GQLNameIdentifier -> m [FieldParser n (QueryDB b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))]
- buildTableInsertMutationFields :: forall b r m n. (MonadBuildSchema b r m n, BackendTableSelectSchema b) => (SourceInfo b -> TableInfo b -> m (InputFieldsParser n (BackendInsert b (UnpreparedValue b)))) -> MkRootFieldName -> Scenario -> SourceInfo b -> TableName b -> TableInfo b -> GQLNameIdentifier -> m [FieldParser n (AnnotatedInsert b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))]
- buildTableUpdateMutationFields :: forall b r m n. (MonadBuildSchema b r m n, AggregationPredicatesSchema b, BackendTableSelectSchema b) => (TableInfo b -> m (InputFieldsParser n (BackendUpdate b (UnpreparedValue b)))) -> MkRootFieldName -> Scenario -> SourceInfo b -> TableName b -> TableInfo b -> GQLNameIdentifier -> m [FieldParser n (AnnotatedUpdateG b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))]
- buildTableDeleteMutationFields :: forall b r m n. (MonadBuildSchema b r m n, AggregationPredicatesSchema b, BackendTableSelectSchema b) => MkRootFieldName -> Scenario -> SourceInfo b -> TableName b -> TableInfo b -> GQLNameIdentifier -> m [FieldParser n (AnnDelG b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))]
- buildFieldDescription :: Text -> Comment -> Maybe Description
Documentation
setFieldNameCase :: NamingCase -> TableInfo b -> CustomRootField -> (GQLNameIdentifier -> GQLNameIdentifier) -> GQLNameIdentifier -> Name Source #
Builds field name with proper case. Please note that this is a pure
function as all the validation has already been done while preparing
GQLNameIdentifier
.
buildTableQueryAndSubscriptionFields :: forall b r m n. (MonadBuildSchema b r m n, AggregationPredicatesSchema b, BackendTableSelectSchema b) => MkRootFieldName -> SourceInfo b -> TableName b -> TableInfo b -> GQLNameIdentifier -> m ([FieldParser n (QueryDB b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))], [FieldParser n (QueryDB b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))], Maybe (Name, Parser 'Output n (ApolloFederationParserFunction n))) Source #
buildTableQueryAndSubscriptionFields builds the field parsers of a table. It returns a tuple with array of field parsers that correspond to the field parsers of the query root and the field parsers of the subscription root
buildTableStreamingSubscriptionFields :: forall b r m n. (MonadBuildSchema b r m n, AggregationPredicatesSchema b, BackendTableSelectSchema b) => MkRootFieldName -> SourceInfo b -> TableName b -> TableInfo b -> GQLNameIdentifier -> m [FieldParser n (QueryDB b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))] Source #
buildTableInsertMutationFields :: forall b r m n. (MonadBuildSchema b r m n, BackendTableSelectSchema b) => (SourceInfo b -> TableInfo b -> m (InputFieldsParser n (BackendInsert b (UnpreparedValue b)))) -> MkRootFieldName -> Scenario -> SourceInfo b -> TableName b -> TableInfo b -> GQLNameIdentifier -> m [FieldParser n (AnnotatedInsert b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))] Source #
buildTableUpdateMutationFields Source #
:: forall b r m n. (MonadBuildSchema b r m n, AggregationPredicatesSchema b, BackendTableSelectSchema b) | |
=> (TableInfo b -> m (InputFieldsParser n (BackendUpdate b (UnpreparedValue b)))) | an action that builds |
-> MkRootFieldName | |
-> Scenario | |
-> SourceInfo b | The source that the table lives in |
-> TableName b | The name of the table being acted on |
-> TableInfo b | table info |
-> GQLNameIdentifier | field display name |
-> m [FieldParser n (AnnotatedUpdateG b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))] |
This function is the basic building block for update mutations. It
implements the mutation schema in the general shape described in
https://hasura.io/docs/latest/graphql/core/databases/postgres/mutations/update.html
.
Something that varies between backends is the update operators
that they
support (i.e. the schema fields _set
, _inc
, etc., see
Hasura.Backends.Postgres.Instances.Schema.updateOperators for an example
implementation). Therefore, this function is parameterised over a monadic
action that produces the operators that the backend supports in the context
of some table and associated update permissions.
Apart from this detail, the rest of the arguments are the same as those
of BackendSchema.
buildTableUpdateMutationFields
.
The suggested way to use this is like:
instance BackendSchema MyBackend where ... buildTableUpdateMutationFields = GSB.buildTableUpdateMutationFields myBackendUpdateOperators ...
buildTableDeleteMutationFields :: forall b r m n. (MonadBuildSchema b r m n, AggregationPredicatesSchema b, BackendTableSelectSchema b) => MkRootFieldName -> Scenario -> SourceInfo b -> TableName b -> TableInfo b -> GQLNameIdentifier -> m [FieldParser n (AnnDelG b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))] Source #
buildFieldDescription :: Text -> Comment -> Maybe Description Source #