2015-09-22 14:02:12 +02:00
|
|
|
{-# LANGUAGE CPP #-}
|
2015-09-21 18:26:22 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2015-09-22 14:23:18 +02:00
|
|
|
module Data.GraphQL.Encoder where
|
2015-09-21 18:26:22 +02:00
|
|
|
|
2015-09-22 14:02:12 +02:00
|
|
|
#if !MIN_VERSION_base(4,8,0)
|
|
|
|
import Control.Applicative ((<$>))
|
|
|
|
import Data.Monoid (Monoid, mconcat, mempty)
|
|
|
|
#endif
|
2015-09-21 18:26:22 +02:00
|
|
|
import Data.Monoid ((<>))
|
|
|
|
|
2015-09-22 10:45:14 +02:00
|
|
|
import Data.Text (Text, cons, intercalate, pack, snoc)
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
import Data.GraphQL.AST
|
|
|
|
|
|
|
|
-- * Document
|
|
|
|
|
2015-09-22 13:53:37 +02:00
|
|
|
-- TODO: Use query shorthand
|
2015-09-21 18:26:22 +02:00
|
|
|
document :: Document -> Text
|
2015-09-22 13:53:37 +02:00
|
|
|
document (Document defs) = (`snoc` '\n') . mconcat $ definition <$> defs
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
definition :: Definition -> Text
|
|
|
|
definition (DefinitionOperation x) = operationDefinition x
|
|
|
|
definition (DefinitionFragment x) = fragmentDefinition x
|
|
|
|
definition (DefinitionType x) = typeDefinition x
|
|
|
|
|
|
|
|
operationDefinition :: OperationDefinition -> Text
|
|
|
|
operationDefinition (Query n) = "query " <> node n
|
|
|
|
operationDefinition (Mutation n) = "mutation " <> node n
|
|
|
|
|
|
|
|
node :: Node -> Text
|
|
|
|
node (Node name vds ds ss) =
|
|
|
|
name
|
|
|
|
<> optempty variableDefinitions vds
|
2015-09-22 10:45:14 +02:00
|
|
|
<> optempty directives ds
|
2015-09-21 18:26:22 +02:00
|
|
|
<> selectionSet ss
|
|
|
|
|
|
|
|
variableDefinitions :: [VariableDefinition] -> Text
|
2015-09-22 10:45:14 +02:00
|
|
|
variableDefinitions = parensCommas variableDefinition
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
variableDefinition :: VariableDefinition -> Text
|
|
|
|
variableDefinition (VariableDefinition var ty dv) =
|
2015-09-22 10:45:14 +02:00
|
|
|
variable var <> ":" <> type_ ty <> maybe mempty defaultValue dv
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
defaultValue :: DefaultValue -> Text
|
2015-09-22 10:45:14 +02:00
|
|
|
defaultValue val = "=" <> value val
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
variable :: Variable -> Text
|
|
|
|
variable (Variable name) = "$" <> name
|
|
|
|
|
|
|
|
selectionSet :: SelectionSet -> Text
|
2015-09-22 10:45:14 +02:00
|
|
|
selectionSet = bracesCommas selection
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
selection :: Selection -> Text
|
|
|
|
selection (SelectionField x) = field x
|
|
|
|
selection (SelectionInlineFragment x) = inlineFragment x
|
|
|
|
selection (SelectionFragmentSpread x) = fragmentSpread x
|
|
|
|
|
|
|
|
field :: Field -> Text
|
|
|
|
field (Field alias name args ds ss) =
|
2015-09-22 11:16:36 +02:00
|
|
|
optempty (`snoc` ':') alias
|
2015-09-21 18:26:22 +02:00
|
|
|
<> name
|
|
|
|
<> optempty arguments args
|
2015-09-22 10:45:14 +02:00
|
|
|
<> optempty directives ds
|
|
|
|
<> optempty selectionSet ss
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
arguments :: [Argument] -> Text
|
2015-09-22 10:45:14 +02:00
|
|
|
arguments = parensCommas argument
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
argument :: Argument -> Text
|
2015-09-22 10:45:14 +02:00
|
|
|
argument (Argument name v) = name <> ":" <> value v
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
-- * Fragments
|
|
|
|
|
|
|
|
fragmentSpread :: FragmentSpread -> Text
|
|
|
|
fragmentSpread (FragmentSpread name ds) =
|
2015-09-22 10:45:14 +02:00
|
|
|
"..." <> name <> optempty directives ds
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
inlineFragment :: InlineFragment -> Text
|
|
|
|
inlineFragment (InlineFragment (NamedType tc) ds ss) =
|
2015-09-22 10:45:14 +02:00
|
|
|
"... on " <> tc
|
|
|
|
<> optempty directives ds
|
|
|
|
<> optempty selectionSet ss
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
fragmentDefinition :: FragmentDefinition -> Text
|
|
|
|
fragmentDefinition (FragmentDefinition name (NamedType tc) ds ss) =
|
|
|
|
"fragment " <> name <> " on " <> tc
|
2015-09-22 10:45:14 +02:00
|
|
|
<> optempty directives ds
|
|
|
|
<> selectionSet ss
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
-- * Values
|
|
|
|
|
|
|
|
value :: Value -> Text
|
|
|
|
value (ValueVariable x) = variable x
|
|
|
|
-- TODO: This will be replaced with `decimal` Buidler
|
|
|
|
value (ValueInt x) = pack $ show x
|
|
|
|
-- TODO: This will be replaced with `decimal` Buidler
|
|
|
|
value (ValueFloat x) = pack $ show x
|
|
|
|
value (ValueBoolean x) = booleanValue x
|
|
|
|
value (ValueString x) = stringValue x
|
|
|
|
value (ValueEnum x) = x
|
|
|
|
value (ValueList x) = listValue x
|
|
|
|
value (ValueObject x) = objectValue x
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
listValue :: ListValue -> Text
|
2015-09-22 10:45:14 +02:00
|
|
|
listValue (ListValue vs) = bracketsCommas value vs
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
objectValue :: ObjectValue -> Text
|
2015-09-22 10:45:14 +02:00
|
|
|
objectValue (ObjectValue ofs) = bracesCommas objectField ofs
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
objectField :: ObjectField -> Text
|
2015-09-22 10:45:14 +02:00
|
|
|
objectField (ObjectField name v) = name <> ":" <> value v
|
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
|
|
|
|
|
|
|
directives :: [Directive] -> Text
|
2015-09-22 10:45:14 +02:00
|
|
|
directives = spaces directive
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
directive :: Directive -> Text
|
|
|
|
directive (Directive name args) = "@" <> name <> optempty arguments args
|
|
|
|
|
|
|
|
-- * Type Reference
|
|
|
|
|
|
|
|
type_ :: Type -> Text
|
|
|
|
type_ (TypeNamed (NamedType x)) = x
|
|
|
|
type_ (TypeList x) = listType x
|
|
|
|
type_ (TypeNonNull x) = nonNullType x
|
|
|
|
|
|
|
|
namedType :: NamedType -> Text
|
|
|
|
namedType (NamedType name) = name
|
|
|
|
|
|
|
|
listType :: ListType -> Text
|
2015-09-22 10:45:14 +02:00
|
|
|
listType (ListType ty) = brackets (type_ ty)
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
nonNullType :: NonNullType -> Text
|
|
|
|
nonNullType (NonNullTypeNamed (NamedType x)) = x <> "!"
|
|
|
|
nonNullType (NonNullTypeList x) = listType x <> "!"
|
|
|
|
|
|
|
|
typeDefinition :: TypeDefinition -> Text
|
|
|
|
typeDefinition (TypeDefinitionObject x) = objectTypeDefinition x
|
|
|
|
typeDefinition (TypeDefinitionInterface x) = interfaceTypeDefinition x
|
|
|
|
typeDefinition (TypeDefinitionUnion x) = unionTypeDefinition x
|
|
|
|
typeDefinition (TypeDefinitionScalar x) = scalarTypeDefinition x
|
|
|
|
typeDefinition (TypeDefinitionEnum x) = enumTypeDefinition x
|
|
|
|
typeDefinition (TypeDefinitionInputObject x) = inputObjectTypeDefinition x
|
|
|
|
typeDefinition (TypeDefinitionTypeExtension x) = typeExtensionDefinition x
|
|
|
|
|
|
|
|
objectTypeDefinition :: ObjectTypeDefinition -> Text
|
|
|
|
objectTypeDefinition (ObjectTypeDefinition name ifaces fds) =
|
|
|
|
"type " <> name
|
2015-09-22 10:45:14 +02:00
|
|
|
<> optempty (spaced . interfaces) ifaces
|
|
|
|
<> optempty fieldDefinitions fds
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
interfaces :: Interfaces -> Text
|
2015-09-22 10:45:14 +02:00
|
|
|
interfaces = ("implements " <>) . spaces namedType
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
fieldDefinitions :: [FieldDefinition] -> Text
|
2015-09-22 10:45:14 +02:00
|
|
|
fieldDefinitions = bracesCommas fieldDefinition
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
fieldDefinition :: FieldDefinition -> Text
|
|
|
|
fieldDefinition (FieldDefinition name args ty) =
|
|
|
|
name <> optempty argumentsDefinition args
|
2015-09-22 10:45:14 +02:00
|
|
|
<> ":"
|
2015-09-21 18:26:22 +02:00
|
|
|
<> type_ ty
|
|
|
|
|
|
|
|
argumentsDefinition :: ArgumentsDefinition -> Text
|
2015-09-22 10:45:14 +02:00
|
|
|
argumentsDefinition = parensCommas inputValueDefinition
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
interfaceTypeDefinition :: InterfaceTypeDefinition -> Text
|
|
|
|
interfaceTypeDefinition (InterfaceTypeDefinition name fds) =
|
2015-09-22 10:45:14 +02:00
|
|
|
"interface " <> name <> fieldDefinitions fds
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
unionTypeDefinition :: UnionTypeDefinition -> Text
|
|
|
|
unionTypeDefinition (UnionTypeDefinition name ums) =
|
2015-09-22 10:45:14 +02:00
|
|
|
"union " <> name <> "=" <> unionMembers ums
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
unionMembers :: [NamedType] -> Text
|
2015-09-22 10:45:14 +02:00
|
|
|
unionMembers = intercalate "|" . fmap namedType
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
scalarTypeDefinition :: ScalarTypeDefinition -> Text
|
|
|
|
scalarTypeDefinition (ScalarTypeDefinition name) = "scalar " <> name
|
|
|
|
|
|
|
|
enumTypeDefinition :: EnumTypeDefinition -> Text
|
|
|
|
enumTypeDefinition (EnumTypeDefinition name evds) =
|
|
|
|
"enum " <> name
|
2015-09-22 10:45:14 +02:00
|
|
|
<> bracesCommas enumValueDefinition evds
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
enumValueDefinition :: EnumValueDefinition -> Text
|
|
|
|
enumValueDefinition (EnumValueDefinition name) = name
|
|
|
|
|
|
|
|
inputObjectTypeDefinition :: InputObjectTypeDefinition -> Text
|
|
|
|
inputObjectTypeDefinition (InputObjectTypeDefinition name ivds) =
|
2015-09-22 10:45:14 +02:00
|
|
|
"input " <> name <> inputValueDefinitions ivds
|
|
|
|
|
|
|
|
inputValueDefinitions :: [InputValueDefinition] -> Text
|
|
|
|
inputValueDefinitions = bracesCommas inputValueDefinition
|
|
|
|
|
|
|
|
inputValueDefinition :: InputValueDefinition -> Text
|
|
|
|
inputValueDefinition (InputValueDefinition name ty dv) =
|
|
|
|
name <> ":" <> type_ ty <> maybe mempty defaultValue dv
|
2015-09-21 18:26:22 +02:00
|
|
|
|
|
|
|
typeExtensionDefinition :: TypeExtensionDefinition -> Text
|
|
|
|
typeExtensionDefinition (TypeExtensionDefinition otd) =
|
|
|
|
"extend " <> objectTypeDefinition otd
|
|
|
|
|
|
|
|
-- * Internal
|
|
|
|
|
2015-09-22 10:45:14 +02:00
|
|
|
spaced :: Text -> Text
|
|
|
|
spaced = cons '\SP'
|
|
|
|
|
|
|
|
between :: Char -> Char -> Text -> Text
|
|
|
|
between open close = cons open . (`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 '"' '"'
|
|
|
|
|
2015-09-22 10:45:14 +02:00
|
|
|
spaces :: (a -> Text) -> [a] -> Text
|
|
|
|
spaces f = intercalate "\SP" . fmap f
|
|
|
|
|
|
|
|
parensCommas :: (a -> Text) -> [a] -> Text
|
|
|
|
parensCommas f = parens . intercalate "," . fmap f
|
|
|
|
|
|
|
|
bracketsCommas :: (a -> Text) -> [a] -> Text
|
|
|
|
bracketsCommas f = brackets . intercalate "," . fmap f
|
|
|
|
|
|
|
|
bracesCommas :: (a -> Text) -> [a] -> Text
|
|
|
|
bracesCommas f = braces . intercalate "," . fmap f
|
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
|