{- 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 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 this :: Name
this _ _) == :: InputObjectType -> InputObjectType -> Bool
== (InputObjectType that :: Name
that _ _) = Name
this Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
that

-- | 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

-- | 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 inputType :: ScalarType
inputType) = ScalarType -> Maybe ScalarType
forall a. a -> Maybe a
Just ScalarType
inputType
isScalarType (NonNullScalarType inputType :: ScalarType
inputType) = ScalarType -> Maybe ScalarType
forall a. a -> Maybe a
Just ScalarType
inputType
isScalarType _ = Maybe ScalarType
forall a. Maybe a
Nothing

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

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

isListType :: Type -> Maybe Type
isListType :: Type -> Maybe Type
isListType (ListType inputType :: Type
inputType) = Type -> Maybe Type
forall a. a -> Maybe a
Just Type
inputType
isListType (NonNullListType inputType :: Type
inputType) = Type -> Maybe Type
forall a. a -> Maybe a
Just Type
inputType
isListType _ = 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 _) = Bool
True
isNonNullType (NonNullEnumType _) = Bool
True
isNonNullType (NonNullInputObjectType _) = Bool
True
isNonNullType (NonNullListType _) = Bool
True
isNonNullType _ = Bool
False