Skip to content

Commit

Permalink
Add allow wrapping the return result in Identity.
Browse files Browse the repository at this point in the history
Why:

Often two very similar types exist in the application.
1) The type returned from the server.
2) The type used by a component.
Frequently enough the only difference between the two is that some fields are wrapped in a functor. So if we allow wrapping the results in Identity, we will be able to share the type definition. E.g.

type Data f = { id: ID, name: f String }

type Return = Data Identity
type State = Data (Either Union)

Thus Identity fulfills a similar role to ErrorBoundary, but is a newtype.
  • Loading branch information
bakhtiyarneyman committed May 1, 2024
1 parent a5531b0 commit 4d58b7e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ You can edit this file as you like.
, "halogen-subscriptions"
, "heterogeneous"
, "http-methods"
, "identity"
, "integers"
, "lists"
, "maybe"
Expand Down
7 changes: 5 additions & 2 deletions src/GraphQL/Client/QueryReturns.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module GraphQL.Client.QueryReturns

import Prelude

import Data.Identity (Identity(..))
import Data.Maybe (Maybe)
import Data.Newtype (class Newtype, unwrap)
import Data.Symbol (class IsSymbol)
Expand Down Expand Up @@ -55,6 +56,8 @@ else instance queryReturnsGqlType :: QueryReturnsAt at a q t => QueryReturnsAt a
queryReturnsAtImpl at _ q = queryReturnsAtImpl at (undefined :: a) q
else instance queryReturnsApplyDirective :: QueryReturnsAt at a q t => QueryReturnsAt at a (ApplyDirective name args q) t where
queryReturnsAtImpl at a _ = queryReturnsAtImpl at a (undefined :: q)
else instance queryReturnsIdentity :: QueryReturnsAt at a q t => QueryReturnsAt at a (Identity q) (Identity t) where
queryReturnsAtImpl at a _ = Identity $ queryReturnsAtImpl at a (undefined :: q)
else instance queryReturnErrorBoundary :: QueryReturnsAt at a q t => QueryReturnsAt at a (ErrorBoundary q) (BoundaryResult Unit t) where
queryReturnsAtImpl at a _ = ErrorBoundary.Result $ queryReturnsAtImpl at a (undefined :: q)
else instance queryReturnsSpread ::
Expand All @@ -75,9 +78,9 @@ else instance queryReturnsArray :: QueryReturnsAt at a q t => QueryReturnsAt at
else instance queryReturnsMaybe :: QueryReturnsAt at a q t => QueryReturnsAt at (Maybe a) q (Maybe t) where
queryReturnsAtImpl at _ q = pure $ queryReturnsAtImpl at (undefined :: a) q
else instance queryReturnsUnion ::
HMapWithIndex (PropToSchemaType schema) (Record query) (Record returns) =>
HMapWithIndex (PropToSchemaType schema) { | query} { | returns } =>
QueryReturnsAt at (GqlUnion schema) (GqlUnion query) (UnionReturned returns) where
queryReturnsAtImpl at _ _ = undefined
queryReturnsAtImpl _ _ _ = undefined
else instance queryReturnsParamsArgs ::
( QueryReturnsAt at t q result
, HMapWithIndex (ArgPropToGql params) { | args } s
Expand Down
4 changes: 3 additions & 1 deletion src/GraphQL/Client/ToGqlString.purs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import Data.DateTime as DT
import Data.Enum (class BoundedEnum, fromEnum)
import Data.FoldableWithIndex (foldlWithIndex)
import Data.Function (on)
import Data.Identity (Identity(..))
import Data.List (List)
import Data.List as List
import Data.Map (Map)
Expand Down Expand Up @@ -108,6 +109,8 @@ else instance gqlQueryStringApplyDirective ::
<> reflectSymbol (Proxy :: Proxy name)
<> gqlArgStringRecordTopLevel args
<> toGqlQueryStringImpl opts q
else instance gqlQueryStringIdentity :: GqlQueryString a => GqlQueryString (Identity a) where
toGqlQueryStringImpl opts (Identity a) = toGqlQueryStringImpl opts a
else instance gqlQueryStringErrorBoundary :: GqlQueryString a => GqlQueryString (ErrorBoundary a) where
toGqlQueryStringImpl opts (ErrorBoundary a) = toGqlQueryStringImpl opts a
else instance gqlQueryStringSymbol :: IsSymbol s => GqlQueryString (Proxy s) where
Expand Down Expand Up @@ -484,4 +487,3 @@ else instance isIgnoreArgOrArg :: (IsIgnoreArg l, IsIgnoreArg r) => IsIgnoreArg
ArgR r -> isIgnoreArg r
else instance isIgnoreArgOther :: IsIgnoreArg a where
isIgnoreArg _ = false

18 changes: 18 additions & 0 deletions test/GraphQL/Client/QueryReturns.Test.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module GraphQL.Client.QueryReturns.Test where

import Prelude

import Data.Identity (Identity(..))
import Data.Maybe (Maybe(..))
import Data.Newtype (class Newtype)
import GraphQL.Client.Alias ((:))
Expand Down Expand Up @@ -328,6 +329,23 @@ testDirective =
=>> { id }
}

testIdentity
:: Proxy
{ users ::
Array
{ id :: Identity Int
}
}
testIdentity =
queryReturns testSchemaProxy
$
{ users:
{ is_in_rec:
[ { int: 0 } ] +++ ((ArgR [ ignoreOrStr true, ignoreOrStr false ]) :: OrArg IgnoreArg _)
}
=>> { id: Identity id }
}

ignoreOrStr
:: Boolean
-> OrArg IgnoreArg
Expand Down

0 comments on commit 4d58b7e

Please sign in to comment.