Skip to content

Commit 34df2d4

Browse files
committed
adds query deriving from types #66
1 parent 04ae666 commit 34df2d4

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

Diff for: src/GraphQL/Client/GetFields.purs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
-- | Get the graphql fields from a data type
2+
module GraphQL.Client.GetFields (class GetFieldsStandard, PropGetFieldsStandard, getFieldsStandard) where
3+
4+
import Prelude
5+
6+
import Data.HeytingAlgebra (class HeytingAlgebraRecord, tt)
7+
import Data.Maybe (Maybe)
8+
import Heterogeneous.Mapping (class HMap, class Mapping, hmap)
9+
import Prim.RowList (class RowToList)
10+
import Type.Proxy (Proxy(..))
11+
12+
class GetFieldsStandard :: forall k. k -> Type -> Constraint
13+
class GetFieldsStandard t fields | t -> fields where
14+
getFieldsStandard :: Proxy t -> fields
15+
16+
data PropGetFieldsStandard
17+
= PropGetFieldsStandard
18+
19+
data PropToProxy
20+
= PropToProxy
21+
22+
instance getFieldsStandardRecord ::
23+
( RowToList r t
24+
, HeytingAlgebraRecord t r r
25+
, HMap PropGetFieldsStandard { | r } fields
26+
, HMap PropGetFieldsStandard { | input } { | r }
27+
) =>
28+
GetFieldsStandard { | input } fields where
29+
getFieldsStandard _ = recordGetFieldsStandard (tt :: { | r })
30+
else instance getFieldsStandardMaybe :: GetFieldsStandard a fields => GetFieldsStandard (Maybe a) fields where
31+
getFieldsStandard _ = getFieldsStandard (Proxy :: _ a)
32+
else instance getFieldsStandardArray :: GetFieldsStandard a fields => GetFieldsStandard (Array a) fields where
33+
getFieldsStandard _ = getFieldsStandard (Proxy :: _ a)
34+
else instance getFieldsStandardLeaf :: GetFieldsStandard a Unit where
35+
getFieldsStandard _ = unit
36+
37+
38+
instance propToProxy ::
39+
Mapping PropToProxy t (Proxy t) where
40+
mapping PropToProxy _ = Proxy
41+
42+
recordGetFieldsStandard ::
43+
forall t fields.
44+
HMap PropGetFieldsStandard ({ | t }) fields => { | t } -> fields
45+
recordGetFieldsStandard = hmap PropGetFieldsStandard
46+
47+
48+
instance propGetFieldsStandard ::
49+
(GetFieldsStandard t fields) =>
50+
Mapping PropGetFieldsStandard t fields where
51+
mapping PropGetFieldsStandard _ = getFieldsStandard (Proxy :: _ t)
52+

Diff for: test/GraphQL/Client/GetFields.Test.purs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module GraphQL.Client.GetFields.Test where
2+
3+
import Prelude
4+
import Data.Argonaut.Core (Json)
5+
import Data.DateTime (DateTime)
6+
import Data.Maybe (Maybe)
7+
import GraphQL.Client.GetFields (getFieldsStandard)
8+
import Type.Proxy (Proxy(..))
9+
10+
-- Type level tests
11+
type User
12+
= { id :: Int
13+
, name :: String
14+
}
15+
16+
test1 ::
17+
{ id :: Unit
18+
, name :: Unit
19+
}
20+
test1 = getFieldsStandard (Proxy :: _ User)
21+
22+
type UserComplex
23+
= { id :: Int
24+
, name :: String
25+
, orders ::
26+
Array
27+
{ id :: String
28+
, fulfilled :: Boolean
29+
, status :: Maybe String
30+
, created_at :: DateTime
31+
, fulfilled_at :: Maybe DateTime
32+
, complaints :: Maybe (Array { subject :: String, body :: String })
33+
}
34+
, other_data :: Maybe Json
35+
}
36+
37+
test2 ::
38+
{ id :: Unit
39+
, name :: Unit
40+
, orders ::
41+
{ complaints ::
42+
{ body :: Unit
43+
, subject :: Unit
44+
}
45+
, created_at :: Unit
46+
, fulfilled :: Unit
47+
, fulfilled_at :: Unit
48+
, id :: Unit
49+
, status :: Unit
50+
}
51+
, other_data :: Unit
52+
}
53+
test2 = getFieldsStandard (Proxy :: _ UserComplex)

0 commit comments

Comments
 (0)