GraphQL.purs 1.94 KB
Newer Older
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
-- https://github.com/OxfordAbstracts/purescript-graphql-client/pull/68/files#diff-404493334c4108c2411bd1767059be2e94295df5742d14d58116667dc5e80dd7R16-R19

module Gargantext.Utils.GraphQL
       -- | Get the graphql fields from a data type
       (class GetFieldsStandard, PropGetFieldsStandard, getFieldsStandard) where

import Prelude

import Data.HeytingAlgebra (class HeytingAlgebraRecord, tt)
import Data.Maybe (Maybe)
import Heterogeneous.Mapping (class HMap, class Mapping, hmap)
import Prim.RowList (class RowToList)
import Type.Proxy (Proxy(..))

class GetFieldsStandard :: forall k. k -> Type -> Constraint
class GetFieldsStandard t fields | t -> fields where
  getFieldsStandard :: Proxy t -> fields

data PropGetFieldsStandard
  = PropGetFieldsStandard

data PropToProxy
  = PropToProxy

instance getFieldsStandardRecord ::
  ( RowToList r t
  , HeytingAlgebraRecord t r r
  , HMap PropGetFieldsStandard { | r } fields
  , HMap PropGetFieldsStandard { | input } { | r }
  ) =>
  GetFieldsStandard { | input } fields where
  getFieldsStandard _ = recordGetFieldsStandard (tt :: { | r })
else instance getFieldsStandardMaybe :: GetFieldsStandard a fields => GetFieldsStandard (Maybe a) fields where
  getFieldsStandard _ = getFieldsStandard (Proxy :: _ a)
else instance getFieldsStandardArray :: GetFieldsStandard a fields => GetFieldsStandard (Array a) fields where
  getFieldsStandard _ = getFieldsStandard (Proxy :: _ a)
else instance getFieldsStandardLeaf :: GetFieldsStandard a Unit where
  getFieldsStandard _ = unit


instance propToProxy ::
  Mapping PropToProxy t (Proxy t) where
  mapping PropToProxy _ = Proxy

recordGetFieldsStandard ::
  forall t fields.
  HMap PropGetFieldsStandard ({ | t }) fields => { | t } -> fields
recordGetFieldsStandard = hmap PropGetFieldsStandard


instance propGetFieldsStandard ::
  (GetFieldsStandard t fields) =>
  Mapping PropGetFieldsStandard t fields where
  mapping PropGetFieldsStandard _ = getFieldsStandard (Proxy :: _ t)