Skip to content

Commit

Permalink
get variable names inside arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
roryc89 committed Sep 22, 2023
1 parent 6dadfc4 commit e79d71c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/GraphQL/Client/Variables.purs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Control.Apply (lift2)
import Data.Argonaut.Core (Json, jsonEmptyObject)
import Data.Argonaut.Encode (class EncodeJson, encodeJson)
import Data.Function (on)
import Data.List (List(..), intercalate, nubBy)
import Data.List (List(..), foldMap, intercalate, nubBy)
import Data.Maybe (Maybe)
import Data.Newtype (class Newtype)
import Data.Symbol (class IsSymbol, reflectSymbol)
Expand Down Expand Up @@ -266,10 +266,14 @@ else instance queryVarsSpreadNewtype ::
) =>
GetGqlQueryVars newtypeSchema (Spread (Proxy alias) args q) where
getGqlQueryVars isArgs _ _ = getGqlQueryVars isArgs (Proxy :: Proxy { | schema }) (Proxy :: Proxy (Spread (Proxy alias) args q))
else instance queryVarsArray :: GetGqlQueryVars a q => GetGqlQueryVars (Array a) q where
else instance queryVarsArrayParam :: GetGqlQueryVars a q => GetGqlQueryVars (Array a) q where
getGqlQueryVars isArgs _ q = getGqlQueryVars isArgs (Proxy :: Proxy a) q
else instance queryVarsMaybe :: GetGqlQueryVars a q => GetGqlQueryVars (Maybe a) q where
else instance queryVarsArrayArg :: GetGqlQueryVars a q => GetGqlQueryVars a (Array q) where
getGqlQueryVars isArgs proxy = foldMap (getGqlQueryVars isArgs proxy)
else instance queryVarsMaybeParam :: GetGqlQueryVars a q => GetGqlQueryVars (Maybe a) q where
getGqlQueryVars isArgs _ q = getGqlQueryVars isArgs (Proxy :: Proxy a) q
else instance queryVarsMaybeArg :: GetGqlQueryVars a q => GetGqlQueryVars a (Maybe q) where
getGqlQueryVars isArgs proxy = foldMap (getGqlQueryVars isArgs proxy)
else instance queryVarsNotNull :: GetGqlQueryVars a q => GetGqlQueryVars (NotNull a) q where
getGqlQueryVars isArgs _ q = getGqlQueryVars isArgs (Proxy :: Proxy a) q
else instance queryVarsUnion ::
Expand All @@ -282,7 +286,7 @@ else instance queryVarsParamsArgs ::
) =>
GetGqlQueryVars ({ | params } -> t) (Args { | args } q) where
getGqlQueryVars _dn _ (Args args q) =
getGqlQueryVars true (Proxy :: _ { | params }) args -- in args values are nullable by default
(getGqlQueryVars true (Proxy :: _ { | params }) args) -- in args values are nullable by default

<> getGqlQueryVars false (Proxy :: Proxy t) q
else instance queryVarsParamsNoArgs ::
Expand Down
16 changes: 15 additions & 1 deletion test/GraphQL/Client/Variable.Test.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Data.Maybe (Maybe)
import GraphQL.Client.Alias ((:))
import GraphQL.Client.Alias.Dynamic (Spread(..))
import GraphQL.Client.Args (OrArg(..), (++), (=>>))
import GraphQL.Client.AsGql (AsGql(..))
import GraphQL.Client.AsGql (AsGql)
import GraphQL.Client.Variable (Var(..))
import GraphQL.Client.Variables (getQueryVars, getVarsTypeNames, withVars)
import Test.Spec (Spec, describe, it)
Expand Down Expand Up @@ -35,6 +35,20 @@ spec =
getVarsTypeNames testSchemaProxy (q `withVars` {}) `shouldEqual`
"($nameVar: Name, $myOtherVar: UserId!, $myVar: customId!)"

it "should return vars for a query with vars in arrays in arguments" do
let
q =
{ users:
{ is_in_rec:
[ { string: Var :: Var "strVar" String
}
]
} =>> { id: unit }
} `withVars` { strVar: "strVal" }

getVarsTypeNames testSchemaProxy (q `withVars` {strVar: "val"}) `shouldEqual`
"($strVar: String)"

type TestSchema =
{ users ::
{ online :: Maybe (AsGql "IsOnline" Boolean)
Expand Down

0 comments on commit e79d71c

Please sign in to comment.