在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):morpheusgraphql/morpheus-graphql开源软件地址(OpenSource Url):https://github.com/morpheusgraphql/morpheus-graphql开源编程语言(OpenSource Language):Haskell 65.7%开源软件介绍(OpenSource Introduction):Morpheus GraphQLBuild GraphQL APIs with your favorite functional language! Morpheus GraphQL (Server & Client) helps you to build GraphQL APIs in Haskell with native Haskell types. Morpheus will convert your Haskell types to a GraphQL schema and all your resolvers are just native Haskell functions. Morpheus GraphQL can also convert your GraphQL Schema or Query to Haskell types and validate them in compile time. Morpheus is still in an early stage of development, so any feedback is more than welcome, and we appreciate any contribution! Just open an issue here on GitHub, or join our Slack channel to get in touch. Please note that this readme file provides only a brief introduction to the library. If you are interested in more advanced topics, visit Docs. Getting StartedSetupTo get started with Morpheus, you first need to add it to your project's dependencies, as follows (assuming you're using hpack): package.yml dependencies:
- morpheus-graphql Additionally, you should tell stack which version to pick: stack.yml resolver: lts-16.2
extra-deps:
- morpheus-graphql-0.19.0
- morpheus-graphql-app-0.19.0
- morpheus-graphql-core-0.19.0 Building your first GraphQL APIwith GraphQL syntaxschema.gql type Query {
deity(name: String! = "Morpheus"): Deity!
}
"""
Description for Deity
"""
type Deity {
"""
Description for name
"""
name: String!
power: String @deprecated(reason: "some reason for")
} API.hs {-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
module API (api) where
import Data.ByteString.Lazy.Char8 (ByteString)
import Data.Morpheus (interpreter)
import Data.Morpheus.Document (importGQLDocument)
import Data.Morpheus.Types (RootResolver (..), Undefined (..))
import Data.Text (Text)
importGQLDocument "schema.gql"
rootResolver :: RootResolver IO () Query Undefined Undefined
rootResolver =
RootResolver
{ queryResolver = Query {deity},
mutationResolver = Undefined,
subscriptionResolver = Undefined
}
where
deity DeityArgs {name} =
pure
Deity
{ name = pure name,
power = pure (Just "Shapeshifting")
}
api :: ByteString -> IO ByteString
api = interpreter rootResolver Template Haskell Generates types:
with Native Haskell TypesTo define a GraphQL API with Morpheus we start by defining the API Schema as a native Haskell data type,
which derives the data Query m = Query
{ deity :: DeityArgs -> m Deity
} deriving (Generic, GQLType)
data Deity = Deity
{ fullName :: Text -- Non-Nullable Field
, power :: Maybe Text -- Nullable Field
} deriving (Generic, GQLType)
data DeityArgs = DeityArgs
{ name :: Text -- Required Argument
, mythology :: Maybe Text -- Optional Argument
} deriving (Generic, GQLType) For each field in the In above example, the field of data DeityArgs = DeityArgs
{ name :: Text -- Required Argument
, mythology :: Maybe Text -- Optional Argument
, type' :: Text
} deriving (Generic, GQLType) The field name in the final request will be resolveDeity :: DeityArgs -> ResolverQ () IO Deity
resolveDeity DeityArgs { name, mythology } = liftEither $ dbDeity name mythology
askDB :: Text -> Maybe Text -> IO (Either String Deity)
askDB = ... To make this rootResolver :: RootResolver IO () Query Undefined Undefined
rootResolver =
RootResolver
{ queryResolver = Query {deity = resolveDeity}
, mutationResolver = Undefined
, subscriptionResolver = Undefined
}
gqlApi :: ByteString -> IO ByteString
gqlApi = interpreter rootResolver As you can see, the API is defined as main :: IO ()
main = scotty 3000 $ post "/api" $ raw =<< (liftIO . gqlApi =<< body) If we now send a POST request to query GetDeity {
deity (name: "Morpheus") {
fullName
power
}
} our query will be resolved! {
"data": {
"deity": {
"fullName": "Morpheus",
"power": "Shapeshifting"
}
}
} Serverless ExampleIf you are interested in creating a Mythology API is deployed on : api.morpheusgraphql.com where you can test it with ShowcaseBelow are the list of projects using Morpheus GraphQL. If you want to start using Morpheus GraphQL, they are good templates to begin with.
Edit this section and send PR if you want to share your project. AboutThe nameMorpheus is the greek god of sleep and dreams whose name comes from the greek word μορφή meaning form or shape. He is said to be able to mimic different forms and GraphQL is good at doing exactly that: Transforming data in the shape of many different APIs. TeamMorpheus is written and maintained by nalchevanidze Roadmap
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论