From 5a9f7b9a65058de0f82889b78f892f01a96e13ee Mon Sep 17 00:00:00 2001 From: Finn Hodgkin Date: Wed, 25 Sep 2024 11:28:07 +0100 Subject: [PATCH] Allow `Variant`s as well as data type enums --- src/GraphQL/Client/Args.purs | 4 +++- src/GraphQL/Client/ToGqlString.purs | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/GraphQL/Client/Args.purs b/src/GraphQL/Client/Args.purs index e2bfc74..e7ca9b1 100644 --- a/src/GraphQL/Client/Args.purs +++ b/src/GraphQL/Client/Args.purs @@ -7,6 +7,7 @@ import Data.Bifunctor (class Bifunctor) import Data.Maybe (Maybe) import Data.Newtype (class Newtype) import Data.Symbol (class IsSymbol) +import Data.Variant (Variant) import GraphQL.Client.Args.AllowedMismatch (AllowedMismatch) import GraphQL.Client.AsGql (AsGql) import GraphQL.Client.NullArray (NullArray) @@ -70,7 +71,8 @@ class ArgGqlAt at params arg instance argAsGql :: ArgGqlAt at param arg => ArgGqlAt at (AsGql gqlName param) arg else instance argToGqlNotNull :: (IsNotNull at param arg, ArgGqlAt at param arg) => ArgGqlAt at (NotNull param) arg else instance argToGqlIgnore :: ArgGqlAt at param IgnoreArg -else instance argVarJson :: ArgGqlAt at Json (Var sym Json) -- Json can only be used with a variable +else instance argToGqlVariant :: (Union r () params) => ArgGqlAt at (Variant params) (Variant r) +else instance argVarJson :: ArgGqlAt at Json (Var sym Json) -- Json can only be used with a variable else instance argToGqlJsonNotAllowed :: TE.Fail (TE.Text "A `Json` query argument can only be used as a variable ") => ArgGqlAt at Json Json else instance argVar :: ArgGqlAt at param arg => ArgGqlAt at param (Var sym arg) else instance argToGqlArrayNull :: ArgGqlAt at (Array param) NullArray diff --git a/src/GraphQL/Client/ToGqlString.purs b/src/GraphQL/Client/ToGqlString.purs index 1431988..5f7e0a1 100644 --- a/src/GraphQL/Client/ToGqlString.purs +++ b/src/GraphQL/Client/ToGqlString.purs @@ -56,6 +56,7 @@ import Data.String.Regex.Flags (global) import Data.String.Regex.Unsafe (unsafeRegex) import Data.Symbol (class IsSymbol, reflectSymbol) import Data.Time (Time) +import Data.Variant (Unvariant(..), Variant, unvariant) import Foreign (Foreign) import Foreign.Object (Object) import Foreign.Object as Object @@ -317,6 +318,8 @@ else instance gqlArgStringErrorBoundary :: GqlArgString a => GqlArgString (Error toGqlArgStringImpl (ErrorBoundary a) = toGqlArgStringImpl a else instance gqlArgStringAllowedMismatch :: GqlArgString a => GqlArgString (AllowedMismatch t a) where toGqlArgStringImpl = unwrap >>> toGqlArgStringImpl +else instance gqlargVariant :: GqlArgString (Variant r) where + toGqlArgStringImpl v = unvariant v # \(Unvariant f) -> f \p _ -> reflectSymbol p else instance gqlArgStringVar :: IsSymbol sym => GqlArgString (Var sym a) where toGqlArgStringImpl _ = "$" <> reflectSymbol (Proxy :: Proxy sym) else instance gqlArgStringOrArg ::