graphql-engine/server/src-lib/Hasura/RemoteSchema/Metadata/Permission.hs
Jesse Hallett c265e303f6 server: codecs for remote schemas metadata
These codecs should fully cover the `remote_schemas` property of the Metadata type.

Ticket: [GDC-522](https://hasurahq.atlassian.net/browse/GDC-522)

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6812
GitOrigin-RevId: 1b256f6829486295957c232b92ff184bd9a86469
2022-12-15 17:39:22 +00:00

62 lines
2.1 KiB
Haskell

{-# LANGUAGE TemplateHaskell #-}
module Hasura.RemoteSchema.Metadata.Permission
( RemoteSchemaPermissionDefinition (..),
RemoteSchemaPermissionMetadata (..),
)
where
import Autodocodec (HasCodec (codec), object, optionalField', requiredField', requiredFieldWith, (.=))
import Autodocodec.Extended (graphQLSchemaDocumentCodec)
import Data.Aeson qualified as J
import Data.Aeson.TH qualified as J
import Hasura.Prelude
import Hasura.Session
import Language.GraphQL.Draft.Printer qualified as G
import Language.GraphQL.Draft.Syntax qualified as G
import Text.Builder qualified as TB
newtype RemoteSchemaPermissionDefinition = RemoteSchemaPermissionDefinition
{ _rspdSchema :: G.SchemaDocument
}
deriving (Show, Eq, Generic)
instance NFData RemoteSchemaPermissionDefinition
instance Hashable RemoteSchemaPermissionDefinition
instance HasCodec RemoteSchemaPermissionDefinition where
codec =
object "RemoteSchemaPermissionDefinition" $
RemoteSchemaPermissionDefinition
<$> requiredFieldWith
"schema"
graphQLSchemaDocumentCodec
"GraphQL schema document, e.g. the content of schema.gql"
.= _rspdSchema
instance J.FromJSON RemoteSchemaPermissionDefinition where
parseJSON = J.withObject "RemoteSchemaPermissionDefinition" $ \obj -> do
fmap RemoteSchemaPermissionDefinition $ obj J..: "schema"
instance J.ToJSON RemoteSchemaPermissionDefinition where
toJSON (RemoteSchemaPermissionDefinition schema) =
J.object $ ["schema" J..= J.String (TB.run . G.schemaDocument $ schema)]
data RemoteSchemaPermissionMetadata = RemoteSchemaPermissionMetadata
{ _rspmRole :: RoleName,
_rspmDefinition :: RemoteSchemaPermissionDefinition,
_rspmComment :: Maybe Text
}
deriving (Show, Eq, Generic)
instance HasCodec RemoteSchemaPermissionMetadata where
codec =
object "RemoteSchemaPermissionMetadata" $
RemoteSchemaPermissionMetadata
<$> requiredField' "role" .= _rspmRole
<*> requiredField' "definition" .= _rspmDefinition
<*> optionalField' "comment" .= _rspmComment
$(J.deriveJSON hasuraJSON {J.omitNothingFields = True} ''RemoteSchemaPermissionMetadata)