graphql-utils (empty) → 0.1.0.0
raw patch · 3 files changed
+165/−0 lines, 3 filesdep +aesondep +aeson-helperdep +base
Dependencies added: aeson, aeson-helper, base, graphql, text, unordered-containers, vector
Files
- LICENSE +30/−0
- graphql-utils.cabal +30/−0
- src/Data/GraphQL/Utils.hs +105/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Li Meng Jun (c) 2017++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Li Meng Jun nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ graphql-utils.cabal view
@@ -0,0 +1,30 @@+name: graphql-utils+version: 0.1.0.0+synopsis: GraphQL Utils+description: Easy use graphql on aeson library+homepage: https://github.com/Lupino/yuntan-common/tree/master/graphql-utils#readme+license: BSD3+license-file: LICENSE+author: Li Meng Jun+maintainer: lmjubuntu@gmail.com+copyright: MIT+category: GraphQL, Data, Utils+build-type: Simple+-- extra-source-files:+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: Data.GraphQL.Utils+ build-depends: base >= 4.7 && < 5+ , aeson+ , unordered-containers+ , text+ , vector+ , graphql+ , aeson-helper+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/Lupino/yuntan-common
+ src/Data/GraphQL/Utils.hs view
@@ -0,0 +1,105 @@+{-# LANGUAGE OverloadedStrings #-}++module Data.GraphQL.Utils+ ( get+ , getInt+ , getFloat+ , getText+ , getEnum+ , getBool+ , getObject+ , getList+ , value+ , value'+ , pick+ ) where++import Control.Applicative (Alternative (..))+import qualified Data.Aeson as A (Value (..))+import qualified Data.Aeson.Helper as J (pick)+import Data.GraphQL.AST (Name)+import Data.GraphQL.AST.Core (ObjectField)+import Data.GraphQL.Schema (Argument (..), Resolver, Value (..),+ array, object, scalar, scalarA)+import qualified Data.HashMap.Strict as HM (toList)+import Data.Maybe (fromMaybe, mapMaybe)+import Data.Text (Text)+import qualified Data.Vector as V (Vector, head, null, toList)++get :: Name -> [Argument] -> Maybe Value+get _ [] = Nothing+get k (Argument n v:xs) | k == n = Just v+ | otherwise = get k xs++getInt :: Num a => Name -> [Argument] -> Maybe a+getInt n argv =+ case get n argv of+ (Just (ValueInt v)) -> Just $ fromIntegral v+ _ -> Nothing++getFloat :: Name -> [Argument] -> Maybe Double+getFloat n argv =+ case get n argv of+ (Just (ValueFloat v)) -> Just v+ _ -> Nothing++getBool :: Name -> [Argument] -> Maybe Bool+getBool n argv =+ case get n argv of+ (Just (ValueBoolean v)) -> Just v+ _ -> Nothing++getText :: Name -> [Argument] -> Maybe Text+getText n argv =+ case get n argv of+ (Just (ValueString v)) -> Just v+ _ -> Nothing++getEnum :: Name -> [Argument] -> Maybe Name+getEnum n argv =+ case get n argv of+ (Just (ValueEnum v)) -> Just v+ _ -> Nothing++getObject :: Name -> [Argument] -> Maybe [ObjectField]+getObject n argv =+ case get n argv of+ (Just (ValueObject v)) -> Just v+ _ -> Nothing++getList :: Name -> [Argument] -> Maybe [Value]+getList n argv =+ case get n argv of+ (Just (ValueList v)) -> Just v+ _ -> Nothing++value :: Alternative f => Name -> A.Value -> Resolver f+value k (A.Object v) = object k . listToResolver $ HM.toList v+value k (A.Array v) = if isO v then array k (map value' $ V.toList v)+ else scalar k v+value k v = scalar k v++isOv :: A.Value -> Bool+isOv (A.Object _) = True+isOv _ = False++isO :: V.Vector A.Value -> Bool+isO v | V.null v = False+ | otherwise = isOv $ V.head v++value' :: Alternative f => A.Value -> [Resolver f]+value' (A.Object v) = listToResolver $ HM.toList v+value' _ = []++listToResolver :: Alternative f => [(Text, A.Value)] -> [Resolver f]+listToResolver [] = []+listToResolver ((k, v):xs) = value k v : listToResolver xs++pick :: Alternative f => Name -> A.Value -> Resolver f+pick n v = scalarA n $ \args -> pure $ J.pick (keys args) v+ where keys :: [Argument] -> [Text]+ keys args = mapMaybe getText' (fromMaybe [] $ getList "keys" args)++ getText' :: Value -> Maybe Text+ getText' (ValueString k) = Just k+ getText' _ = Nothing