Skip to content

Commit

Permalink
remove unsafe-coerce
Browse files Browse the repository at this point in the history
  • Loading branch information
roryc89 committed Apr 26, 2021
1 parent b304d03 commit 9e904ab
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: purescript-contrib/setup-purescript@main
with:
purescript: "0.14.1"
psa: "0.7.2"
psa: "0.8.2"
spago: "0.20.1"

- name: Cache PureScript dependencies
Expand Down
67 changes: 67 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"name": "purescript-graphql-client",
"license": [
"MIT"
],
"repository": {
"type": "git",
"url": "https://github.com/OxfordAbstracts/purescript-graphql-client"
},
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"output"
],
"dependencies": {
"purescript-aff": "^v6.0.0",
"purescript-aff-promise": "^v3.0.0",
"purescript-affjax": "^v12.0.0",
"purescript-argonaut-codecs": "^v8.1.0",
"purescript-argonaut-core": "^v6.0.0",
"purescript-arrays": "^v6.0.0",
"purescript-bifunctors": "^v5.0.0",
"purescript-control": "^v5.0.0",
"purescript-datetime": "^v5.0.0",
"purescript-effect": "^v3.0.0",
"purescript-either": "^v5.0.0",
"purescript-enums": "^v5.0.0",
"purescript-exceptions": "^v5.0.0",
"purescript-foldable-traversable": "^v5.0.0",
"purescript-foreign": "^v6.0.0",
"purescript-foreign-generic": "^v11.0.0",
"purescript-foreign-object": "^v3.0.0",
"purescript-functions": "^v5.0.0",
"purescript-halogen-subscriptions": "https://github.com/purescript-halogen/purescript-halogen-subscriptions.git#v1.0.0",
"purescript-heterogeneous": "^v0.5.0",
"purescript-http-methods": "^v5.0.0",
"purescript-integers": "^v5.0.0",
"purescript-lists": "^v6.0.0",
"purescript-maybe": "^v5.0.0",
"purescript-media-types": "^v5.0.0",
"purescript-newtype": "^v4.0.0",
"purescript-node-buffer": "^v7.0.0",
"purescript-node-fs": "^v6.0.0",
"purescript-nullable": "^v5.0.0",
"purescript-numbers": "^v8.0.0",
"purescript-ordered-collections": "^v2.0.0",
"purescript-parsing": "^v6.0.0",
"purescript-prelude": "^v5.0.0",
"purescript-profunctor": "^v5.0.0",
"purescript-profunctor-lenses": "^v7.0.0",
"purescript-psci-support": "^v5.0.0",
"purescript-quickcheck": "^v7.0.0",
"purescript-record": "^v3.0.0",
"purescript-spec": "^v5.0.0",
"purescript-spec-discovery": "^v6.0.0",
"purescript-string-parsers": "^v6.0.0",
"purescript-strings": "^v5.0.0",
"purescript-strings-extra": "^v3.0.0",
"purescript-transformers": "^v5.0.0",
"purescript-tuples": "^v6.0.0",
"purescript-type-equality": "^v4.0.0",
"purescript-typelevel": "^v6.0.0",
"purescript-unicode": "^v5.0.0",
"purescript-unsafe-coerce": "^v5.0.0"
}
}
2 changes: 1 addition & 1 deletion spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ You can edit this file as you like.
, "type-equality"
, "typelevel"
, "unicode"
, "unsafe-coerce"
-- , "unsafe-coerce"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
Expand Down
12 changes: 2 additions & 10 deletions src/GraphQL/Client/BaseClients/Affjax.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import Affjax.RequestBody as RequestBody
import Affjax.RequestHeader (RequestHeader(..))
import Affjax.ResponseFormat as ResponseFormat
import Data.Argonaut.Core (Json)
import Data.Argonaut.Encode (encodeJson)
import Data.Either (Either(..))
import Data.HTTP.Method as Method
import Data.Maybe (Maybe(..))
import Data.MediaType.Common (applicationJSON)
import Effect.Aff (Aff, error, throwError)
import GraphQL.Client.Types (class QueryClient)
import Unsafe.Coerce (unsafeCoerce)

data AffjaxClient
= AffjaxClient URL (Array RequestHeader)
Expand Down Expand Up @@ -41,18 +41,10 @@ queryPostForeign opStr url headers queryName q =
, content =
Just
$ RequestBody.Json
$ toJson
$ encodeJson
{ query: opStr <> " " <> queryName <> " " <> q
, variables: {}
, operationName: queryName
}
, headers = headers <> [ ContentType applicationJSON ]
}
where
toJson ::
{ operationName :: String
, query :: String
, variables :: {}
} ->
Json
toJson = unsafeCoerce
4 changes: 1 addition & 3 deletions src/GraphQL/Client/BaseClients/Apollo.purs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import GraphQL.Client.BaseClients.Apollo.ErrorPolicy (ErrorPolicy(..))
import GraphQL.Client.BaseClients.Apollo.FetchPolicy (FetchPolicy)
import GraphQL.Client.ToGqlString (toGqlQueryString)
import GraphQL.Client.Types (class GqlQuery, class QueryClient, class SubscriptionClient, class WatchQueryClient, Client(..))
import Unsafe.Coerce (unsafeCoerce)

type ApolloClientOptions
= { url :: URL
Expand Down Expand Up @@ -182,8 +181,7 @@ queryForeign ::
forall q m client.
QueryClient client q m =>
Boolean -> Foreign -> client -> String -> String -> Aff Json
queryForeign isMutation opts client name q_ = fromEffectFnAff $ fn opts (unsafeCoerce client) q
-- fromEffectFnAff $ fn (apolloOptionsToForeign opts) (unsafeCoerce client) q
queryForeign isMutation opts client name q_ = fromEffectFnAff $ fn opts (unsafeToForeign client) q
where
fn = if isMutation then mutationImpl else queryImpl

Expand Down
5 changes: 2 additions & 3 deletions src/GraphQL/Client/BaseClients/Urql.purs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ import Data.Tuple (Tuple(..))
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
import Foreign (Foreign)
import Foreign (Foreign, unsafeToForeign)
import Foreign.Object (Object)
import Foreign.Object as Object
import GraphQL.Client.Types (class QueryClient, class SubscriptionClient, Client(..))
import Unsafe.Coerce (unsafeCoerce)

type UrqlClientOptions
= { url :: URL
Expand Down Expand Up @@ -110,7 +109,7 @@ queryForeign ::
forall client o.
QueryClient client o o =>
Boolean -> client -> String -> String -> Aff Json
queryForeign isMutation client name q_ = fromEffectFnAff $ fn (unsafeCoerce client) q
queryForeign isMutation client name q_ = fromEffectFnAff $ fn (unsafeToForeign client) q
where
fn = if isMutation then mutationImpl else queryImpl

Expand Down
3 changes: 3 additions & 0 deletions src/GraphQL/Hasura/Encode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.unsafeToJson = function (x) {
return x
}
8 changes: 3 additions & 5 deletions src/GraphQL/Hasura/Encode.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Data.DateTime (Date, DateTime(..), Time(..), day, month, year)
import Data.Enum (class BoundedEnum, fromEnum)
import Data.Maybe (Maybe)
import Heterogeneous.Mapping (class HMap, class Mapping, hmap)
import Unsafe.Coerce (unsafeCoerce)

class EncodeHasura a where
encodeHasura :: a -> Json
Expand Down Expand Up @@ -65,10 +64,9 @@ showEnum :: forall b. BoundedEnum b => b -> String
showEnum = show <<< fromEnum

instance encodeHasuraRecord :: HMap EncodeHasuraProp { | r } jsonRecord => EncodeHasura { | r } where
encodeHasura = encodeRecordProps >>> recordToJson
where
recordToJson :: jsonRecord -> Json
recordToJson = unsafeCoerce
encodeHasura = encodeRecordProps >>> unsafeToJson

foreign import unsafeToJson :: forall a. a -> Json

data EncodeHasuraProp
= EncodeHasuraProp
Expand Down
12 changes: 6 additions & 6 deletions test/GraphQL/Hasura/Decode.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,53 @@ module GraphQL.Hasura.Decode.Test where

import Prelude

import Data.Argonaut.Core (fromString)
import Data.DateTime (DateTime(..), Time(..), canonicalDate)
import Data.Either (Either(..))
import Data.Enum (class BoundedEnum, toEnum)
import Data.Maybe (fromMaybe)
import GraphQL.Hasura.Decode (decodeHasura)
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual)
import Unsafe.Coerce (unsafeCoerce)

spec :: Spec Unit
spec =
describe " GraphQL.Hasura.Decode" do
describe "decodeHasura" do
it "decodes a date time without milliseconds"
$ decodeHasura (unsafeCoerce "2020-10-09T08:07:06")
$ decodeHasura (fromString "2020-10-09T08:07:06")
`shouldEqual`
( Right
$ DateTime
(canonicalDate (toEnumB 2020) (toEnumB 10) (toEnumB 9))
(Time (toEnumB 8) (toEnumB 7) (toEnumB 6) (toEnumB 0))
)
it "decodes a date time with milliseconds"
$ decodeHasura (unsafeCoerce "2020-10-09T08:07:06.555")
$ decodeHasura (fromString "2020-10-09T08:07:06.555")
`shouldEqual`
( Right
$ DateTime
(canonicalDate (toEnumB 2020) (toEnumB 10) (toEnumB 9))
(Time (toEnumB 8) (toEnumB 7) (toEnumB 6) (toEnumB 555))
)
it "decodes a date time with microseconds"
$ decodeHasura (unsafeCoerce "2020-10-09T08:07:06.555555")
$ decodeHasura (fromString "2020-10-09T08:07:06.555555")
`shouldEqual`
( Right
$ DateTime
(canonicalDate (toEnumB 2020) (toEnumB 10) (toEnumB 9))
(Time (toEnumB 8) (toEnumB 7) (toEnumB 6) (toEnumB 555))
)
it "decodes a date time with a positive timezone"
$ decodeHasura (unsafeCoerce "2020-10-09T08:07:06.555555+01:00")
$ decodeHasura (fromString "2020-10-09T08:07:06.555555+01:00")
`shouldEqual`
( Right
$ DateTime
(canonicalDate (toEnumB 2020) (toEnumB 10) (toEnumB 9))
(Time (toEnumB 7) (toEnumB 7) (toEnumB 6) (toEnumB 555))
)
it "decodes a date time with a negative timezone"
$ decodeHasura (unsafeCoerce "2020-10-09T08:00:00.555555-02:30")
$ decodeHasura (fromString "2020-10-09T08:00:00.555555-02:30")
`shouldEqual`
( Right
$ DateTime
Expand Down

0 comments on commit 9e904ab

Please sign in to comment.