summaryrefslogtreecommitdiff
path: root/src/Language/GraphQL
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2021-09-21 09:37:57 +0200
committerEugen Wissner <belka@caraus.de>2021-09-21 09:37:57 +0200
commita3f18932bd00661f7ecd2da724461d99a2d540ae (patch)
treec4e01aac9e3399ec942bcb5746e0e7c79d7a74b0 /src/Language/GraphQL
parent60d11678398e8215ffc3d9c92d003453f843162b (diff)
downloadgraphql-a3f18932bd00661f7ecd2da724461d99a2d540ae.tar.gz
Add TH module with gql quasi quoter
Diffstat (limited to 'src/Language/GraphQL')
-rw-r--r--src/Language/GraphQL/TH.hs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/Language/GraphQL/TH.hs b/src/Language/GraphQL/TH.hs
new file mode 100644
index 0000000..02dd7d6
--- /dev/null
+++ b/src/Language/GraphQL/TH.hs
@@ -0,0 +1,35 @@
+{- This Source Code Form is subject to the terms of the Mozilla Public License,
+ v. 2.0. If a copy of the MPL was not distributed with this file, You can
+ obtain one at https://mozilla.org/MPL/2.0/. -}
+
+-- | Template Haskell helpers.
+module Language.GraphQL.TH
+ ( gql
+ ) where
+
+import Language.Haskell.TH.Quote (QuasiQuoter(..))
+import Language.Haskell.TH (Exp(..), Lit(..))
+
+stripIndentation :: String -> String
+stripIndentation code = unlines
+ $ reverse
+ $ dropWhile null
+ $ reverse
+ $ indent spaces <$> lines withoutLeadingNewlines
+ where
+ indent 0 xs = xs
+ indent count (' ' : xs) = indent (count - 1) xs
+ indent _ xs = xs
+ withoutLeadingNewlines = dropWhile (== '\n') code
+ spaces = length $ takeWhile (== ' ') withoutLeadingNewlines
+
+gql :: QuasiQuoter
+gql = QuasiQuoter
+ { quoteExp = pure . LitE . StringL . stripIndentation
+ , quotePat = const
+ $ fail "Illegal gql QuasiQuote (allowed as expression only, used as a pattern)"
+ , quoteType = const
+ $ fail "Illegal gql QuasiQuote (allowed as expression only, used as a type)"
+ , quoteDec = const
+ $ fail "Illegal gql QuasiQuote (allowed as expression only, used as a declaration)"
+ }