morpheus-graphql-client

Morpheus GraphQL Client https://morpheusgraphql.com

Version on this page:0.14.1
Stackage Nightly 2020-09-29:0.15.1
Latest on Hackage:0.15.1

See all snapshots morpheus-graphql-client appears in

MIT licensed by Daviti Nalchevanidze

Module documentation for 0.14.1

This version can be pinned in stack with:morpheus-graphql-client-0.14.1@sha256:5766b2a1e46e2fe61085b5eabd278da1a4aae96d86cf71d2f315b1b766770d69,2630

Morpheus GraphQL Client

Morpheus GraphQL Client with Template haskell QuasiQuotes

defineByDocumentFile
    "./schema.gql"
  [gql|
    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"

Changes

Changelog

0.14.1 - 16.08.2020

minor

  • fixed Build error during testing #5602

0.14.0 - 15.08.2020

new features

  • supports interfaces.

  • supports of block string values.

  • support of schema. issue #412

    schema {
      query: MyQuery
    }
    
  • generated types have instance of class Eq

breaking changes

  • custom scalars Should Provide instance of class Eq

0.13.0 - 22.06.2020

breaking changes

  • from now you should provide for every custom graphql scalar definition coresponoding haskell type definition and GQLScalar implementation fot it. for details see examples-client

  • input fields and query arguments are imported without namespacing

new features

  • exposed: ScalarValues,GQLScalar, ID

0.12.0 - 21.05.2020