2016-03-15 14:02:34 +01:00
|
|
|
-- | This module defines an abstract syntax tree for the @GraphQL@ language based on
|
|
|
|
-- <https://facebook.github.io/graphql/ Facebook's GraphQL Specification>.
|
2017-01-28 18:15:14 +01:00
|
|
|
--
|
|
|
|
-- Target AST for Parser.
|
2019-07-14 05:58:05 +02:00
|
|
|
module Language.GraphQL.AST
|
|
|
|
( Alias
|
|
|
|
, Argument(..)
|
|
|
|
, Definition(..)
|
|
|
|
, Directive(..)
|
|
|
|
, Document
|
|
|
|
, Field(..)
|
|
|
|
, FragmentDefinition(..)
|
|
|
|
, FragmentSpread(..)
|
|
|
|
, InlineFragment(..)
|
|
|
|
, Name
|
|
|
|
, NonNullType(..)
|
|
|
|
, ObjectField(..)
|
|
|
|
, OperationDefinition(..)
|
|
|
|
, OperationType(..)
|
|
|
|
, Selection(..)
|
|
|
|
, SelectionSet
|
|
|
|
, SelectionSetOpt
|
|
|
|
, Type(..)
|
|
|
|
, TypeCondition
|
|
|
|
, Value(..)
|
|
|
|
, VariableDefinition(..)
|
|
|
|
) where
|
2015-09-12 12:54:05 +02:00
|
|
|
|
2015-09-18 15:02:51 +02:00
|
|
|
import Data.Int (Int32)
|
2017-01-28 18:15:14 +01:00
|
|
|
import Data.List.NonEmpty (NonEmpty)
|
|
|
|
import Data.Text (Text)
|
2015-09-12 12:54:05 +02:00
|
|
|
|
|
|
|
-- * Document
|
|
|
|
|
2019-08-29 07:40:50 +02:00
|
|
|
-- | GraphQL document.
|
2017-01-28 18:15:14 +01:00
|
|
|
type Document = NonEmpty Definition
|
|
|
|
|
2019-11-03 10:42:10 +01:00
|
|
|
-- | Name
|
|
|
|
type Name = Text
|
|
|
|
|
2019-10-25 09:07:45 +02:00
|
|
|
-- | Directive.
|
|
|
|
data Directive = Directive Name [Argument] deriving (Eq, Show)
|
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
-- * Operations
|
2015-09-12 12:54:05 +02:00
|
|
|
|
2019-08-29 07:40:50 +02:00
|
|
|
-- | Top-level definition of a document, either an operation or a fragment.
|
2019-10-01 06:59:30 +02:00
|
|
|
data Definition
|
|
|
|
= DefinitionOperation OperationDefinition
|
|
|
|
| DefinitionFragment FragmentDefinition
|
|
|
|
deriving (Eq, Show)
|
2015-09-12 12:54:05 +02:00
|
|
|
|
2019-08-29 07:40:50 +02:00
|
|
|
-- | Operation definition.
|
2019-10-01 06:59:30 +02:00
|
|
|
data OperationDefinition
|
|
|
|
= OperationSelectionSet SelectionSet
|
|
|
|
| OperationDefinition OperationType
|
|
|
|
(Maybe Name)
|
|
|
|
[VariableDefinition]
|
|
|
|
[Directive]
|
|
|
|
SelectionSet
|
|
|
|
deriving (Eq, Show)
|
2015-09-21 10:05:09 +02:00
|
|
|
|
2019-08-29 07:40:50 +02:00
|
|
|
-- | GraphQL has 3 operation types: queries, mutations and subscribtions.
|
|
|
|
--
|
|
|
|
-- Currently only queries and mutations are supported.
|
|
|
|
data OperationType = Query | Mutation deriving (Eq, Show)
|
2015-09-12 12:54:05 +02:00
|
|
|
|
2019-08-29 07:40:50 +02:00
|
|
|
-- * Selections
|
2015-09-12 12:54:05 +02:00
|
|
|
|
2019-11-06 06:34:36 +01:00
|
|
|
-- | "Top-level" selection, selection on an operation or fragment.
|
2017-01-28 18:15:14 +01:00
|
|
|
type SelectionSet = NonEmpty Selection
|
2015-09-12 12:54:05 +02:00
|
|
|
|
2019-09-25 05:35:36 +02:00
|
|
|
-- | Field selection.
|
2017-01-28 18:15:14 +01:00
|
|
|
type SelectionSetOpt = [Selection]
|
2015-09-26 01:12:22 +02:00
|
|
|
|
2019-08-29 07:40:50 +02:00
|
|
|
-- | Single selection element.
|
|
|
|
data Selection
|
2019-10-01 06:59:30 +02:00
|
|
|
= SelectionField Field
|
2019-08-29 07:40:50 +02:00
|
|
|
| SelectionFragmentSpread FragmentSpread
|
|
|
|
| SelectionInlineFragment InlineFragment
|
|
|
|
deriving (Eq, Show)
|
2015-09-12 12:54:05 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
-- * Field
|
|
|
|
|
2019-11-03 10:42:10 +01:00
|
|
|
-- | Single GraphQL field.
|
|
|
|
--
|
|
|
|
-- The only required property of a field is its name. Optionally it can also
|
|
|
|
-- have an alias, arguments or a list of subfields.
|
|
|
|
--
|
|
|
|
-- Given the following query:
|
|
|
|
--
|
|
|
|
-- @
|
|
|
|
-- {
|
|
|
|
-- zuck: user(id: 4) {
|
|
|
|
-- id
|
|
|
|
-- name
|
|
|
|
-- }
|
|
|
|
-- }
|
|
|
|
-- @
|
|
|
|
--
|
|
|
|
-- * "user", "id" and "name" are field names.
|
|
|
|
-- * "user" has two subfields, "id" and "name".
|
|
|
|
-- * "zuck" is an alias for "user". "id" and "name" have no aliases.
|
2019-11-06 06:34:36 +01:00
|
|
|
-- * "id: 4" is an argument for "user". "id" and "name" don't have any
|
2019-11-03 10:42:10 +01:00
|
|
|
-- arguments.
|
2019-08-29 07:40:50 +02:00
|
|
|
data Field
|
2019-10-01 06:59:30 +02:00
|
|
|
= Field (Maybe Alias) Name [Argument] [Directive] SelectionSetOpt
|
2019-08-29 07:40:50 +02:00
|
|
|
deriving (Eq, Show)
|
2015-09-12 12:54:05 +02:00
|
|
|
|
2019-11-03 10:42:10 +01:00
|
|
|
-- | Alternative field name.
|
|
|
|
--
|
|
|
|
-- @
|
|
|
|
-- {
|
|
|
|
-- smallPic: profilePic(size: 64)
|
|
|
|
-- bigPic: profilePic(size: 1024)
|
|
|
|
-- }
|
|
|
|
-- @
|
|
|
|
--
|
|
|
|
-- Here "smallPic" and "bigPic" are aliases for the same field, "profilePic",
|
|
|
|
-- used to distinquish between profile pictures with different arguments
|
|
|
|
-- (sizes).
|
|
|
|
type Alias = Name
|
|
|
|
|
|
|
|
-- | Single argument.
|
|
|
|
--
|
|
|
|
-- @
|
|
|
|
-- {
|
|
|
|
-- user(id: 4) {
|
|
|
|
-- name
|
|
|
|
-- }
|
|
|
|
-- }
|
|
|
|
-- @
|
|
|
|
--
|
|
|
|
-- Here "id" is an argument for the field "user" and its value is 4.
|
2015-09-12 12:54:05 +02:00
|
|
|
data Argument = Argument Name Value deriving (Eq,Show)
|
|
|
|
|
|
|
|
-- * Fragments
|
|
|
|
|
2019-08-29 07:40:50 +02:00
|
|
|
-- | Fragment spread.
|
2019-10-01 06:59:30 +02:00
|
|
|
data FragmentSpread = FragmentSpread Name [Directive] deriving (Eq, Show)
|
2015-09-12 12:54:05 +02:00
|
|
|
|
2019-08-29 07:40:50 +02:00
|
|
|
-- | Inline fragment.
|
2019-10-01 06:59:30 +02:00
|
|
|
data InlineFragment = InlineFragment (Maybe TypeCondition) [Directive] SelectionSet
|
2019-08-29 07:40:50 +02:00
|
|
|
deriving (Eq, Show)
|
2015-09-12 12:54:05 +02:00
|
|
|
|
2019-08-29 07:40:50 +02:00
|
|
|
-- | Fragment definition.
|
|
|
|
data FragmentDefinition
|
2019-10-01 06:59:30 +02:00
|
|
|
= FragmentDefinition Name TypeCondition [Directive] SelectionSet
|
2019-08-29 07:40:50 +02:00
|
|
|
deriving (Eq, Show)
|
2015-09-12 12:54:05 +02:00
|
|
|
|
2019-10-25 09:07:45 +02:00
|
|
|
-- * Inputs
|
2015-09-12 12:54:05 +02:00
|
|
|
|
2019-08-29 07:40:50 +02:00
|
|
|
-- | Input value.
|
2019-11-03 10:42:10 +01:00
|
|
|
data Value = Variable Name
|
|
|
|
| Int Int32
|
|
|
|
| Float Double
|
|
|
|
| String Text
|
|
|
|
| Boolean Bool
|
|
|
|
| Null
|
|
|
|
| Enum Name
|
|
|
|
| List [Value]
|
|
|
|
| Object [ObjectField]
|
2019-07-18 05:10:02 +02:00
|
|
|
deriving (Eq, Show)
|
2017-01-28 18:15:14 +01:00
|
|
|
|
2019-08-29 07:40:50 +02:00
|
|
|
-- | Key-value pair.
|
|
|
|
--
|
|
|
|
-- A list of 'ObjectField's represents a GraphQL object type.
|
2019-07-18 05:10:02 +02:00
|
|
|
data ObjectField = ObjectField Name Value deriving (Eq, Show)
|
2015-09-12 12:54:05 +02:00
|
|
|
|
2019-08-29 07:40:50 +02:00
|
|
|
-- | Variable definition.
|
2019-07-18 05:10:02 +02:00
|
|
|
data VariableDefinition = VariableDefinition Name Type (Maybe Value)
|
2019-08-29 07:40:50 +02:00
|
|
|
deriving (Eq, Show)
|
2015-09-12 12:54:05 +02:00
|
|
|
|
2019-11-03 10:42:10 +01:00
|
|
|
-- | Type condition.
|
|
|
|
type TypeCondition = Name
|
|
|
|
|
2019-08-29 07:40:50 +02:00
|
|
|
-- | Type representation.
|
2017-01-28 18:15:14 +01:00
|
|
|
data Type = TypeNamed Name
|
|
|
|
| TypeList Type
|
2015-09-12 12:54:05 +02:00
|
|
|
| TypeNonNull NonNullType
|
2019-08-29 07:40:50 +02:00
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
-- | Helper type to represent Non-Null types and lists of such types.
|
2017-01-28 18:15:14 +01:00
|
|
|
data NonNullType = NonNullTypeNamed Name
|
|
|
|
| NonNullTypeList Type
|
2019-08-29 07:40:50 +02:00
|
|
|
deriving (Eq, Show)
|