{- 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/. -}

{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}

-- | Input types and values.
--
-- This module is intended to be imported qualified, to avoid name clashes
-- with 'Language.GraphQL.Type.Out'.
module Language.GraphQL.Type.In
    ( Argument(..)
    , Arguments
    , InputField(..)
    , InputObjectType(..)
    , Type(..)
    , isNonNullType
    , pattern EnumBaseType
    , pattern ListBaseType
    , pattern InputObjectBaseType
    , pattern ScalarBaseType
    ) where

import Data.HashMap.Strict (HashMap)
import Data.Text (Text)
import qualified Data.Text as Text
import Language.GraphQL.AST.Document (Name)
import qualified Language.GraphQL.Type.Definition as Definition

-- | Single field of an 'InputObjectType'.
data InputField = InputField (Maybe Text) Type (Maybe Definition.Value)

-- | Input object type definition.
--
-- An input object defines a structured collection of fields which may be
-- supplied to a field argument.
data InputObjectType = InputObjectType
    Name (Maybe Text) (HashMap Name InputField)

instance Eq InputObjectType where
    (InputObjectType Name
this Maybe Name
_ HashMap Name InputField
_) == :: InputObjectType -> InputObjectType -> Bool
== (InputObjectType Name
that Maybe Name
_ HashMap Name InputField
_) = Name
this Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
that

instance Show InputObjectType where
    show :: InputObjectType -> String
show (InputObjectType Name
typeName Maybe Name
_ HashMap Name InputField
_) = Name -> String
Text.unpack Name
typeName

-- | These types may be used as input types for arguments and directives.
--
-- GraphQL distinguishes between "wrapping" and "named" types. Each wrapping
-- type can wrap other wrapping or named types. Wrapping types are lists and
-- Non-Null types (named types are nullable by default).
data Type
    = NamedScalarType Definition.ScalarType
    | NamedEnumType Definition.EnumType
    | NamedInputObjectType InputObjectType
    | ListType Type
    | NonNullScalarType Definition.ScalarType
    | NonNullEnumType Definition.EnumType
    | NonNullInputObjectType InputObjectType
    | NonNullListType Type
    deriving Type -> Type -> Bool
(Type -> Type -> Bool) -> (Type -> Type -> Bool) -> Eq Type
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Type -> Type -> Bool
$c/= :: Type -> Type -> Bool
== :: Type -> Type -> Bool
$c== :: Type -> Type -> Bool
Eq

instance Show Type where
    show :: Type -> String
show (NamedScalarType ScalarType
scalarType) = ScalarType -> String
forall a. Show a => a -> String
show ScalarType
scalarType
    show (NamedEnumType EnumType
enumType) = EnumType -> String
forall a. Show a => a -> String
show EnumType
enumType
    show (NamedInputObjectType InputObjectType
inputObjectType) = InputObjectType -> String
forall a. Show a => a -> String
show InputObjectType
inputObjectType
    show (ListType Type
baseType) = [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [String
"[", Type -> String
forall a. Show a => a -> String
show Type
baseType, String
"]"]
    show (NonNullScalarType ScalarType
scalarType) = Char
'!' Char -> ShowS
forall a. a -> [a] -> [a]
: ScalarType -> String
forall a. Show a => a -> String
show ScalarType
scalarType
    show (NonNullEnumType EnumType
enumType) = Char
'!' Char -> ShowS
forall a. a -> [a] -> [a]
: EnumType -> String
forall a. Show a => a -> String
show EnumType
enumType
    show (NonNullInputObjectType InputObjectType
inputObjectType) = Char
'!' Char -> ShowS
forall a. a -> [a] -> [a]
: InputObjectType -> String
forall a. Show a => a -> String
show InputObjectType
inputObjectType
    show (NonNullListType Type
baseType) = [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [String
"![", Type -> String
forall a. Show a => a -> String
show Type
baseType, String
"]"]

-- | Field argument definition.
data Argument = Argument (Maybe Text) Type (Maybe Definition.Value)

-- | Field argument definitions.
type Arguments = HashMap Name Argument

-- | Matches either 'NamedScalarType' or 'NonNullScalarType'.
pattern ScalarBaseType :: Definition.ScalarType -> Type
pattern $mScalarBaseType :: forall r. Type -> (ScalarType -> r) -> (Void# -> r) -> r
ScalarBaseType scalarType <- (isScalarType -> Just scalarType)

-- | Matches either 'NamedEnumType' or 'NonNullEnumType'.
pattern EnumBaseType :: Definition.EnumType -> Type
pattern $mEnumBaseType :: forall r. Type -> (EnumType -> r) -> (Void# -> r) -> r
EnumBaseType enumType <- (isEnumType -> Just enumType)

-- | Matches either 'NamedInputObjectType' or 'NonNullInputObjectType'.
pattern InputObjectBaseType :: InputObjectType -> Type
pattern $mInputObjectBaseType :: forall r. Type -> (InputObjectType -> r) -> (Void# -> r) -> r
InputObjectBaseType objectType <- (isInputObjectType -> Just objectType)

-- | Matches either 'ListType' or 'NonNullListType'.
pattern ListBaseType :: Type -> Type
pattern $mListBaseType :: forall r. Type -> (Type -> r) -> (Void# -> r) -> r
ListBaseType listType <- (isListType -> Just listType)

{-# COMPLETE EnumBaseType, ListBaseType, InputObjectBaseType, ScalarBaseType #-}

isScalarType :: Type -> Maybe Definition.ScalarType
isScalarType :: Type -> Maybe ScalarType
isScalarType (NamedScalarType ScalarType
inputType) = ScalarType -> Maybe ScalarType
forall a. a -> Maybe a
Just ScalarType
inputType
isScalarType (NonNullScalarType ScalarType
inputType) = ScalarType -> Maybe ScalarType
forall a. a -> Maybe a
Just ScalarType
inputType
isScalarType Type
_ = Maybe ScalarType
forall a. Maybe a
Nothing

isInputObjectType :: Type -> Maybe InputObjectType
isInputObjectType :: Type -> Maybe InputObjectType
isInputObjectType (NamedInputObjectType InputObjectType
inputType) = InputObjectType -> Maybe InputObjectType
forall a. a -> Maybe a
Just InputObjectType
inputType
isInputObjectType (NonNullInputObjectType InputObjectType
inputType) = InputObjectType -> Maybe InputObjectType
forall a. a -> Maybe a
Just InputObjectType
inputType
isInputObjectType Type
_ = Maybe InputObjectType
forall a. Maybe a
Nothing

isEnumType :: Type -> Maybe Definition.EnumType
isEnumType :: Type -> Maybe EnumType
isEnumType (NamedEnumType EnumType
inputType) = EnumType -> Maybe EnumType
forall a. a -> Maybe a
Just EnumType
inputType
isEnumType (NonNullEnumType EnumType
inputType) = EnumType -> Maybe EnumType
forall a. a -> Maybe a
Just EnumType
inputType
isEnumType Type
_ = Maybe EnumType
forall a. Maybe a
Nothing

isListType :: Type -> Maybe Type
isListType :: Type -> Maybe Type
isListType (ListType Type
inputType) = Type -> Maybe Type
forall a. a -> Maybe a
Just Type
inputType
isListType (NonNullListType Type
inputType) = Type -> Maybe Type
forall a. a -> Maybe a
Just Type
inputType
isListType Type
_ = Maybe Type
forall a. Maybe a
Nothing

-- | Checks whether the given input type is a non-null type.
isNonNullType :: Type -> Bool
isNonNullType :: Type -> Bool
isNonNullType (NonNullScalarType ScalarType
_) = Bool
True
isNonNullType (NonNullEnumType EnumType
_) = Bool
True
isNonNullType (NonNullInputObjectType InputObjectType
_) = Bool
True
isNonNullType (NonNullListType Type
_) = Bool
True
isNonNullType Type
_ = Bool
False