{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NoImplicitPrelude #-}

module Data.Morpheus.Parsing.Request.Selection
  ( parseSelectionSet,
    parseFragmentDefinition,
  )
where

--
-- MORPHEUS

import Data.Morpheus.Parsing.Internal.Arguments
  ( maybeArguments,
  )
import Data.Morpheus.Parsing.Internal.Internal
  ( Parser,
    getLocation,
  )
import Data.Morpheus.Parsing.Internal.Pattern
  ( optionalDirectives,
  )
import Data.Morpheus.Parsing.Internal.Terms
  ( keyword,
    parseAlias,
    parseName,
    parseTypeCondition,
    setOf,
    spreadLiteral,
  )
import Data.Morpheus.Types.Internal.AST
  ( Fragment (..),
    Position,
    RAW,
    Ref (..),
    Selection (..),
    SelectionContent (..),
    SelectionSet,
  )
import Data.Morpheus.Types.Internal.AST.Name
import Relude
import Text.Megaparsec
  ( label,
    try,
  )

-- Selection Sets : https://graphql.github.io/graphql-spec/June2018/#sec-Selection-Sets
--
-- SelectionSet:
--  { Selection(list) }
--
-- Selection:
--   Field
--   FragmentSpread
--   InlineFragment
--
parseSelectionSet :: Parser (SelectionSet RAW)
parseSelectionSet :: Parser (SelectionSet RAW)
parseSelectionSet = forall e s (m :: * -> *) a.
MonadParsec e s m =>
String -> m a -> m a
label String
"SelectionSet" forall a b. (a -> b) -> a -> b
$ forall (map :: * -> * -> *) k a.
(FromList GQLResult map k a, KeyOf k a) =>
Parser a -> Parser (map k a)
setOf ParsecT Void ByteString GQLResult (Selection RAW)
parseSelection
  where
    parseSelection :: ParsecT Void ByteString GQLResult (Selection RAW)
parseSelection =
      forall e s (m :: * -> *) a.
MonadParsec e s m =>
String -> m a -> m a
label String
"Selection" forall a b. (a -> b) -> a -> b
$
        forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ParsecT Void ByteString GQLResult (Selection RAW)
inlineFragment
          forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT Void ByteString GQLResult (Selection RAW)
spread
          forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT Void ByteString GQLResult (Selection RAW)
parseSelectionField

-- Fields: https://graphql.github.io/graphql-spec/June2018/#sec-Language.Fields
--
-- Field
-- Alias(opt) Name Arguments(opt) Directives(opt) SelectionSet(opt)
--
parseSelectionField :: Parser (Selection RAW)
parseSelectionField :: ParsecT Void ByteString GQLResult (Selection RAW)
parseSelectionField =
  forall e s (m :: * -> *) a.
MonadParsec e s m =>
String -> m a -> m a
label String
"SelectionField" forall a b. (a -> b) -> a -> b
$
    forall (s :: Stage).
Position
-> Maybe FieldName
-> FieldName
-> Arguments s
-> Directives s
-> SelectionContent s
-> Maybe FragmentName
-> Selection s
Selection
      forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Position
getLocation
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (Maybe FieldName)
parseAlias
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (t :: NAME). Parser (Name t)
parseName
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (s :: Stage). Parse (Value s) => Parser (Arguments s)
maybeArguments
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (s :: Stage). Parse (Value s) => Parser (Directives s)
optionalDirectives
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (SelectionContent RAW)
parseSelectionContent
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (f :: * -> *) a. Alternative f => f a
empty

parseSelectionContent :: Parser (SelectionContent RAW)
parseSelectionContent :: Parser (SelectionContent RAW)
parseSelectionContent =
  forall e s (m :: * -> *) a.
MonadParsec e s m =>
String -> m a -> m a
label String
"SelectionContent" forall a b. (a -> b) -> a -> b
$
    forall (s :: Stage). SelectionSet s -> SelectionContent s
SelectionSet forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser (SelectionSet RAW)
parseSelectionSet
      forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (s :: Stage). SelectionContent s
SelectionField

--
-- Fragments: https://graphql.github.io/graphql-spec/June2018/#sec-Language.Fragments
--
--  FragmentName : Name
--

--  FragmentSpread
--    ...FragmentName Directives(opt)
--
spread :: Parser (Selection RAW)
spread :: ParsecT Void ByteString GQLResult (Selection RAW)
spread = forall e s (m :: * -> *) a.
MonadParsec e s m =>
String -> m a -> m a
label String
"FragmentSpread" forall a b. (a -> b) -> a -> b
$ do
  Position
refPosition <- Parser Position
spreadLiteral
  FragmentName
refName <- forall (t :: NAME). Parser (Name t)
parseName
  Directives RAW
directives <- forall (s :: Stage). Parse (Value s) => Parser (Directives s)
optionalDirectives
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Directives RAW -> Ref FragmentName -> Selection RAW
Spread Directives RAW
directives Ref {Position
FragmentName
refPosition :: Position
refName :: FragmentName
refName :: FragmentName
refPosition :: Position
..}

-- FragmentDefinition : https://graphql.github.io/graphql-spec/June2018/#FragmentDefinition
--
--  FragmentDefinition:
--   fragment FragmentName TypeCondition Directives(opt) SelectionSet
--
parseFragmentDefinition :: Parser (Fragment RAW)
parseFragmentDefinition :: Parser (Fragment RAW)
parseFragmentDefinition = forall e s (m :: * -> *) a.
MonadParsec e s m =>
String -> m a -> m a
label String
"Fragment" forall a b. (a -> b) -> a -> b
$ do
  ByteString -> Parser ()
keyword ByteString
"fragment"
  Position
fragmentPosition <- Parser Position
getLocation
  FragmentName
fragmentName <- forall (t :: NAME). Parser (Name t)
parseName
  FragmentName -> Position -> Parser (Fragment RAW)
fragmentBody FragmentName
fragmentName Position
fragmentPosition

-- Inline Fragments : https://graphql.github.io/graphql-spec/June2018/#sec-Inline-Fragments
--
--  InlineFragment:
--  ... TypeCondition(opt) Directives(opt) SelectionSet
--
inlineFragment :: Parser (Selection RAW)
inlineFragment :: ParsecT Void ByteString GQLResult (Selection RAW)
inlineFragment = forall e s (m :: * -> *) a.
MonadParsec e s m =>
String -> m a -> m a
label String
"InlineFragment" forall a b. (a -> b) -> a -> b
$ do
  Position
fragmentPosition <- Parser Position
spreadLiteral
  Fragment RAW -> Selection RAW
InlineFragment forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FragmentName -> Position -> Parser (Fragment RAW)
fragmentBody FragmentName
"INLINE_FRAGMENT" Position
fragmentPosition

fragmentBody :: FragmentName -> Position -> Parser (Fragment RAW)
fragmentBody :: FragmentName -> Position -> Parser (Fragment RAW)
fragmentBody FragmentName
fragmentName Position
fragmentPosition = forall e s (m :: * -> *) a.
MonadParsec e s m =>
String -> m a -> m a
label String
"FragmentBody" forall a b. (a -> b) -> a -> b
$ do
  TypeName
fragmentType <- Parser TypeName
parseTypeCondition
  Directives RAW
fragmentDirectives <- forall (s :: Stage). Parse (Value s) => Parser (Directives s)
optionalDirectives
  MergeMap 'True FieldName (Selection RAW)
fragmentSelection <- Parser (SelectionSet RAW)
parseSelectionSet
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Fragment {MergeMap 'True FieldName (Selection RAW)
Directives RAW
Position
TypeName
FragmentName
fragmentDirectives :: Directives RAW
fragmentSelection :: SelectionSet RAW
fragmentPosition :: Position
fragmentType :: TypeName
fragmentName :: FragmentName
fragmentSelection :: MergeMap 'True FieldName (Selection RAW)
fragmentDirectives :: Directives RAW
fragmentType :: TypeName
fragmentPosition :: Position
fragmentName :: FragmentName
..}