morpheus-graphql-client: Morpheus GraphQL Client

[ client, graphql, library, mit, web ] [ Propose Tags ] [ Report a vulnerability ]

Build GraphQL APIs with your favorite functional language!


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.12.0, 0.13.0, 0.14.0, 0.14.1, 0.15.0, 0.15.1, 0.16.0, 0.17.0, 0.18.0, 0.19.0, 0.19.1, 0.19.2, 0.19.3, 0.20.0, 0.20.1, 0.21.0, 0.22.0, 0.22.1, 0.23.0, 0.24.0, 0.24.1, 0.24.2, 0.24.3, 0.25.0, 0.26.0, 0.27.0, 0.27.1, 0.27.2, 0.27.3, 0.28.0, 0.28.1
Change log changelog.md
Dependencies aeson (>=1.4.4 && <3.0.0), base (>=4.7.0 && <5.0.0), bytestring (>=0.10.4 && <0.15.0), containers (>=0.4.2.1 && <=0.7), file-embed (>=0.0.10 && <1.0.0), modern-uri (>=0.1.0.0 && <1.0.0), morpheus-graphql-code-gen-utils (>=0.28.0 && <0.29.0), morpheus-graphql-core (>=0.28.0 && <0.29.0), morpheus-graphql-subscriptions (>=0.28.0 && <0.29.0), mtl (>=2.0.0 && <3.0.0), prettyprinter (>=1.7.0 && <2.0.0), relude (>=0.3.0 && <2.0.0), req (>=3.0.0 && <4.0.0), template-haskell (>=2.0.0 && <3.0.0), text (>=1.2.3 && <3.0.0), transformers (>=0.3.0 && <0.7.0), unliftio-core (>=0.0.1 && <0.4.0), unordered-containers (>=0.2.8 && <0.3.0), websockets (>=0.12.6.0 && <1.0.0), wuss (>=1.0.0 && <3.0.0) [details]
License MIT
Copyright (c) 2019 Daviti Nalchevanidze
Author Daviti Nalchevanidze
Maintainer d.nalchevanidze@gmail.com
Category web, graphql, client
Home page https://morpheusgraphql.com
Bug tracker https://github.com/nalchevanidze/morpheus-graphql/issues
Source repo head: git clone https://github.com/nalchevanidze/morpheus-graphql
Uploaded by nalchevanidze at 2024-06-10T08:34:31Z
Distributions LTSHaskell:0.28.1, NixOS:0.27.3, Stackage:0.28.1
Downloads 3556 total (31 in the last 30 days)
Rating 2.25 (votes: 2) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2024-06-10 [all 1 reports]

Readme for morpheus-graphql-client-0.28.1

[back to package description]

Morpheus GraphQL Client

Morpheus GraphQL Client with Template haskell QuasiQuotes

declareLocalTypesInline
    "./schema.gql"
  [raw|
    query GetHero ($character: Character)
      {
        deity (fatherOf:$character) {
          name
          power
          worships {
            deity2Name: name
          }
        }
      }
  |]

with schema:

input Character {
  name: String!
}

type Deity {
  name: String!
  worships: Deity
  power: Power!
}

enum Power {
  Lightning
  Teleportation
  Omniscience
}

will validate query and Generate:

  • namespaced response and variable types
  • instance for Fetch typeClass
data GetHero = GetHero {
  deity: DeityDeity
}

-- from: {user
data DeityDeity = DeityDeity {
  name: Text,
  worships: Maybe DeityWorshipsDeity
  power: Power
}

-- from: {deity{worships
data DeityWorshipsDeity = DeityWorshipsDeity {
  name: Text,
}

data Power =
    PowerLightning
  | PowerTeleportation
  | PowerOmniscience

data GetHeroArgs = GetHeroArgs {
  character: Character
}

data Character = Character {
  name: Person
}

as you see, response type field name collision can be handled with GraphQL alias.

with fetch you can fetch well typed response GetHero.

  fetchHero :: Args GetHero -> m (Either String GetHero)
  fetchHero = fetch jsonRes args
      where
        args = GetHeroArgs {character = Person {name = "Zeus"}}
        jsonRes :: ByteString -> m ByteString
        jsonRes = <GraphQL APi>

in this case, jsonRes resolves a request into a response in some monad m.

A fetch resolver implementation against a real API may look like the following:

{-# LANGUAGE OverloadedStrings #-}

import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Char8 as C8
import Network.HTTP.Req

resolver :: String -> ByteString -> IO ByteString
resolver tok b = runReq defaultHttpConfig $ do
    let headers = header "Content-Type" "application/json"
    responseBody <$> req POST (https "swapi.graph.cool") (ReqBodyLbs b) lbsResponse headers

this is demonstrated in examples/src/Client/StarWarsClient.hs

types can be generated from introspection too:

defineByIntrospectionFile "./introspection.json"