2015-09-21 18:26:22 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2019-08-02 13:52:51 +02:00
|
|
|
{-# LANGUAGE ExplicitForAll #-}
|
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | This module defines a printer for the @GraphQL@ language.
|
2019-07-14 05:58:05 +02:00
|
|
|
module Language.GraphQL.Encoder
|
2019-07-31 05:40:17 +02:00
|
|
|
( Formatter(..)
|
|
|
|
, definition
|
2019-07-27 07:19:21 +02:00
|
|
|
, document
|
2019-07-14 05:58:05 +02:00
|
|
|
) where
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
import Data.Foldable (fold)
|
2015-09-21 18:26:22 +02:00
|
|
|
import Data.Monoid ((<>))
|
2017-01-28 18:15:14 +01:00
|
|
|
import qualified Data.List.NonEmpty as NonEmpty (toList)
|
2019-07-27 07:19:21 +02:00
|
|
|
import Data.Text (Text, pack)
|
|
|
|
import qualified Data.Text as Text
|
2019-07-07 06:31:53 +02:00
|
|
|
import Language.GraphQL.AST
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-07-31 05:40:17 +02:00
|
|
|
-- | Instructs the encoder whether a GraphQL should be minified or pretty
|
|
|
|
-- printed.
|
|
|
|
data Formatter
|
|
|
|
= Minified
|
|
|
|
| Pretty Int
|
|
|
|
|
2019-07-27 07:19:21 +02:00
|
|
|
-- | Converts a 'Document' into a string.
|
2019-07-31 05:40:17 +02:00
|
|
|
document :: Formatter -> Document -> Text
|
|
|
|
document formatter defs
|
|
|
|
| Pretty _ <- formatter = Text.intercalate "\n" encodeDocument
|
|
|
|
| Minified <-formatter = Text.snoc (mconcat encodeDocument) '\n'
|
|
|
|
where
|
|
|
|
encodeDocument = NonEmpty.toList $ definition formatter <$> defs
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-07-27 07:19:21 +02:00
|
|
|
-- | Converts a 'Definition' into a string.
|
2019-07-31 05:40:17 +02:00
|
|
|
definition :: Formatter -> Definition -> Text
|
|
|
|
definition formatter x
|
|
|
|
| Pretty _ <- formatter = Text.snoc (encodeDefinition x) '\n'
|
|
|
|
| Minified <- formatter = encodeDefinition x
|
2019-07-27 07:19:21 +02:00
|
|
|
where
|
|
|
|
encodeDefinition (DefinitionOperation operation)
|
2019-07-31 05:40:17 +02:00
|
|
|
= operationDefinition formatter operation
|
2019-07-27 07:19:21 +02:00
|
|
|
encodeDefinition (DefinitionFragment fragment)
|
2019-07-31 05:40:17 +02:00
|
|
|
= fragmentDefinition formatter fragment
|
|
|
|
|
|
|
|
operationDefinition :: Formatter -> OperationDefinition -> Text
|
|
|
|
operationDefinition formatter (OperationSelectionSet sels)
|
|
|
|
= selectionSet formatter sels
|
|
|
|
operationDefinition formatter (OperationDefinition Query name vars dirs sels)
|
2019-08-02 13:52:51 +02:00
|
|
|
= "query " <> node formatter name vars dirs sels
|
2019-07-31 05:40:17 +02:00
|
|
|
operationDefinition formatter (OperationDefinition Mutation name vars dirs sels)
|
2019-08-02 13:52:51 +02:00
|
|
|
= "mutation " <> node formatter name vars dirs sels
|
2019-07-31 05:40:17 +02:00
|
|
|
|
|
|
|
node :: Formatter
|
2019-08-02 13:52:51 +02:00
|
|
|
-> Maybe Name
|
2019-07-31 05:40:17 +02:00
|
|
|
-> VariableDefinitions
|
|
|
|
-> Directives
|
|
|
|
-> SelectionSet
|
|
|
|
-> Text
|
|
|
|
node formatter name vars dirs sels
|
2019-08-02 13:52:51 +02:00
|
|
|
= fold name
|
|
|
|
<> optempty (variableDefinitions formatter) vars
|
|
|
|
<> optempty (directives formatter) dirs
|
|
|
|
<> eitherFormat formatter " " mempty
|
2019-07-31 05:40:17 +02:00
|
|
|
<> selectionSet formatter sels
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
variableDefinitions :: Formatter -> [VariableDefinition] -> Text
|
|
|
|
variableDefinitions formatter
|
|
|
|
= parensCommas formatter $ variableDefinition formatter
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
variableDefinition :: Formatter -> VariableDefinition -> Text
|
|
|
|
variableDefinition formatter (VariableDefinition var ty dv)
|
|
|
|
= variable var
|
|
|
|
<> eitherFormat formatter ": " ":"
|
|
|
|
<> type_ ty
|
|
|
|
<> maybe mempty (defaultValue formatter) dv
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
defaultValue :: Formatter -> Value -> Text
|
|
|
|
defaultValue formatter val
|
|
|
|
= eitherFormat formatter " = " "="
|
|
|
|
<> value formatter val
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-07-18 05:10:02 +02:00
|
|
|
variable :: Name -> Text
|
2017-01-28 18:15:14 +01:00
|
|
|
variable var = "$" <> var
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-07-31 05:40:17 +02:00
|
|
|
selectionSet :: Formatter -> SelectionSet -> Text
|
|
|
|
selectionSet formatter@(Pretty _) = bracesNewLines (selection formatter) . NonEmpty.toList
|
|
|
|
selectionSet Minified = bracesCommas (selection Minified) . NonEmpty.toList
|
2017-01-28 18:15:14 +01:00
|
|
|
|
2019-07-31 05:40:17 +02:00
|
|
|
selectionSetOpt :: Formatter -> SelectionSetOpt -> Text
|
|
|
|
selectionSetOpt formatter@(Pretty _) = bracesNewLines $ selection formatter
|
|
|
|
selectionSetOpt Minified = bracesCommas $ selection Minified
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-07-31 05:40:17 +02:00
|
|
|
selection :: Formatter -> Selection -> Text
|
|
|
|
selection formatter (SelectionField x) = field formatter x
|
|
|
|
selection formatter (SelectionInlineFragment x) = inlineFragment formatter x
|
2019-08-02 13:52:51 +02:00
|
|
|
selection formatter (SelectionFragmentSpread x) = fragmentSpread formatter x
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-07-31 05:40:17 +02:00
|
|
|
field :: Formatter -> Field -> Text
|
2019-08-02 13:52:51 +02:00
|
|
|
field formatter (Field alias name args dirs selso)
|
|
|
|
= optempty (`Text.append` colon) (fold alias)
|
2015-09-21 18:26:22 +02:00
|
|
|
<> name
|
2019-08-02 13:52:51 +02:00
|
|
|
<> optempty (arguments formatter) args
|
|
|
|
<> optempty (directives formatter) dirs
|
|
|
|
<> selectionSetOpt'
|
|
|
|
where
|
|
|
|
colon = eitherFormat formatter ": " ":"
|
|
|
|
selectionSetOpt'
|
|
|
|
| null selso = mempty
|
|
|
|
| otherwise = eitherFormat formatter " " mempty <> selectionSetOpt formatter selso
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
arguments :: Formatter -> [Argument] -> Text
|
|
|
|
arguments formatter = parensCommas formatter $ argument formatter
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
argument :: Formatter -> Argument -> Text
|
|
|
|
argument formatter (Argument name v)
|
|
|
|
= name
|
|
|
|
<> eitherFormat formatter ": " ":"
|
|
|
|
<> value formatter v
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
-- * Fragments
|
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
fragmentSpread :: Formatter -> FragmentSpread -> Text
|
|
|
|
fragmentSpread formatter (FragmentSpread name ds) =
|
|
|
|
"..." <> name <> optempty (directives formatter) ds
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-07-31 05:40:17 +02:00
|
|
|
inlineFragment :: Formatter -> InlineFragment -> Text
|
2019-08-02 13:52:51 +02:00
|
|
|
inlineFragment formatter (InlineFragment tc dirs sels)
|
|
|
|
= "... on " <> fold tc
|
|
|
|
<> directives formatter dirs
|
|
|
|
<> eitherFormat formatter " " mempty
|
|
|
|
<> selectionSet formatter sels
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-07-31 05:40:17 +02:00
|
|
|
fragmentDefinition :: Formatter -> FragmentDefinition -> Text
|
2019-08-02 13:52:51 +02:00
|
|
|
fragmentDefinition formatter (FragmentDefinition name tc dirs sels)
|
|
|
|
= "fragment " <> name <> " on " <> tc
|
|
|
|
<> optempty (directives formatter) dirs
|
|
|
|
<> eitherFormat formatter " " mempty
|
|
|
|
<> selectionSet formatter sels
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
-- * Values
|
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
value :: Formatter -> Value -> Text
|
|
|
|
value _ (ValueVariable x) = variable x
|
2017-01-28 18:15:14 +01:00
|
|
|
-- TODO: This will be replaced with `decimal` Builder
|
2019-08-02 13:52:51 +02:00
|
|
|
value _ (ValueInt x) = pack $ show x
|
2017-01-28 18:15:14 +01:00
|
|
|
-- TODO: This will be replaced with `decimal` Builder
|
2019-08-02 13:52:51 +02:00
|
|
|
value _ (ValueFloat x) = pack $ show x
|
|
|
|
value _ (ValueBoolean x) = booleanValue x
|
|
|
|
value _ ValueNull = mempty
|
|
|
|
value _ (ValueString x) = stringValue x
|
|
|
|
value _ (ValueEnum x) = x
|
|
|
|
value formatter (ValueList x) = listValue formatter x
|
|
|
|
value formatter (ValueObject x) = objectValue formatter x
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
booleanValue :: Bool -> Text
|
|
|
|
booleanValue True = "true"
|
|
|
|
booleanValue False = "false"
|
|
|
|
|
|
|
|
-- TODO: Escape characters
|
2016-02-22 13:59:38 +01:00
|
|
|
stringValue :: Text -> Text
|
|
|
|
stringValue = quotes
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
listValue :: Formatter -> [Value] -> Text
|
|
|
|
listValue formatter = bracketsCommas formatter $ value formatter
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
objectValue :: Formatter -> [ObjectField] -> Text
|
|
|
|
objectValue formatter = bracesCommas $ objectField formatter
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
objectField :: Formatter -> ObjectField -> Text
|
|
|
|
objectField formatter (ObjectField name v) = name <> colon <> value formatter v
|
|
|
|
where
|
|
|
|
colon
|
|
|
|
| Pretty _ <- formatter = ": "
|
|
|
|
| Minified <- formatter = ":"
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2015-09-22 10:45:14 +02:00
|
|
|
-- * Directives
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
directives :: Formatter -> [Directive] -> Text
|
|
|
|
directives formatter@(Pretty _) = Text.cons ' ' . spaces (directive formatter)
|
|
|
|
directives Minified = spaces (directive Minified)
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
directive :: Formatter -> Directive -> Text
|
|
|
|
directive formatter (Directive name args)
|
|
|
|
= "@" <> name <> optempty (arguments formatter) args
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
-- * Type Reference
|
|
|
|
|
|
|
|
type_ :: Type -> Text
|
2017-01-28 18:15:14 +01:00
|
|
|
type_ (TypeNamed x) = x
|
|
|
|
type_ (TypeList x) = listType x
|
2015-09-21 18:26:22 +02:00
|
|
|
type_ (TypeNonNull x) = nonNullType x
|
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
listType :: Type -> Text
|
|
|
|
listType x = brackets (type_ x)
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
nonNullType :: NonNullType -> Text
|
2017-01-28 18:15:14 +01:00
|
|
|
nonNullType (NonNullTypeNamed x) = x <> "!"
|
2015-09-21 18:26:22 +02:00
|
|
|
nonNullType (NonNullTypeList x) = listType x <> "!"
|
|
|
|
|
|
|
|
-- * Internal
|
|
|
|
|
2015-09-22 10:45:14 +02:00
|
|
|
between :: Char -> Char -> Text -> Text
|
2019-07-27 07:19:21 +02:00
|
|
|
between open close = Text.cons open . (`Text.snoc` close)
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
parens :: Text -> Text
|
2015-09-22 10:45:14 +02:00
|
|
|
parens = between '(' ')'
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
brackets :: Text -> Text
|
2015-09-22 10:45:14 +02:00
|
|
|
brackets = between '[' ']'
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2015-09-22 10:45:14 +02:00
|
|
|
braces :: Text -> Text
|
|
|
|
braces = between '{' '}'
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2015-09-22 13:53:37 +02:00
|
|
|
quotes :: Text -> Text
|
|
|
|
quotes = between '"' '"'
|
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
spaces :: forall a. (a -> Text) -> [a] -> Text
|
2019-07-27 07:19:21 +02:00
|
|
|
spaces f = Text.intercalate "\SP" . fmap f
|
2015-09-22 10:45:14 +02:00
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
parensCommas :: forall a. Formatter -> (a -> Text) -> [a] -> Text
|
|
|
|
parensCommas formatter f
|
|
|
|
= parens
|
|
|
|
. Text.intercalate (eitherFormat formatter ", " ",")
|
|
|
|
. fmap f
|
2015-09-22 10:45:14 +02:00
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
bracketsCommas :: Formatter -> (a -> Text) -> [a] -> Text
|
|
|
|
bracketsCommas formatter f
|
|
|
|
= brackets
|
|
|
|
. Text.intercalate (eitherFormat formatter ", " ",")
|
|
|
|
. fmap f
|
2015-09-22 10:45:14 +02:00
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
bracesCommas :: forall a. (a -> Text) -> [a] -> Text
|
2019-07-27 07:19:21 +02:00
|
|
|
bracesCommas f = braces . Text.intercalate "," . fmap f
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
bracesNewLines :: forall a. (a -> Text) -> [a] -> Text
|
2019-07-31 05:40:17 +02:00
|
|
|
bracesNewLines f xs = Text.append (Text.intercalate "\n" $ "{" : fmap f xs) "\n}"
|
|
|
|
|
2015-09-21 18:26:22 +02:00
|
|
|
optempty :: (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
|
|
|
|
optempty f xs = if xs == mempty then mempty else f xs
|
2019-08-02 13:52:51 +02:00
|
|
|
|
|
|
|
eitherFormat :: forall a. Formatter -> a -> a -> a
|
|
|
|
eitherFormat (Pretty _) pretty _ = pretty
|
|
|
|
eitherFormat Minified _ minified = minified
|