{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE Safe #-}

-- | This module defines a minifier and a printer for the @GraphQL@ language.
module Language.GraphQL.AST.Encoder
    ( Formatter
    , definition
    , directive
    , document
    , minified
    , operationType
    , pretty
    , type'
    , typeSystemDefinition
    , value
    ) where

import Data.Foldable (fold, Foldable (..))
import qualified Data.List.NonEmpty as NonEmpty
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Lazy as Lazy (Text)
import qualified Data.Text.Lazy as Lazy.Text
import Data.Text.Lazy.Builder (Builder)
import qualified Data.Text.Lazy.Builder as Builder
import Data.Text.Lazy.Builder.Int (decimal)
import Data.Text.Lazy.Builder.RealFloat (realFloat)
import qualified Language.GraphQL.AST.Document as Full
import qualified Language.GraphQL.AST.DirectiveLocation as DirectiveLocation

-- | Instructs the encoder whether the GraphQL document should be minified or
--   pretty printed.
--
--   Use 'pretty' or 'minified' to construct the formatter.
data Formatter
    = Minified
    | Pretty !Word

-- | Constructs a formatter for pretty printing.
pretty :: Formatter
pretty :: Formatter
pretty = Word -> Formatter
Pretty Word
0

-- | Constructs a formatter for minifying.
minified :: Formatter
minified :: Formatter
minified = Formatter
Minified

-- | Converts a Document' into a string.
document :: Formatter -> Full.Document -> Lazy.Text
document :: Formatter -> Document -> Text
document Formatter
formatter Document
defs
    | Pretty Word
_ <- Formatter
formatter = Text -> [Text] -> Text
Lazy.Text.intercalate Text
"\n" [Text]
encodeDocument
    | Formatter
Minified <-Formatter
formatter = Text -> Char -> Text
Lazy.Text.snoc (forall a. Monoid a => [a] -> a
mconcat [Text]
encodeDocument) Char
'\n'
  where
    encodeDocument :: [Text]
encodeDocument = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Definition -> [Text] -> [Text]
executableDefinition [] Document
defs
    executableDefinition :: Definition -> [Text] -> [Text]
executableDefinition (Full.ExecutableDefinition ExecutableDefinition
executableDefinition') [Text]
acc =
        Formatter -> ExecutableDefinition -> Text
definition Formatter
formatter ExecutableDefinition
executableDefinition' forall a. a -> [a] -> [a]
: [Text]
acc
    executableDefinition (Full.TypeSystemDefinition TypeSystemDefinition
typeSystemDefinition' Location
_location) [Text]
acc =
        Formatter -> TypeSystemDefinition -> Text
typeSystemDefinition Formatter
formatter TypeSystemDefinition
typeSystemDefinition' forall a. a -> [a] -> [a]
: [Text]
acc
    executableDefinition (Full.TypeSystemExtension TypeSystemExtension
typeSystemExtension' Location
_location) [Text]
acc =
        Formatter -> TypeSystemExtension -> Text
typeSystemExtension Formatter
formatter TypeSystemExtension
typeSystemExtension' forall a. a -> [a] -> [a]
: [Text]
acc

directiveLocation :: DirectiveLocation.DirectiveLocation -> Lazy.Text
directiveLocation :: DirectiveLocation -> Text
directiveLocation = String -> Text
Lazy.Text.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show

withLineBreak :: Formatter -> Lazy.Text.Text -> Lazy.Text.Text
withLineBreak :: Formatter -> Text -> Text
withLineBreak Formatter
formatter Text
encodeDefinition
    | Pretty Word
_ <- Formatter
formatter = Text -> Char -> Text
Lazy.Text.snoc Text
encodeDefinition Char
'\n'
    | Formatter
Minified <- Formatter
formatter = Text
encodeDefinition

typeSystemExtension :: Formatter -> Full.TypeSystemExtension -> Lazy.Text
typeSystemExtension :: Formatter -> TypeSystemExtension -> Text
typeSystemExtension Formatter
formatter = \case
    Full.SchemaExtension SchemaExtension
schemaExtension' ->
        Formatter -> SchemaExtension -> Text
schemaExtension Formatter
formatter SchemaExtension
schemaExtension'
    Full.TypeExtension TypeExtension
typeExtension' -> Formatter -> TypeExtension -> Text
typeExtension Formatter
formatter TypeExtension
typeExtension'

schemaExtension :: Formatter -> Full.SchemaExtension -> Lazy.Text
schemaExtension :: Formatter -> SchemaExtension -> Text
schemaExtension Formatter
formatter = \case
    Full.SchemaOperationExtension [Directive]
operationDirectives NonEmpty OperationTypeDefinition
operationTypeDefinitions' ->
        Formatter -> Text -> Text
withLineBreak Formatter
formatter
            forall a b. (a -> b) -> a -> b
$ Text
"extend schema "
            forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
operationDirectives
            forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> (a -> Text) -> [a] -> Text
bracesList Formatter
formatter (Formatter -> OperationTypeDefinition -> Text
operationTypeDefinition Formatter
formatter) (forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty OperationTypeDefinition
operationTypeDefinitions')
    Full.SchemaDirectivesExtension NonEmpty Directive
operationDirectives -> Text
"extend schema "
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) (forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty Directive
operationDirectives)

typeExtension :: Formatter -> Full.TypeExtension -> Lazy.Text
typeExtension :: Formatter -> TypeExtension -> Text
typeExtension Formatter
formatter = \case
    Full.ScalarTypeExtension Text
name' NonEmpty Directive
directives'
        -> Text
"extend scalar "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> Formatter -> [Directive] -> Text
directives Formatter
formatter (forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty Directive
directives')
    Full.ObjectTypeFieldsDefinitionExtension Text
name' ImplementsInterfaces []
ifaces' [Directive]
directives' NonEmpty FieldDefinition
fields'
        -> Text
"extend type "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Text
" " forall a. Semigroup a => a -> a -> a
<>) (forall (t :: * -> *). Foldable t => ImplementsInterfaces t -> Text
implementsInterfaces ImplementsInterfaces []
ifaces')
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
directives'
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
" " Text
""
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> (a -> Text) -> [a] -> Text
bracesList Formatter
formatter (Formatter -> FieldDefinition -> Text
fieldDefinition Formatter
nextFormatter) (forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty FieldDefinition
fields')
    Full.ObjectTypeDirectivesExtension Text
name' ImplementsInterfaces []
ifaces' NonEmpty Directive
directives'
        -> Text
"extend type "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Text
" " forall a. Semigroup a => a -> a -> a
<>) (forall (t :: * -> *). Foldable t => ImplementsInterfaces t -> Text
implementsInterfaces ImplementsInterfaces []
ifaces')
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) (forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty Directive
directives')
    Full.ObjectTypeImplementsInterfacesExtension Text
name' ImplementsInterfaces NonEmpty
ifaces'
        -> Text
"extend type "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Text
" " forall a. Semigroup a => a -> a -> a
<>) (forall (t :: * -> *). Foldable t => ImplementsInterfaces t -> Text
implementsInterfaces ImplementsInterfaces NonEmpty
ifaces')
    Full.InterfaceTypeFieldsDefinitionExtension Text
name' [Directive]
directives' NonEmpty FieldDefinition
fields'
        -> Text
"extend interface "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
directives'
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
" " Text
""
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> (a -> Text) -> [a] -> Text
bracesList Formatter
formatter (Formatter -> FieldDefinition -> Text
fieldDefinition Formatter
nextFormatter) (forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty FieldDefinition
fields')
    Full.InterfaceTypeDirectivesExtension Text
name' NonEmpty Directive
directives'
        -> Text
"extend interface "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) (forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty Directive
directives')
    Full.UnionTypeUnionMemberTypesExtension Text
name' [Directive]
directives' UnionMemberTypes NonEmpty
members'
        -> Text
"extend union "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
directives'
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
" " Text
""
        forall a. Semigroup a => a -> a -> a
<> forall (t :: * -> *).
Foldable t =>
Formatter -> UnionMemberTypes t -> Text
unionMemberTypes Formatter
formatter UnionMemberTypes NonEmpty
members'
    Full.UnionTypeDirectivesExtension Text
name' NonEmpty Directive
directives'
        -> Text
"extend union "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) (forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty Directive
directives')
    Full.EnumTypeEnumValuesDefinitionExtension Text
name' [Directive]
directives' NonEmpty EnumValueDefinition
members'
        -> Text
"extend enum "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
directives'
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
" " Text
""
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> (a -> Text) -> [a] -> Text
bracesList Formatter
formatter (Formatter -> EnumValueDefinition -> Text
enumValueDefinition Formatter
formatter) (forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty EnumValueDefinition
members')
    Full.EnumTypeDirectivesExtension Text
name' NonEmpty Directive
directives'
        -> Text
"extend enum "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) (forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty Directive
directives')
    Full.InputObjectTypeInputFieldsDefinitionExtension Text
name' [Directive]
directives' NonEmpty InputValueDefinition
fields'
        -> Text
"extend input "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
directives'
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
" " Text
""
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> (a -> Text) -> [a] -> Text
bracesList Formatter
formatter (Formatter -> InputValueDefinition -> Text
inputValueDefinition Formatter
nextFormatter) (forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty InputValueDefinition
fields')
    Full.InputObjectTypeDirectivesExtension Text
name' NonEmpty Directive
directives'
        -> Text
"extend input "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) (forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty Directive
directives')
  where
    nextFormatter :: Formatter
nextFormatter = Formatter -> Formatter
incrementIndent Formatter
formatter

-- | Converts a t'Full.TypeSystemDefinition' into a string.
typeSystemDefinition :: Formatter -> Full.TypeSystemDefinition -> Lazy.Text
typeSystemDefinition :: Formatter -> TypeSystemDefinition -> Text
typeSystemDefinition Formatter
formatter = \case
    Full.SchemaDefinition [Directive]
operationDirectives NonEmpty OperationTypeDefinition
operationTypeDefinitions' ->
        Formatter -> Text -> Text
withLineBreak Formatter
formatter
            forall a b. (a -> b) -> a -> b
$ Text
"schema "
            forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
operationDirectives
            forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> (a -> Text) -> [a] -> Text
bracesList Formatter
formatter (Formatter -> OperationTypeDefinition -> Text
operationTypeDefinition Formatter
formatter) (forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty OperationTypeDefinition
operationTypeDefinitions')
    Full.TypeDefinition TypeDefinition
typeDefinition' -> Formatter -> TypeDefinition -> Text
typeDefinition Formatter
formatter TypeDefinition
typeDefinition'
    Full.DirectiveDefinition Description
description' Text
name' ArgumentsDefinition
arguments' NonEmpty DirectiveLocation
locations
        -> Formatter -> Description -> Text
description Formatter
formatter Description
description'
        forall a. Semigroup a => a -> a -> a
<> Text
"@"
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> Formatter -> ArgumentsDefinition -> Text
argumentsDefinition Formatter
formatter ArgumentsDefinition
arguments'
        forall a. Semigroup a => a -> a -> a
<> Text
" on"
        forall a. Semigroup a => a -> a -> a
<> forall (t :: * -> *). Foldable t => Formatter -> t Text -> Text
pipeList Formatter
formatter (DirectiveLocation -> Text
directiveLocation forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NonEmpty DirectiveLocation
locations)

operationTypeDefinition :: Formatter -> Full.OperationTypeDefinition -> Lazy.Text.Text
operationTypeDefinition :: Formatter -> OperationTypeDefinition -> Text
operationTypeDefinition Formatter
formatter (Full.OperationTypeDefinition OperationType
operationType' Text
namedType')
    = Formatter -> Text
indentLine (Formatter -> Formatter
incrementIndent Formatter
formatter)
    forall a. Semigroup a => a -> a -> a
<> Formatter -> OperationType -> Text
operationType Formatter
formatter OperationType
operationType'
    forall a. Semigroup a => a -> a -> a
<> Formatter -> Text
colon Formatter
formatter
    forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
namedType'

fieldDefinition :: Formatter -> Full.FieldDefinition -> Lazy.Text.Text
fieldDefinition :: Formatter -> FieldDefinition -> Text
fieldDefinition Formatter
formatter FieldDefinition
fieldDefinition' =
    let Full.FieldDefinition Description
description' Text
name' ArgumentsDefinition
arguments' Type
type'' [Directive]
directives' = FieldDefinition
fieldDefinition'
     in forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> Description -> Text
description Formatter
formatter) Description
description'
            forall a. Semigroup a => a -> a -> a
<> Formatter -> Text
indentLine Formatter
formatter
            forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
            forall a. Semigroup a => a -> a -> a
<> Formatter -> ArgumentsDefinition -> Text
argumentsDefinition Formatter
formatter ArgumentsDefinition
arguments'
            forall a. Semigroup a => a -> a -> a
<> Formatter -> Text
colon Formatter
formatter
            forall a. Semigroup a => a -> a -> a
<> Type -> Text
type' Type
type''
            forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
directives'

argumentsDefinition :: Formatter -> Full.ArgumentsDefinition -> Lazy.Text.Text
argumentsDefinition :: Formatter -> ArgumentsDefinition -> Text
argumentsDefinition Formatter
formatter (Full.ArgumentsDefinition [InputValueDefinition]
arguments') =
    forall a. Formatter -> (a -> Text) -> [a] -> Text
parensCommas Formatter
formatter (Formatter -> InputValueDefinition -> Text
argumentDefinition Formatter
formatter) [InputValueDefinition]
arguments'

argumentDefinition :: Formatter -> Full.InputValueDefinition -> Lazy.Text.Text
argumentDefinition :: Formatter -> InputValueDefinition -> Text
argumentDefinition Formatter
formatter InputValueDefinition
definition' =
    let Full.InputValueDefinition Description
description' Text
name' Type
type'' Maybe (Node ConstValue)
defaultValue' [Directive]
directives' = InputValueDefinition
definition'
     in forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> Description -> Text
description Formatter
formatter) Description
description'
            forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
            forall a. Semigroup a => a -> a -> a
<> Formatter -> Text
colon Formatter
formatter
            forall a. Semigroup a => a -> a -> a
<> Type -> Text
type' Type
type''
            forall a. Semigroup a => a -> a -> a
<> forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (Formatter -> ConstValue -> Text
defaultValue Formatter
formatter forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Node a -> a
Full.node) Maybe (Node ConstValue)
defaultValue'
            forall a. Semigroup a => a -> a -> a
<> Formatter -> [Directive] -> Text
directives Formatter
formatter [Directive]
directives'

inputValueDefinition :: Formatter -> Full.InputValueDefinition -> Lazy.Text.Text
inputValueDefinition :: Formatter -> InputValueDefinition -> Text
inputValueDefinition Formatter
formatter InputValueDefinition
definition' =
    let Full.InputValueDefinition Description
description' Text
name' Type
type'' Maybe (Node ConstValue)
defaultValue' [Directive]
directives' = InputValueDefinition
definition'
     in forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> Description -> Text
description Formatter
formatter) Description
description'
            forall a. Semigroup a => a -> a -> a
<> Formatter -> Text
indentLine Formatter
formatter
            forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
            forall a. Semigroup a => a -> a -> a
<> Formatter -> Text
colon Formatter
formatter
            forall a. Semigroup a => a -> a -> a
<> Type -> Text
type' Type
type''
            forall a. Semigroup a => a -> a -> a
<> forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (Formatter -> ConstValue -> Text
defaultValue Formatter
formatter forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Node a -> a
Full.node) Maybe (Node ConstValue)
defaultValue'
            forall a. Semigroup a => a -> a -> a
<> Formatter -> [Directive] -> Text
directives Formatter
formatter [Directive]
directives'

typeDefinition :: Formatter -> Full.TypeDefinition -> Lazy.Text
typeDefinition :: Formatter -> TypeDefinition -> Text
typeDefinition Formatter
formatter = \case
    Full.ScalarTypeDefinition Description
description' Text
name' [Directive]
directives'
        -> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> Description -> Text
description Formatter
formatter) Description
description'
        forall a. Semigroup a => a -> a -> a
<> Text
"scalar "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
directives'
    Full.ObjectTypeDefinition Description
description' Text
name' ImplementsInterfaces []
ifaces' [Directive]
directives' [FieldDefinition]
fields'
        -> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> Description -> Text
description Formatter
formatter) Description
description'
        forall a. Semigroup a => a -> a -> a
<> Text
"type "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Text
" " forall a. Semigroup a => a -> a -> a
<>) (forall (t :: * -> *). Foldable t => ImplementsInterfaces t -> Text
implementsInterfaces ImplementsInterfaces []
ifaces')
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
directives'
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
" " Text
""
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> (a -> Text) -> [a] -> Text
bracesList Formatter
formatter (Formatter -> FieldDefinition -> Text
fieldDefinition Formatter
nextFormatter) [FieldDefinition]
fields'
    Full.InterfaceTypeDefinition Description
description' Text
name' [Directive]
directives' [FieldDefinition]
fields'
        -> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> Description -> Text
description Formatter
formatter) Description
description'
        forall a. Semigroup a => a -> a -> a
<> Text
"interface "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
directives'
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
" " Text
""
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> (a -> Text) -> [a] -> Text
bracesList Formatter
formatter (Formatter -> FieldDefinition -> Text
fieldDefinition Formatter
nextFormatter) [FieldDefinition]
fields'
    Full.UnionTypeDefinition Description
description' Text
name' [Directive]
directives' UnionMemberTypes []
members'
        -> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> Description -> Text
description Formatter
formatter) Description
description'
        forall a. Semigroup a => a -> a -> a
<> Text
"union "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
directives'
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
" " Text
""
        forall a. Semigroup a => a -> a -> a
<> forall (t :: * -> *).
Foldable t =>
Formatter -> UnionMemberTypes t -> Text
unionMemberTypes Formatter
formatter UnionMemberTypes []
members'
    Full.EnumTypeDefinition Description
description' Text
name' [Directive]
directives' [EnumValueDefinition]
members'
        -> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> Description -> Text
description Formatter
formatter) Description
description'
        forall a. Semigroup a => a -> a -> a
<> Text
"enum "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
directives'
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
" " Text
""
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> (a -> Text) -> [a] -> Text
bracesList Formatter
formatter (Formatter -> EnumValueDefinition -> Text
enumValueDefinition Formatter
formatter) [EnumValueDefinition]
members'
    Full.InputObjectTypeDefinition Description
description' Text
name' [Directive]
directives' [InputValueDefinition]
fields'
        -> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> Description -> Text
description Formatter
formatter) Description
description'
        forall a. Semigroup a => a -> a -> a
<> Text
"input "
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
directives'
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
" " Text
""
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> (a -> Text) -> [a] -> Text
bracesList Formatter
formatter (Formatter -> InputValueDefinition -> Text
inputValueDefinition Formatter
nextFormatter) [InputValueDefinition]
fields'
  where
    nextFormatter :: Formatter
nextFormatter = Formatter -> Formatter
incrementIndent Formatter
formatter

implementsInterfaces :: Foldable t => Full.ImplementsInterfaces t -> Lazy.Text
implementsInterfaces :: forall (t :: * -> *). Foldable t => ImplementsInterfaces t -> Text
implementsInterfaces (Full.ImplementsInterfaces t Text
interfaces)
    | forall (t :: * -> *) a. Foldable t => t a -> Bool
null t Text
interfaces = forall a. Monoid a => a
mempty
    | Bool
otherwise = Text -> Text
Lazy.Text.fromStrict
        forall a b. (a -> b) -> a -> b
$ Text -> Text -> Text
Text.append Text
"implements "
        forall a b. (a -> b) -> a -> b
$ Text -> [Text] -> Text
Text.intercalate Text
" & "
        forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> [a]
toList t Text
interfaces

unionMemberTypes :: Foldable t => Formatter -> Full.UnionMemberTypes t -> Lazy.Text
unionMemberTypes :: forall (t :: * -> *).
Foldable t =>
Formatter -> UnionMemberTypes t -> Text
unionMemberTypes Formatter
formatter (Full.UnionMemberTypes t Text
memberTypes)
    | forall (t :: * -> *) a. Foldable t => t a -> Bool
null t Text
memberTypes = forall a. Monoid a => a
mempty
    | Bool
otherwise = Text -> Text -> Text
Lazy.Text.append Text
"="
        forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *). Foldable t => Formatter -> t Text -> Text
pipeList Formatter
formatter
        forall a b. (a -> b) -> a -> b
$ Text -> Text
Lazy.Text.fromStrict
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) a. Foldable t => t a -> [a]
toList t Text
memberTypes

pipeList :: Foldable t => Formatter -> t Lazy.Text -> Lazy.Text
pipeList :: forall (t :: * -> *). Foldable t => Formatter -> t Text -> Text
pipeList Formatter
Minified =  (Text
" " forall a. Semigroup a => a -> a -> a
<>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text] -> Text
Lazy.Text.intercalate Text
" | " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
pipeList (Pretty Word
_) =  [Text] -> Text
Lazy.Text.concat
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Text
"\n" forall a. Semigroup a => a -> a -> a
<> Text
indentSymbol forall a. Semigroup a => a -> a -> a
<> Text
"| ") forall a. Semigroup a => a -> a -> a
<>)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
toList 

enumValueDefinition :: Formatter -> Full.EnumValueDefinition -> Lazy.Text
enumValueDefinition :: Formatter -> EnumValueDefinition -> Text
enumValueDefinition (Pretty Word
_) EnumValueDefinition
enumValue =
    let Full.EnumValueDefinition Description
description' Text
name' [Directive]
directives' = EnumValueDefinition
enumValue
        formatter :: Formatter
formatter = Word -> Formatter
Pretty Word
1
     in Formatter -> Description -> Text
description Formatter
formatter Description
description'
        forall a. Semigroup a => a -> a -> a
<> Formatter -> Text
indentLine Formatter
formatter
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> Formatter -> [Directive] -> Text
directives Formatter
formatter [Directive]
directives'
enumValueDefinition Formatter
Minified EnumValueDefinition
enumValue =
    let Full.EnumValueDefinition Description
description' Text
name' [Directive]
directives' = EnumValueDefinition
enumValue
     in Formatter -> Description -> Text
description Formatter
Minified Description
description'
        forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name'
        forall a. Semigroup a => a -> a -> a
<> Formatter -> [Directive] -> Text
directives Formatter
Minified [Directive]
directives'

description :: Formatter -> Full.Description -> Lazy.Text.Text
description :: Formatter -> Description -> Text
description Formatter
_formatter (Full.Description Maybe Text
Nothing) = Text
""
description Formatter
formatter (Full.Description (Just Text
description')) =
    Formatter -> Text -> Text
stringValue Formatter
formatter Text
description'

-- | Converts a t'Full.ExecutableDefinition' into a string.
definition :: Formatter -> Full.ExecutableDefinition -> Lazy.Text
definition :: Formatter -> ExecutableDefinition -> Text
definition Formatter
formatter ExecutableDefinition
x
    | Pretty Word
_ <- Formatter
formatter = Text -> Char -> Text
Lazy.Text.snoc (ExecutableDefinition -> Text
encodeDefinition ExecutableDefinition
x) Char
'\n'
    | Formatter
Minified <- Formatter
formatter = ExecutableDefinition -> Text
encodeDefinition ExecutableDefinition
x
  where
    encodeDefinition :: ExecutableDefinition -> Text
encodeDefinition (Full.DefinitionOperation OperationDefinition
operation)
        = Formatter -> OperationDefinition -> Text
operationDefinition Formatter
formatter OperationDefinition
operation
    encodeDefinition (Full.DefinitionFragment FragmentDefinition
fragment)
        = Formatter -> FragmentDefinition -> Text
fragmentDefinition Formatter
formatter FragmentDefinition
fragment

-- | Converts a 'Full.OperationDefinition into a string.
operationDefinition :: Formatter -> Full.OperationDefinition -> Lazy.Text
operationDefinition :: Formatter -> OperationDefinition -> Text
operationDefinition Formatter
formatter = \case
    Full.SelectionSet SelectionSet
sels Location
_ -> Formatter -> SelectionSet -> Text
selectionSet Formatter
formatter SelectionSet
sels
    Full.OperationDefinition OperationType
Full.Query Maybe Text
name [VariableDefinition]
vars [Directive]
dirs SelectionSet
sels Location
_ ->
        Text
"query " forall a. Semigroup a => a -> a -> a
<> Maybe Text
-> [VariableDefinition] -> [Directive] -> SelectionSet -> Text
root Maybe Text
name [VariableDefinition]
vars [Directive]
dirs SelectionSet
sels
    Full.OperationDefinition OperationType
Full.Mutation Maybe Text
name [VariableDefinition]
vars [Directive]
dirs SelectionSet
sels Location
_ ->
        Text
"mutation " forall a. Semigroup a => a -> a -> a
<> Maybe Text
-> [VariableDefinition] -> [Directive] -> SelectionSet -> Text
root Maybe Text
name [VariableDefinition]
vars [Directive]
dirs SelectionSet
sels
    Full.OperationDefinition OperationType
Full.Subscription Maybe Text
name [VariableDefinition]
vars [Directive]
dirs SelectionSet
sels Location
_ ->
        Text
"subscription " forall a. Semigroup a => a -> a -> a
<> Maybe Text
-> [VariableDefinition] -> [Directive] -> SelectionSet -> Text
root Maybe Text
name [VariableDefinition]
vars [Directive]
dirs SelectionSet
sels
  where
    -- | Converts a Query or Mutation into a string.
    root :: Maybe Full.Name ->
        [Full.VariableDefinition] ->
        [Full.Directive] ->
        Full.SelectionSet ->
        Lazy.Text
    root :: Maybe Text
-> [VariableDefinition] -> [Directive] -> SelectionSet -> Text
root Maybe Text
name [VariableDefinition]
vars [Directive]
dirs SelectionSet
sels
        = Text -> Text
Lazy.Text.fromStrict (forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold Maybe Text
name)
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [VariableDefinition] -> Text
variableDefinitions Formatter
formatter) [VariableDefinition]
vars
        forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
dirs
        forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
" " forall a. Monoid a => a
mempty
        forall a. Semigroup a => a -> a -> a
<> Formatter -> SelectionSet -> Text
selectionSet Formatter
formatter SelectionSet
sels

variableDefinitions :: Formatter -> [Full.VariableDefinition] -> Lazy.Text
variableDefinitions :: Formatter -> [VariableDefinition] -> Text
variableDefinitions Formatter
formatter
    = forall a. Formatter -> (a -> Text) -> [a] -> Text
parensCommas Formatter
formatter forall a b. (a -> b) -> a -> b
$ Formatter -> VariableDefinition -> Text
variableDefinition Formatter
formatter

variableDefinition :: Formatter -> Full.VariableDefinition -> Lazy.Text
variableDefinition :: Formatter -> VariableDefinition -> Text
variableDefinition Formatter
formatter VariableDefinition
variableDefinition' =
    let Full.VariableDefinition Text
variableName Type
variableType Maybe (Node ConstValue)
defaultValue' Location
_ =
            VariableDefinition
variableDefinition'
     in Text -> Text
variable Text
variableName
    forall a. Semigroup a => a -> a -> a
<> Formatter -> Text
colon Formatter
formatter
    forall a. Semigroup a => a -> a -> a
<> Type -> Text
type' Type
variableType
    forall a. Semigroup a => a -> a -> a
<> forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (Formatter -> ConstValue -> Text
defaultValue Formatter
formatter forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Node a -> a
Full.node) Maybe (Node ConstValue)
defaultValue'

defaultValue :: Formatter -> Full.ConstValue -> Lazy.Text
defaultValue :: Formatter -> ConstValue -> Text
defaultValue Formatter
formatter ConstValue
val
    = forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
" = " Text
"="
    forall a. Semigroup a => a -> a -> a
<> Formatter -> Value -> Text
value Formatter
formatter (ConstValue -> Value
fromConstValue ConstValue
val)

variable :: Full.Name -> Lazy.Text
variable :: Text -> Text
variable Text
var = Text
"$" forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
var

selectionSet :: Formatter -> Full.SelectionSet -> Lazy.Text
selectionSet :: Formatter -> SelectionSet -> Text
selectionSet Formatter
formatter
    = forall a. Formatter -> (a -> Text) -> [a] -> Text
bracesList Formatter
formatter (Formatter -> Selection -> Text
selection Formatter
formatter)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NonEmpty a -> [a]
NonEmpty.toList

selectionSetOpt :: Formatter -> Full.SelectionSetOpt -> Lazy.Text
selectionSetOpt :: Formatter -> SelectionSetOpt -> Text
selectionSetOpt Formatter
formatter = forall a. Formatter -> (a -> Text) -> [a] -> Text
bracesList Formatter
formatter forall a b. (a -> b) -> a -> b
$ Formatter -> Selection -> Text
selection Formatter
formatter

indentSymbol :: Lazy.Text
indentSymbol :: Text
indentSymbol = Text
"  "

indent :: (Integral a) => a -> Lazy.Text
indent :: forall a. Integral a => a -> Text
indent a
indentation = Int64 -> Text -> Text
Lazy.Text.replicate (forall a b. (Integral a, Num b) => a -> b
fromIntegral a
indentation) Text
indentSymbol

selection :: Formatter -> Full.Selection -> Lazy.Text
selection :: Formatter -> Selection -> Text
selection Formatter
formatter = Text -> Text -> Text
Lazy.Text.append (Formatter -> Text
indentLine Formatter
formatter')
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. Selection -> Text
encodeSelection
  where
    encodeSelection :: Selection -> Text
encodeSelection (Full.FieldSelection Field
fieldSelection) =
        Formatter -> Field -> Text
field Formatter
formatter' Field
fieldSelection
    encodeSelection (Full.InlineFragmentSelection InlineFragment
fragmentSelection) =
        Formatter -> InlineFragment -> Text
inlineFragment Formatter
formatter' InlineFragment
fragmentSelection
    encodeSelection (Full.FragmentSpreadSelection FragmentSpread
fragmentSelection) =
        Formatter -> FragmentSpread -> Text
fragmentSpread Formatter
formatter' FragmentSpread
fragmentSelection
    formatter' :: Formatter
formatter' = Formatter -> Formatter
incrementIndent Formatter
formatter

indentLine :: Formatter -> Lazy.Text
indentLine :: Formatter -> Text
indentLine Formatter
formatter
    | Pretty Word
indentation <- Formatter
formatter = forall a. Integral a => a -> Text
indent Word
indentation
    | Bool
otherwise = Text
""

incrementIndent :: Formatter -> Formatter
incrementIndent :: Formatter -> Formatter
incrementIndent Formatter
formatter
    | Pretty Word
indentation <- Formatter
formatter = Word -> Formatter
Pretty forall a b. (a -> b) -> a -> b
$ Word
indentation forall a. Num a => a -> a -> a
+ Word
1
    | Bool
otherwise = Formatter
Minified

colon :: Formatter -> Lazy.Text
colon :: Formatter -> Text
colon Formatter
formatter = forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
": " Text
":"

-- | Converts Field into a string.
field :: Formatter -> Full.Field -> Lazy.Text
field :: Formatter -> Field -> Text
field Formatter
formatter (Full.Field Maybe Text
alias Text
name [Argument]
args [Directive]
dirs SelectionSetOpt
set Location
_)
    = forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty Text -> Text
prependAlias (forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold Maybe Text
alias)
    forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name
    forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Argument] -> Text
arguments Formatter
formatter) [Argument]
args
    forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
dirs
    forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty SelectionSetOpt -> Text
selectionSetOpt' SelectionSetOpt
set
  where
    prependAlias :: Text -> Text
prependAlias Text
aliasName = Text -> Text
Lazy.Text.fromStrict Text
aliasName forall a. Semigroup a => a -> a -> a
<>  Formatter -> Text
colon Formatter
formatter
    selectionSetOpt' :: SelectionSetOpt -> Text
selectionSetOpt' = (forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
" " Text
"" forall a. Semigroup a => a -> a -> a
<>)
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. Formatter -> SelectionSetOpt -> Text
selectionSetOpt Formatter
formatter

arguments :: Formatter -> [Full.Argument] -> Lazy.Text
arguments :: Formatter -> [Argument] -> Text
arguments Formatter
formatter = forall a. Formatter -> (a -> Text) -> [a] -> Text
parensCommas Formatter
formatter forall a b. (a -> b) -> a -> b
$ Formatter -> Argument -> Text
argument Formatter
formatter

argument :: Formatter -> Full.Argument -> Lazy.Text
argument :: Formatter -> Argument -> Text
argument Formatter
formatter (Full.Argument Text
name Node Value
value' Location
_)
    = Text -> Text
Lazy.Text.fromStrict Text
name
    forall a. Semigroup a => a -> a -> a
<> Formatter -> Text
colon Formatter
formatter
    forall a. Semigroup a => a -> a -> a
<> Formatter -> Value -> Text
value Formatter
formatter (forall a. Node a -> a
Full.node Node Value
value')

-- * Fragments

fragmentSpread :: Formatter -> Full.FragmentSpread -> Lazy.Text
fragmentSpread :: Formatter -> FragmentSpread -> Text
fragmentSpread Formatter
formatter (Full.FragmentSpread Text
name [Directive]
directives' Location
_)
    = Text
"..." forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name
    forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
directives'

inlineFragment :: Formatter -> Full.InlineFragment -> Lazy.Text
inlineFragment :: Formatter -> InlineFragment -> Text
inlineFragment Formatter
formatter (Full.InlineFragment Maybe Text
typeCondition [Directive]
directives' SelectionSet
selections Location
_)
    = Text
"... on "
    forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict (forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold Maybe Text
typeCondition)
    forall a. Semigroup a => a -> a -> a
<> Formatter -> [Directive] -> Text
directives Formatter
formatter [Directive]
directives'
    forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
" " forall a. Monoid a => a
mempty
    forall a. Semigroup a => a -> a -> a
<> Formatter -> SelectionSet -> Text
selectionSet Formatter
formatter SelectionSet
selections

fragmentDefinition :: Formatter -> Full.FragmentDefinition -> Lazy.Text
fragmentDefinition :: Formatter -> FragmentDefinition -> Text
fragmentDefinition Formatter
formatter (Full.FragmentDefinition Text
name Text
tc [Directive]
dirs SelectionSet
sels Location
_)
    = Text
"fragment " forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name
    forall a. Semigroup a => a -> a -> a
<> Text
" on " forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
tc
    forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Directive] -> Text
directives Formatter
formatter) [Directive]
dirs
    forall a. Semigroup a => a -> a -> a
<> forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
" " forall a. Monoid a => a
mempty
    forall a. Semigroup a => a -> a -> a
<> Formatter -> SelectionSet -> Text
selectionSet Formatter
formatter SelectionSet
sels

-- * Miscellaneous

-- | Converts a 'Full.Directive' into a string.
directive :: Formatter -> Full.Directive -> Lazy.Text
directive :: Formatter -> Directive -> Text
directive Formatter
formatter (Full.Directive Text
name [Argument]
args Location
_)
    = Text
"@" forall a. Semigroup a => a -> a -> a
<> Text -> Text
Lazy.Text.fromStrict Text
name forall a. Semigroup a => a -> a -> a
<> forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty (Formatter -> [Argument] -> Text
arguments Formatter
formatter) [Argument]
args

directives :: Formatter -> [Full.Directive] -> Lazy.Text
directives :: Formatter -> [Directive] -> Text
directives Formatter
Minified [Directive]
values = forall a. (a -> Text) -> [a] -> Text
spaces (Formatter -> Directive -> Text
directive Formatter
Minified) [Directive]
values
directives Formatter
formatter [Directive]
values
    | forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Directive]
values = Text
""
    | Bool
otherwise = Char -> Text -> Text
Lazy.Text.cons Char
' ' forall a b. (a -> b) -> a -> b
$ forall a. (a -> Text) -> [a] -> Text
spaces (Formatter -> Directive -> Text
directive Formatter
formatter) [Directive]
values

-- | Converts a 'Full.Value' into a string.
value :: Formatter -> Full.Value -> Lazy.Text
value :: Formatter -> Value -> Text
value Formatter
_ (Full.Variable Text
x) = Text -> Text
variable Text
x
value Formatter
_ (Full.Int Int32
x) = Builder -> Text
Builder.toLazyText forall a b. (a -> b) -> a -> b
$ forall a. Integral a => a -> Builder
decimal Int32
x
value Formatter
_ (Full.Float Double
x) = Builder -> Text
Builder.toLazyText forall a b. (a -> b) -> a -> b
$ forall a. RealFloat a => a -> Builder
realFloat Double
x
value Formatter
_ (Full.Boolean  Bool
x) = Bool -> Text
booleanValue Bool
x
value Formatter
_ Value
Full.Null = Text
"null"
value Formatter
formatter (Full.String Text
string) = Formatter -> Text -> Text
stringValue Formatter
formatter Text
string
value Formatter
_ (Full.Enum Text
x) = Text -> Text
Lazy.Text.fromStrict Text
x
value Formatter
formatter (Full.List [Node Value]
x) = Formatter -> [Node Value] -> Text
listValue Formatter
formatter [Node Value]
x
value Formatter
formatter (Full.Object [ObjectField Value]
x) = Formatter -> [ObjectField Value] -> Text
objectValue Formatter
formatter [ObjectField Value]
x

fromConstValue :: Full.ConstValue -> Full.Value
fromConstValue :: ConstValue -> Value
fromConstValue (Full.ConstInt Int32
x) = Int32 -> Value
Full.Int Int32
x
fromConstValue (Full.ConstFloat Double
x) = Double -> Value
Full.Float Double
x
fromConstValue (Full.ConstBoolean  Bool
x) = Bool -> Value
Full.Boolean Bool
x
fromConstValue ConstValue
Full.ConstNull = Value
Full.Null
fromConstValue (Full.ConstString Text
string) = Text -> Value
Full.String Text
string
fromConstValue (Full.ConstEnum Text
x) = Text -> Value
Full.Enum Text
x
fromConstValue (Full.ConstList [Node ConstValue]
x) = [Node Value] -> Value
Full.List forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ConstValue -> Value
fromConstValue forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Node ConstValue]
x
fromConstValue (Full.ConstObject [ObjectField ConstValue]
x) = [ObjectField Value] -> Value
Full.Object forall a b. (a -> b) -> a -> b
$ ObjectField ConstValue -> ObjectField Value
fromConstObjectField forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [ObjectField ConstValue]
x
  where
    fromConstObjectField :: ObjectField ConstValue -> ObjectField Value
fromConstObjectField Full.ObjectField{$sel:value:ObjectField :: forall a. ObjectField a -> Node a
value = Node ConstValue
value', Text
Location
$sel:location:ObjectField :: forall a. ObjectField a -> Location
$sel:name:ObjectField :: forall a. ObjectField a -> Text
location :: Location
name :: Text
..} =
        forall a. Text -> Node a -> Location -> ObjectField a
Full.ObjectField Text
name (ConstValue -> Value
fromConstValue forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Node ConstValue
value') Location
location

booleanValue :: Bool -> Lazy.Text
booleanValue :: Bool -> Text
booleanValue Bool
True  = Text
"true"
booleanValue Bool
False = Text
"false"

quote :: Builder.Builder
quote :: Builder
quote = Char -> Builder
Builder.singleton Char
'\"'

oneLine :: Text -> Builder
oneLine :: Text -> Builder
oneLine Text
string = Builder
quote forall a. Semigroup a => a -> a -> a
<> forall a. (Char -> a -> a) -> a -> Text -> a
Text.foldr Char -> Builder -> Builder
merge Builder
quote Text
string
  where
    merge :: Char -> Builder -> Builder
merge = forall a. Monoid a => a -> a -> a
mappend forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Builder
Builder.fromString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> String
Full.escape

stringValue :: Formatter -> Text -> Lazy.Text
stringValue :: Formatter -> Text -> Text
stringValue Formatter
Minified Text
string = Builder -> Text
Builder.toLazyText forall a b. (a -> b) -> a -> b
$ Text -> Builder
oneLine Text
string
stringValue (Pretty Word
indentation) Text
string =
  if Text -> Bool
hasEscaped Text
string
  then Formatter -> Text -> Text
stringValue Formatter
Minified Text
string
  else Builder -> Text
Builder.toLazyText forall a b. (a -> b) -> a -> b
$ [Builder] -> Builder
encoded [Builder]
lines'
    where
      isWhiteSpace :: Char -> Bool
isWhiteSpace Char
char = Char
char forall a. Eq a => a -> a -> Bool
== Char
' ' Bool -> Bool -> Bool
|| Char
char forall a. Eq a => a -> a -> Bool
== Char
'\t'
      isNewline :: Char -> Bool
isNewline Char
char = Char
char forall a. Eq a => a -> a -> Bool
== Char
'\n' Bool -> Bool -> Bool
|| Char
char forall a. Eq a => a -> a -> Bool
== Char
'\r'
      hasEscaped :: Text -> Bool
hasEscaped = (Char -> Bool) -> Text -> Bool
Text.any (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Bool
isAllowed)
      isAllowed :: Char -> Bool
isAllowed Char
char =
          Char
char forall a. Eq a => a -> a -> Bool
== Char
'\t' Bool -> Bool -> Bool
|| Char -> Bool
isNewline Char
char Bool -> Bool -> Bool
|| (Char
char forall a. Ord a => a -> a -> Bool
>= Char
'\x0020' Bool -> Bool -> Bool
&& Char
char forall a. Eq a => a -> a -> Bool
/= Char
'\x007F')

      tripleQuote :: Builder
tripleQuote = Text -> Builder
Builder.fromText Text
"\"\"\""
      newline :: Builder
newline = Char -> Builder
Builder.singleton Char
'\n'

      strip :: Text -> Text
strip = (Char -> Bool) -> Text -> Text
Text.dropWhile Char -> Bool
isWhiteSpace forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> Text -> Text
Text.dropWhileEnd Char -> Bool
isWhiteSpace
      lines' :: [Builder]
lines' = forall a b. (a -> b) -> [a] -> [b]
map Text -> Builder
Builder.fromText forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> Text -> [Text]
Text.split Char -> Bool
isNewline (Text -> Text -> Text -> Text
Text.replace Text
"\r\n" Text
"\n" forall a b. (a -> b) -> a -> b
$ Text -> Text
strip Text
string)
      encoded :: [Builder] -> Builder
encoded [] = Text -> Builder
oneLine Text
string
      encoded [Builder
_] = Text -> Builder
oneLine Text
string
      encoded [Builder]
lines'' = Builder
tripleQuote forall a. Semigroup a => a -> a -> a
<> Builder
newline
        forall a. Semigroup a => a -> a -> a
<> [Builder] -> Builder
transformLines [Builder]
lines''
        forall a. Semigroup a => a -> a -> a
<> Text -> Builder
Builder.fromLazyText (forall a. Integral a => a -> Text
indent Word
indentation) forall a. Semigroup a => a -> a -> a
<> Builder
tripleQuote
      transformLines :: [Builder] -> Builder
transformLines = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Builder -> Builder -> Builder
transformLine forall a. Monoid a => a
mempty
      transformLine :: Builder -> Builder -> Builder
transformLine Builder
"" Builder
acc = Builder
newline forall a. Semigroup a => a -> a -> a
<> Builder
acc
      transformLine Builder
line' Builder
acc
            = Text -> Builder
Builder.fromLazyText (forall a. Integral a => a -> Text
indent (Word
indentation forall a. Num a => a -> a -> a
+ Word
1))
            forall a. Semigroup a => a -> a -> a
<> Builder
line' forall a. Semigroup a => a -> a -> a
<> Builder
newline forall a. Semigroup a => a -> a -> a
<> Builder
acc

listValue :: Formatter -> [Full.Node Full.Value] -> Lazy.Text
listValue :: Formatter -> [Node Value] -> Text
listValue Formatter
formatter = forall a. Formatter -> (a -> Text) -> [a] -> Text
bracketsCommas Formatter
formatter forall a b. (a -> b) -> a -> b
$ Formatter -> Value -> Text
value Formatter
formatter forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Node a -> a
Full.node

objectValue :: Formatter -> [Full.ObjectField Full.Value] -> Lazy.Text
objectValue :: Formatter -> [ObjectField Value] -> Text
objectValue Formatter
formatter = forall a. (a -> Text) -> [a] -> Text
intercalate forall a b. (a -> b) -> a -> b
$ Formatter -> ObjectField Value -> Text
objectField Formatter
formatter
  where
    intercalate :: (a -> Text) -> [a] -> Text
intercalate a -> Text
f
        = Text -> Text
braces
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text] -> Text
Lazy.Text.intercalate (forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
", " Text
",")
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Text
f

objectField :: Formatter -> Full.ObjectField Full.Value -> Lazy.Text
objectField :: Formatter -> ObjectField Value -> Text
objectField Formatter
formatter (Full.ObjectField Text
name (Full.Node Value
value' Location
_) Location
_) =
    Text -> Text
Lazy.Text.fromStrict Text
name forall a. Semigroup a => a -> a -> a
<> Formatter -> Text
colon Formatter
formatter forall a. Semigroup a => a -> a -> a
<> Formatter -> Value -> Text
value Formatter
formatter Value
value'

-- | Converts a 'Full.Type' a type into a string.
type' :: Full.Type -> Lazy.Text
type' :: Type -> Text
type' (Full.TypeNamed Text
x) = Text -> Text
Lazy.Text.fromStrict Text
x
type' (Full.TypeList Type
x) = Type -> Text
listType Type
x
type' (Full.TypeNonNull NonNullType
x) = NonNullType -> Text
nonNullType NonNullType
x

listType :: Full.Type -> Lazy.Text
listType :: Type -> Text
listType Type
x = Text -> Text
brackets (Type -> Text
type' Type
x)

nonNullType :: Full.NonNullType -> Lazy.Text
nonNullType :: NonNullType -> Text
nonNullType (Full.NonNullTypeNamed Text
x) = Text -> Text
Lazy.Text.fromStrict Text
x forall a. Semigroup a => a -> a -> a
<> Text
"!"
nonNullType (Full.NonNullTypeList Type
x) = Type -> Text
listType Type
x forall a. Semigroup a => a -> a -> a
<> Text
"!"

-- | Produces lowercase operation type: query, mutation or subscription.
operationType :: Formatter -> Full.OperationType -> Lazy.Text
operationType :: Formatter -> OperationType -> Text
operationType Formatter
_formatter OperationType
Full.Query = Text
"query"
operationType Formatter
_formatter OperationType
Full.Mutation = Text
"mutation"
operationType Formatter
_formatter OperationType
Full.Subscription = Text
"subscription"

-- * Internal

between :: Char -> Char -> Lazy.Text -> Lazy.Text
between :: Char -> Char -> Text -> Text
between Char
open Char
close = Char -> Text -> Text
Lazy.Text.cons Char
open forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Char -> Text
`Lazy.Text.snoc` Char
close)

parens :: Lazy.Text -> Lazy.Text
parens :: Text -> Text
parens = Char -> Char -> Text -> Text
between Char
'(' Char
')'

brackets :: Lazy.Text -> Lazy.Text
brackets :: Text -> Text
brackets = Char -> Char -> Text -> Text
between Char
'[' Char
']'

braces :: Lazy.Text -> Lazy.Text
braces :: Text -> Text
braces = Char -> Char -> Text -> Text
between Char
'{' Char
'}'

spaces :: forall a. (a -> Lazy.Text) -> [a] -> Lazy.Text
spaces :: forall a. (a -> Text) -> [a] -> Text
spaces a -> Text
f = Text -> [Text] -> Text
Lazy.Text.intercalate Text
"\SP" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Text
f

parensCommas :: forall a. Formatter -> (a -> Lazy.Text) -> [a] -> Lazy.Text
parensCommas :: forall a. Formatter -> (a -> Text) -> [a] -> Text
parensCommas Formatter
formatter a -> Text
f
    = Text -> Text
parens
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text] -> Text
Lazy.Text.intercalate (forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
", " Text
",")
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Text
f

bracketsCommas :: Formatter -> (a -> Lazy.Text) -> [a] -> Lazy.Text
bracketsCommas :: forall a. Formatter -> (a -> Text) -> [a] -> Text
bracketsCommas Formatter
formatter a -> Text
f
    = Text -> Text
brackets
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text] -> Text
Lazy.Text.intercalate (forall a. Formatter -> a -> a -> a
eitherFormat Formatter
formatter Text
", " Text
",")
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Text
f

bracesList :: forall a. Formatter -> (a -> Lazy.Text) -> [a] -> Lazy.Text
bracesList :: forall a. Formatter -> (a -> Text) -> [a] -> Text
bracesList (Pretty Word
intendation) a -> Text
f [a]
xs
    = Text -> Char -> Text
Lazy.Text.snoc (Text -> [Text] -> Text
Lazy.Text.intercalate Text
"\n" [Text]
content) Char
'\n'
    forall a. Semigroup a => a -> a -> a
<> (Text -> Char -> Text
Lazy.Text.snoc forall a b. (a -> b) -> a -> b
$ Int64 -> Text -> Text
Lazy.Text.replicate (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
intendation) Text
"  ") Char
'}'
  where
    content :: [Text]
content = Text
"{" forall a. a -> [a] -> [a]
: forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Text
f [a]
xs
bracesList Formatter
Minified a -> Text
f [a]
xs = Text -> Text
braces forall a b. (a -> b) -> a -> b
$ Text -> [Text] -> Text
Lazy.Text.intercalate Text
"," forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Text
f [a]
xs

optempty :: (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty :: forall a b. (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
optempty a -> b
f a
xs = if a
xs forall a. Eq a => a -> a -> Bool
== forall a. Monoid a => a
mempty then forall a. Monoid a => a
mempty else a -> b
f a
xs

eitherFormat :: forall a. Formatter -> a -> a -> a
eitherFormat :: forall a. Formatter -> a -> a -> a
eitherFormat (Pretty Word
_) a
x a
_ = a
x
eitherFormat Formatter
Minified a
_ a
x = a
x