Skip to content

Commit b37dcc2

Browse files
authored
Merge pull request #150 from purefunctor/justin/and-or-args-variables
Add instance for AndArgs
2 parents f9c5b57 + 78c6e20 commit b37dcc2

2 files changed

Lines changed: 126 additions & 7 deletions

File tree

src/GraphQL/Client/Variables.purs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import Data.String.CodeUnits (charAt)
3636
import Data.Symbol (class IsSymbol, reflectSymbol)
3737
import GraphQL.Client.Alias (Alias)
3838
import GraphQL.Client.Alias.Dynamic (Spread)
39-
import GraphQL.Client.Args (AndArg, Args, NotNull, OrArg)
39+
import GraphQL.Client.Args (AndArg, AndArgs, Args, NotNull, OrArg)
4040
import GraphQL.Client.AsGql (AsGql)
4141
import GraphQL.Client.Directive (ApplyDirective(..))
4242
import GraphQL.Client.ErrorBoundary (ErrorBoundary)
@@ -126,6 +126,15 @@ else instance getVarAndArg ::
126126
) =>
127127
GetVar (AndArg l r) { | var } where
128128
getVar _ = Proxy
129+
else instance getVarAndArgs ::
130+
( GetVar l { | varL }
131+
, GetVar r { | varR }
132+
, Row.Union varR varL trash
133+
, Row.Union varL varR trash -- keep both union directions to make sure value type is the same
134+
, Row.Nub trash var
135+
) =>
136+
GetVar (AndArgs l r) { | var } where
137+
getVar _ = Proxy
129138
else instance getVarOrArg ::
130139
( GetVar l { | varL }
131140
, GetVar r { | varR }
@@ -229,19 +238,19 @@ else instance varsTypeCheckedWithoutVars ::
229238

230239
else instance varsTypeCheckedIdentity ::
231240
GetVar query {} =>
232-
VarsTypeChecked schema (Identity query) where
241+
VarsTypeChecked schema (Identity query) where
233242
getVarsJson _ _ = jsonEmptyObject
234243
getVarsTypeNames _ _ = ""
235244

236245
else instance varsTypeCheckedErrorBoundary ::
237246
GetVar query {} =>
238247
VarsTypeChecked schema (ErrorBoundary query) where
239248
getVarsJson _ _ = jsonEmptyObject
240-
getVarsTypeNames _ _ = ""
249+
getVarsTypeNames _ _ = ""
241250

242251
else instance varsTypeCheckedUnion ::
243252
GetVar { | query } {} =>
244-
VarsTypeChecked schema (GqlUnion query) where
253+
VarsTypeChecked schema (GqlUnion query) where
245254
getVarsJson _ _ = jsonEmptyObject
246255
getVarsTypeNames _ _ = ""
247256

@@ -256,7 +265,27 @@ else instance varsTypeCheckedSpread ::
256265
class GetGqlQueryVars :: Type -> Type -> Type -> Constraint
257266
class GetGqlQueryVars schema query vars | schema query -> vars
258267

259-
instance queryVarsAsGqlVar ::
268+
instance queryVarsOrArg ::
269+
( GetGqlQueryVars schema l { | varsL }
270+
, GetGqlQueryVars schema r { | varsR }
271+
, Row.Union varsL varsR trash
272+
, Row.Union varsR varsL trash
273+
, Row.Nub trash vars
274+
)
275+
-- Since this matches over _any_ schema, this must come before the pattern
276+
-- match for AsGql such that the schema is preserved through l and r.
277+
=> GetGqlQueryVars schema (OrArg l r) { | vars }
278+
279+
else instance queryVarsAndArgs ::
280+
( GetGqlQueryVars schema l { | varsL }
281+
, GetGqlQueryVars schema r { | varsR }
282+
, Row.Union varsL varsR trash
283+
, Row.Union varsR varsL trash
284+
, Row.Nub trash vars
285+
)
286+
=> GetGqlQueryVars schema (AndArgs l r) { | vars }
287+
288+
else instance queryVarsAsGqlVar ::
260289
( Row.Cons name (Proxy gqlName) () result
261290
, IsSymbol gqlName
262291
) =>

test/GraphQL/Client/Variable.Test.purs

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Prelude
55
import Data.Maybe (Maybe(..))
66
import GraphQL.Client.Alias ((:))
77
import GraphQL.Client.Alias.Dynamic (Spread(..))
8-
import GraphQL.Client.Args (OrArg(..), (++), (=>>))
8+
import GraphQL.Client.Args (OrArg(..), andArg, guardArg, (++), (+++), (=>>))
99
import GraphQL.Client.AsGql (AsGql)
1010
import GraphQL.Client.Variable (Var(..))
1111
import GraphQL.Client.Variables (class GetGqlQueryVars, getQueryVars, getVarsTypeNames, withVars)
@@ -237,6 +237,82 @@ testGqlVarsCreatedAt =
237237
, created_at_lt: 3
238238
}
239239

240+
testGqlVarsOrArg
241+
:: Proxy
242+
{ created_at_eq :: Proxy "Int"
243+
, created_at_lt :: Proxy "Int"
244+
, myVar :: Proxy "customId"
245+
}
246+
testGqlVarsOrArg =
247+
getGqlQueryVars $
248+
{ users:
249+
{ where: { created_at: { eq: guardArg false $ Var @"created_at_eq" @Int, lt: guardArg false $ Var @"created_at_lt" @Int } }
250+
}
251+
=>> { id: guardArg false $ Var @"myVar" @Int }
252+
}
253+
254+
testGqlVarsAndArg
255+
:: Proxy
256+
{ created_at_eq :: Proxy "Int"
257+
, created_at_lt :: Proxy "Int"
258+
, id1 :: Proxy "customId"
259+
, id2 :: Proxy "customId"
260+
}
261+
testGqlVarsAndArg =
262+
getGqlQueryVars $
263+
{ users:
264+
{ where:
265+
{ created_at:
266+
andArg
267+
{ eq: Var @"created_at_eq" @Int }
268+
{ lt: Var @"created_at_lt" @Int }
269+
}
270+
}
271+
=>>
272+
andArg
273+
{ id: Var @"id1" @Int }
274+
{ id: Var @"id2" @Int }
275+
}
276+
277+
testGqlVarsComplex
278+
:: Proxy
279+
{ created_at_eq :: Proxy "Int"
280+
, created_at_eq_2 :: Proxy "Int"
281+
, created_at_lt :: Proxy "Int"
282+
, created_at_lt_2 :: Proxy "Int"
283+
, id1 :: Proxy "customId"
284+
, id2 :: Proxy "customId"
285+
, id3 :: Proxy "customId"
286+
, id4 :: Proxy "customId"
287+
}
288+
testGqlVarsComplex =
289+
getGqlQueryVars $
290+
{ users:
291+
{ where:
292+
{ created_at:
293+
andArg
294+
{ eq:
295+
if true then ArgL $ Var @"created_at_eq" @Int
296+
else ArgR $ Var @"created_at_eq_2" @Int
297+
}
298+
{ lt:
299+
if true then ArgL $ Var @"created_at_lt" @Int
300+
else ArgR $ Var @"created_at_lt_2" @Int
301+
}
302+
}
303+
}
304+
=>>
305+
andArg
306+
{ id:
307+
if true then ArgL $ Var @"id1" @Int
308+
else ArgR $ Var @"id2" @Int
309+
}
310+
{ id:
311+
if true then ArgL $ Var @"id3" @Int
312+
else ArgR $ Var @"id4" @Int
313+
}
314+
}
315+
240316
testBasic
241317
:: Proxy
242318
{ var1 :: Int
@@ -284,6 +360,20 @@ testArgs
284360
}
285361
testArgs = getQueryVars ((Var :: _ "idVar" Int) =>> { name: unit })
286362

363+
testAndArg
364+
:: Proxy
365+
{ aVar :: Int
366+
, bVar :: Number
367+
}
368+
testAndArg =
369+
getQueryVars
370+
( { a:
371+
Var :: _ "aVar" Int
372+
}
373+
++ { b: Var :: _ "bVar" Number }
374+
=>> { name: unit }
375+
)
376+
287377
testAndArgs
288378
:: Proxy
289379
{ aVar :: Int
@@ -294,7 +384,7 @@ testAndArgs =
294384
( { a:
295385
Var :: _ "aVar" Int
296386
}
297-
++ { b: Var :: _ "bVar" Number }
387+
+++ { b: Var :: _ "bVar" Number }
298388
=>> { name: unit }
299389
)
300390

0 commit comments

Comments
 (0)