Skip to content

Commit

Permalink
Release 0.3.0 (#244)
Browse files Browse the repository at this point in the history
* update Version Number

* update import wit Namespace

* foo

* fix hs language extension

* fix schema.gql

* mandatory GQLType

* update subResolver

* update webpage docs

* update changelog date
  • Loading branch information
nalchevanidze authored Oct 4, 2019
1 parent 579fadf commit c3fc385
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 67 deletions.
85 changes: 54 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Just open an issue here on GitHub, or join [our Slack channel](https://morpheus-

To get started with Morpheus, you first need to add it to your project's dependencies, as follows (assuming you're using hpack):

package.yml
_package.yml_

```yaml
dependencies:
Expand All @@ -23,7 +23,7 @@ dependencies:
Additionally, you should tell stack which version to pick:
stack.yml
_stack.yml_
```yaml
resolver: lts-13.24
Expand All @@ -38,38 +38,62 @@ As Morpheus is quite new, make sure stack can find morpheus-graphql by running `

### Building your first GrqphQL API

### with GraphQL syntax and Haskell QuasiQuotes
### with GraphQL syntax

_schema.gql_

```gql
type Query {
deity(name: String!): Deity!
}
type Deity {
name: String!
power: String
}
```

_API.hs_

```haskell
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
[gqlDocument|
type Query {
deity (uid: String!): Deity!
}
module API (api) where
type Deity {
name : String!
power : String
}
|]
import qualified Data.ByteString.Lazy.Char8 as B
rootResolver :: GQLRootResolver IO () () Query () ()
import Data.Morpheus (interpreter)
import Data.Morpheus.Document (importGQLDocumentWithNamespace)
import Data.Morpheus.Types (GQLRootResolver (..), IORes)
import Data.Text (Text)
importGQLDocumentWithNamespace "schema.gql"
rootResolver :: GQLRootResolver IO () () (Query IORes) () ()
rootResolver =
GQLRootResolver {queryResolver = return Query {deity}, mutationResolver = pure (), subscriptionResolver = pure ()}
GQLRootResolver
{queryResolver = return Query {queryDeity}, mutationResolver = pure (), subscriptionResolver = pure ()}
where
deity DeityArgs {uid} = pure Deity {name, power}
queryDeity QueryDeityArgs {queryDeityArgsName} = pure Deity {deityName, deityPower}
where
name _ = pure "Morpheus"
power _ = pure (Just "Shapeshifting")
deityName _ = pure "Morpheus"
deityPower _ = pure (Just "Shapeshifting")
gqlApi :: ByteString -> IO ByteString
gqlApi = interpreter rootResolver
api :: ByteString -> IO ByteString
api = interpreter rootResolver
```

Template Haskell Generates types: `Query` , `Deity`, `DeityArgs`, that can be used by `rootResolver`

generated types are not compatible with `Mutation`, `Subscription`,
they can be used only in `Query`, but this issue will be fixed in next release
`importGQLDocumentWithNamespace` will generate Types with namespaced fields. if you don't need napespacing use `importGQLDocument`

### with Native Haskell Types

Expand All @@ -79,7 +103,7 @@ which derives the `Generic` typeclass. Lazily resolvable fields on this `Query`
```haskell
data Query = Query
{ deity :: DeityArgs -> IORes Deity
} deriving (Generic)
} deriving (Generic, GQLType)
data Deity = Deity
{ fullName :: Text -- Non-Nullable Field
Expand Down Expand Up @@ -260,7 +284,7 @@ Just exchange deriving `GQLQuery` for `GQLMutation` and declare them separately
```haskell
newtype Mutation = Mutation
{ createDeity :: Form -> IOMutRes Deity
} deriving (Generic)
} deriving (Generic, GQLType)
createDeityMutation :: Form -> IOMutRes Deity
createDeityMutation = ...
Expand Down Expand Up @@ -298,15 +322,15 @@ data Content
newtype Query = Query
{ deity :: () -> IORes Deity
} deriving (Generic)
} deriving (Generic, GQLType)
newtype Mutation = Mutation
{ createDeity :: () -> IOMutRes Channel Content Deity
} deriving (Generic)
} deriving (Generic, GQLType)
newtype Subscription = Subscription
{ newDeity :: () -> IOSubRes Channel Content Deity
} deriving (Generic)
} deriving (Generic, GQLType)
rootResolver :: GQLRootResolver IO Channel Content Query Mutation Subscription
rootResolver =
Expand All @@ -318,11 +342,11 @@ rootResolver =
where
fetchDeity = resolver $ dbDeity "" Nothing
createDeity _args = toMutResolver [Event {channels = [ChannelA], content = ContentA 1}] fetchDeity
newDeity _args = Event {channels = [ChannelA], content}
newDeity _args = SubResolver {subChannels = [ChannelA], subResolver}
where
content (Event [ChannelA] (ContentA _value)) = resolver $ dbDeity "" Nothing -- resolve New State
content (Event [ChannelA] (ContentB value)) = resolver $ dbDeity value Nothing -- resolve New State
content _ = fetchDeity -- Resolve Old State
subResolver (Event [ChannelA] (ContentA _value)) = resolver $ dbDeity "" Nothing -- resolve New State
subResolver (Event [ChannelA] (ContentB value)) = resolver $ dbDeity value Nothing -- resolve New State
subResolver _ = fetchDeity -- Resolve Old State
```

## Morpheus `GraphQL Client` with Template haskell QuasiQuotes
Expand Down Expand Up @@ -397,7 +421,6 @@ Morpheus is written and maintained by [_nalchevanidze_](https://github.com/nalch
- Medium future:
- Stabilize API
- Specification-isomorphic introspection
- Specification-isomorphic error handling
- Long term:
- Support all possible GQL features
Expand Down
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## [0.3.0] - \*.09.2019
## [0.3.0] - 04.10.2019

### Added

Expand Down
86 changes: 54 additions & 32 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
layout: home
---

# Morpheus GraphQL [![Hackage](https://img.shields.io/hackage/v/morpheus-graphql.svg)](https://hackage.haskell.org/package/morpheus-graphql) [![CircleCI](https://circleci.com/gh/morpheusgraphql/morpheus-graphql.svg?style=svg)](https://circleci.com/gh/morpheusgraphql/morpheus-graphql)

Build GraphQL APIs with your favourite functional language!
Expand All @@ -18,7 +17,7 @@ Just open an issue here on GitHub, or join [our Slack channel](https://morpheus-

To get started with Morpheus, you first need to add it to your project's dependencies, as follows (assuming you're using hpack):

package.yml
_package.yml_

```yaml
dependencies:
Expand All @@ -27,7 +26,7 @@ dependencies:
Additionally, you should tell stack which version to pick:
stack.yml
_stack.yml_
```yaml
resolver: lts-13.24
Expand All @@ -42,38 +41,62 @@ As Morpheus is quite new, make sure stack can find morpheus-graphql by running `

### Building your first GrqphQL API

### with GraphQL syntax and Haskell QuasiQuotes
### with GraphQL syntax

_schema.gql_

```gql
type Query {
deity(name: String!): Deity!
}
type Deity {
name: String!
power: String
}
```

_API.hs_

```haskell
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
[gqlDocument|
type Query {
deity (uid: String!): Deity!
}
module API (api) where
type Deity {
name : String!
power : String
}
|]
import qualified Data.ByteString.Lazy.Char8 as B
rootResolver :: GQLRootResolver IO () () Query () ()
import Data.Morpheus (interpreter)
import Data.Morpheus.Document (importGQLDocumentWithNamespace)
import Data.Morpheus.Types (GQLRootResolver (..), IORes)
import Data.Text (Text)
importGQLDocumentWithNamespace "schema.gql"
rootResolver :: GQLRootResolver IO () () (Query IORes) () ()
rootResolver =
GQLRootResolver {queryResolver = return Query {deity}, mutationResolver = pure (), subscriptionResolver = pure ()}
GQLRootResolver
{queryResolver = return Query {queryDeity}, mutationResolver = pure (), subscriptionResolver = pure ()}
where
deity DeityArgs {uid} = pure Deity {name, power}
queryDeity QueryDeityArgs {queryDeityArgsName} = pure Deity {deityName, deityPower}
where
name _ = pure "Morpheus"
power _ = pure (Just "Shapeshifting")
deityName _ = pure "Morpheus"
deityPower _ = pure (Just "Shapeshifting")
gqlApi :: ByteString -> IO ByteString
gqlApi = interpreter rootResolver
api :: ByteString -> IO ByteString
api = interpreter rootResolver
```

Template Haskell Generates types: `Query` , `Deity`, `DeityArgs`, that can be used by `rootResolver`

generated types are not compatible with `Mutation`, `Subscription`,
they can be used only in `Query`, but this issue will be fixed in next release
`importGQLDocumentWithNamespace` will generate Types with namespaced fields. if you don't need napespacing use `importGQLDocument`

### with Native Haskell Types

Expand All @@ -83,7 +106,7 @@ which derives the `Generic` typeclass. Lazily resolvable fields on this `Query`
```haskell
data Query = Query
{ deity :: DeityArgs -> IORes Deity
} deriving (Generic)
} deriving (Generic, GQLType)
data Deity = Deity
{ fullName :: Text -- Non-Nullable Field
Expand Down Expand Up @@ -264,7 +287,7 @@ Just exchange deriving `GQLQuery` for `GQLMutation` and declare them separately
```haskell
newtype Mutation = Mutation
{ createDeity :: Form -> IOMutRes Deity
} deriving (Generic)
} deriving (Generic, GQLType)
createDeityMutation :: Form -> IOMutRes Deity
createDeityMutation = ...
Expand Down Expand Up @@ -302,15 +325,15 @@ data Content
newtype Query = Query
{ deity :: () -> IORes Deity
} deriving (Generic)
} deriving (Generic, GQLType)
newtype Mutation = Mutation
{ createDeity :: () -> IOMutRes Channel Content Deity
} deriving (Generic)
} deriving (Generic, GQLType)
newtype Subscription = Subscription
{ newDeity :: () -> IOSubRes Channel Content Deity
} deriving (Generic)
} deriving (Generic, GQLType)
rootResolver :: GQLRootResolver IO Channel Content Query Mutation Subscription
rootResolver =
Expand All @@ -322,11 +345,11 @@ rootResolver =
where
fetchDeity = resolver $ dbDeity "" Nothing
createDeity _args = toMutResolver [Event {channels = [ChannelA], content = ContentA 1}] fetchDeity
newDeity _args = Event {channels = [ChannelA], content}
newDeity _args = SubResolver {subChannels = [ChannelA], subResolver}
where
content (Event [ChannelA] (ContentA _value)) = resolver $ dbDeity "" Nothing -- resolve New State
content (Event [ChannelA] (ContentB value)) = resolver $ dbDeity value Nothing -- resolve New State
content _ = fetchDeity -- Resolve Old State
subResolver (Event [ChannelA] (ContentA _value)) = resolver $ dbDeity "" Nothing -- resolve New State
subResolver (Event [ChannelA] (ContentB value)) = resolver $ dbDeity value Nothing -- resolve New State
subResolver _ = fetchDeity -- Resolve Old State
```

## Morpheus `GraphQL Client` with Template haskell QuasiQuotes
Expand Down Expand Up @@ -401,7 +424,6 @@ Morpheus is written and maintained by [_nalchevanidze_](https://github.com/nalch
- Medium future:
- Stabilize API
- Specification-isomorphic introspection
- Specification-isomorphic error handling
- Long term:
- Support all possible GQL features
Expand Down
2 changes: 1 addition & 1 deletion examples/TH/Simple.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ rootResolver =
GQLRootResolver
{queryResolver = return Query {queryDeity}, mutationResolver = pure (), subscriptionResolver = pure ()}
where
queryDeity _deityArgs = pure Deity {deityName, deityPower}
queryDeity QueryDeityArgs {queryDeityArgsName} = pure Deity {deityName, deityPower}
where
deityName _ = pure "Morpheus"
deityPower _ = pure (Just "Shapeshifting")
Expand Down
2 changes: 1 addition & 1 deletion examples/TH/simple.gql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type Query {
deity(uid: String!): Deity!
deity(name: String!): Deity!
}

type Deity {
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: morpheus-graphql
version: 0.2.2
version: 0.3.0
github: "nalchevanidze/morpheus-graphql"
license: BSD3
author: "Daviti Nalchevanidze"
Expand Down

0 comments on commit c3fc385

Please sign in to comment.