Skip to content

Commit 78c6e20

Browse files
committed
Add OrArg and AndArgs instances for GetGqlQueryVars
1 parent 2f7af43 commit 78c6e20

2 files changed

Lines changed: 98 additions & 2 deletions

File tree

src/GraphQL/Client/Variables.purs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,27 @@ else instance varsTypeCheckedSpread ::
265265
class GetGqlQueryVars :: Type -> Type -> Type -> Constraint
266266
class GetGqlQueryVars schema query vars | schema query -> vars
267267

268-
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 ::
269289
( Row.Cons name (Proxy gqlName) () result
270290
, IsSymbol gqlName
271291
) =>

test/GraphQL/Client/Variable.Test.purs

Lines changed: 77 additions & 1 deletion
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

0 commit comments

Comments
 (0)