Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
bdf711d69f | |||
b215e1a4a7 | |||
1e55f17e7e | |||
9a5d54c035 | |||
0cbe69736b | |||
4c0d226030 | |||
3c1a5c800f | |||
fc9ad9c4a1 | |||
def52ddc20 | |||
3497784984 | |||
587aab005e | |||
625d7100ca | |||
73e21661b4 | |||
7b92e5bcfd | |||
115aa02672 | |||
31c516927d | |||
1dd6b7b013 | |||
b77da3d492 | |||
73fc334bf8 | |||
417ff5da7d | |||
0e3b6184be | |||
51d39b69e8 |
59
CHANGELOG.md
59
CHANGELOG.md
@ -1,6 +1,63 @@
|
|||||||
# Change Log
|
# Change Log
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## [0.6.1.0] - 2019-12-23
|
||||||
|
### Fixed
|
||||||
|
- Parsing multiple string arguments, such as
|
||||||
|
`login(username: "username", password: "password")` would fail on the comma
|
||||||
|
due to strings not having a space consumer.
|
||||||
|
- Fragment spread is evaluated based on the `__typename` resolver. If the
|
||||||
|
resolver is missing, it is assumed that the type condition is satisfied (all
|
||||||
|
fragments are included).
|
||||||
|
- Escaping characters during encoding.
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Directive support (@skip and @include).
|
||||||
|
- Pretifying multi-line string arguments as block strings.
|
||||||
|
|
||||||
|
## [0.6.0.0] - 2019-11-27
|
||||||
|
### Changed
|
||||||
|
- `Language.GraphQL.Encoder` moved to `Language.GraphQL.AST.Encoder`.
|
||||||
|
- `Language.GraphQL.Parser` moved to `Language.GraphQL.AST.Parser`.
|
||||||
|
- `Language.GraphQL.Lexer` moved to `Language.GraphQL.AST.Lexer`.
|
||||||
|
- All `Language.GraphQL.AST.Value` data constructor prefixes were removed. The
|
||||||
|
module should be imported qualified.
|
||||||
|
- All `Language.GraphQL.AST.Core.Value` data constructor prefixes were removed.
|
||||||
|
The module should be imported qualified.
|
||||||
|
- `Language.GraphQL.AST.Core.Object` is now just a HashMap.
|
||||||
|
- `Language.GraphQL.AST.Transform` is isn't exposed publically anymore.
|
||||||
|
- `Language.GraphQL.Schema.resolve` accepts a selection `Seq` (`Data.Sequence`)
|
||||||
|
instead of a list. Selections are stored as sequences internally as well.
|
||||||
|
- Add a reader instance to the resolver's monad stack. The Reader contains
|
||||||
|
a Name/Value hashmap, which will contain resolver arguments.
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Nested fragment support.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- Consume ignored tokens after `$` and `!`. I mistakenly assumed that
|
||||||
|
`$variable` is a single token, same as `Type!` is a single token. This is not
|
||||||
|
the case, for example `Variable` is defined as `$ Name`, so these are two
|
||||||
|
tokens, therefore whitespaces and commas after `$` and `!` should be
|
||||||
|
consumed.
|
||||||
|
|
||||||
|
### Improved
|
||||||
|
- `Language.GraphQL.AST.Parser.type_`: Try type parsers in a variable
|
||||||
|
definition in a different order to avoid using `but`.
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
- `Language.GraphQL.AST.Arguments`. Use `[Language.GraphQL.AST.Argument]`
|
||||||
|
instead.
|
||||||
|
- `Language.GraphQL.AST.Directives`. Use `[Language.GraphQL.AST.Directives]`
|
||||||
|
instead.
|
||||||
|
- `Language.GraphQL.AST.VariableDefinitions`. Use
|
||||||
|
`[Language.GraphQL.AST.VariableDefinition]` instead.
|
||||||
|
- `Language.GraphQL.AST.FragmentName`. Use `Language.GraphQL.AST.Name` instead.
|
||||||
|
- `Language.GraphQL.Execute.Schema` - It was a resolver list, not a schema.
|
||||||
|
- `Language.GraphQL.Schema`: `enum`, `enumA`, `wrappedEnum` and `wrappedEnumA`.
|
||||||
|
Use `scalar`, `scalarA`, `wrappedScalar` and `wrappedScalarA` instead.
|
||||||
|
|
||||||
|
|
||||||
## [0.5.1.0] - 2019-10-22
|
## [0.5.1.0] - 2019-10-22
|
||||||
### Deprecated
|
### Deprecated
|
||||||
- `Language.GraphQL.AST.Arguments`. Use `[Language.GraphQL.AST.Argument]`
|
- `Language.GraphQL.AST.Arguments`. Use `[Language.GraphQL.AST.Argument]`
|
||||||
@ -105,6 +162,8 @@ All notable changes to this project will be documented in this file.
|
|||||||
### Added
|
### Added
|
||||||
- Data types for the GraphQL language.
|
- Data types for the GraphQL language.
|
||||||
|
|
||||||
|
[0.6.1.0]: https://github.com/caraus-ecms/graphql/compare/v0.6.0.0...v0.6.1.0
|
||||||
|
[0.6.0.0]: https://github.com/caraus-ecms/graphql/compare/v0.5.1.0...v0.6.0.0
|
||||||
[0.5.1.0]: https://github.com/caraus-ecms/graphql/compare/v0.5.0.1...v0.5.1.0
|
[0.5.1.0]: https://github.com/caraus-ecms/graphql/compare/v0.5.0.1...v0.5.1.0
|
||||||
[0.5.0.1]: https://github.com/caraus-ecms/graphql/compare/v0.5.0.0...v0.5.0.1
|
[0.5.0.1]: https://github.com/caraus-ecms/graphql/compare/v0.5.0.0...v0.5.0.1
|
||||||
[0.5.0.0]: https://github.com/caraus-ecms/graphql/compare/v0.4.0.0...v0.5.0.0
|
[0.5.0.0]: https://github.com/caraus-ecms/graphql/compare/v0.4.0.0...v0.5.0.0
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name: graphql
|
name: graphql
|
||||||
version: 0.5.1.0
|
version: 0.6.1.0
|
||||||
synopsis: Haskell GraphQL implementation
|
synopsis: Haskell GraphQL implementation
|
||||||
description:
|
description:
|
||||||
This package provides a rudimentary parser for the
|
This package provides a rudimentary parser for the
|
||||||
@ -28,6 +28,7 @@ data-files:
|
|||||||
dependencies:
|
dependencies:
|
||||||
- aeson
|
- aeson
|
||||||
- base >= 4.7 && < 5
|
- base >= 4.7 && < 5
|
||||||
|
- containers
|
||||||
- megaparsec
|
- megaparsec
|
||||||
- text
|
- text
|
||||||
- transformers
|
- transformers
|
||||||
@ -35,6 +36,9 @@ dependencies:
|
|||||||
|
|
||||||
library:
|
library:
|
||||||
source-dirs: src
|
source-dirs: src
|
||||||
|
other-modules:
|
||||||
|
- Language.GraphQL.Execute.Transform
|
||||||
|
- Language.GraphQL.Type.Directive
|
||||||
|
|
||||||
tests:
|
tests:
|
||||||
tasty:
|
tasty:
|
||||||
|
@ -10,7 +10,7 @@ import Data.List.NonEmpty (NonEmpty)
|
|||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Language.GraphQL.Error
|
import Language.GraphQL.Error
|
||||||
import Language.GraphQL.Execute
|
import Language.GraphQL.Execute
|
||||||
import Language.GraphQL.Parser
|
import Language.GraphQL.AST.Parser
|
||||||
import qualified Language.GraphQL.Schema as Schema
|
import qualified Language.GraphQL.Schema as Schema
|
||||||
import Text.Megaparsec (parse)
|
import Text.Megaparsec (parse)
|
||||||
|
|
||||||
|
@ -5,14 +5,11 @@
|
|||||||
module Language.GraphQL.AST
|
module Language.GraphQL.AST
|
||||||
( Alias
|
( Alias
|
||||||
, Argument(..)
|
, Argument(..)
|
||||||
, Arguments
|
|
||||||
, Definition(..)
|
, Definition(..)
|
||||||
, Directive(..)
|
, Directive(..)
|
||||||
, Directives
|
|
||||||
, Document
|
, Document
|
||||||
, Field(..)
|
, Field(..)
|
||||||
, FragmentDefinition(..)
|
, FragmentDefinition(..)
|
||||||
, FragmentName
|
|
||||||
, FragmentSpread(..)
|
, FragmentSpread(..)
|
||||||
, InlineFragment(..)
|
, InlineFragment(..)
|
||||||
, Name
|
, Name
|
||||||
@ -27,22 +24,23 @@ module Language.GraphQL.AST
|
|||||||
, TypeCondition
|
, TypeCondition
|
||||||
, Value(..)
|
, Value(..)
|
||||||
, VariableDefinition(..)
|
, VariableDefinition(..)
|
||||||
, VariableDefinitions
|
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Int (Int32)
|
import Data.Int (Int32)
|
||||||
import Data.List.NonEmpty (NonEmpty)
|
import Data.List.NonEmpty (NonEmpty)
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import Language.GraphQL.AST.Core ( Alias
|
|
||||||
, Name
|
|
||||||
, TypeCondition
|
|
||||||
)
|
|
||||||
|
|
||||||
-- * Document
|
-- * Document
|
||||||
|
|
||||||
-- | GraphQL document.
|
-- | GraphQL document.
|
||||||
type Document = NonEmpty Definition
|
type Document = NonEmpty Definition
|
||||||
|
|
||||||
|
-- | Name
|
||||||
|
type Name = Text
|
||||||
|
|
||||||
|
-- | Directive.
|
||||||
|
data Directive = Directive Name [Argument] deriving (Eq, Show)
|
||||||
|
|
||||||
-- * Operations
|
-- * Operations
|
||||||
|
|
||||||
-- | Top-level definition of a document, either an operation or a fragment.
|
-- | Top-level definition of a document, either an operation or a fragment.
|
||||||
@ -68,7 +66,7 @@ data OperationType = Query | Mutation deriving (Eq, Show)
|
|||||||
|
|
||||||
-- * Selections
|
-- * Selections
|
||||||
|
|
||||||
-- | "Top-level" selection, selection on a operation.
|
-- | "Top-level" selection, selection on an operation or fragment.
|
||||||
type SelectionSet = NonEmpty Selection
|
type SelectionSet = NonEmpty Selection
|
||||||
|
|
||||||
-- | Field selection.
|
-- | Field selection.
|
||||||
@ -83,18 +81,56 @@ data Selection
|
|||||||
|
|
||||||
-- * Field
|
-- * Field
|
||||||
|
|
||||||
-- | GraphQL field.
|
-- | 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.
|
||||||
|
-- * "id: 4" is an argument for "user". "id" and "name" don't have any
|
||||||
|
-- arguments.
|
||||||
data Field
|
data Field
|
||||||
= Field (Maybe Alias) Name [Argument] [Directive] SelectionSetOpt
|
= Field (Maybe Alias) Name [Argument] [Directive] SelectionSetOpt
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
-- * Arguments
|
-- | 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
|
||||||
|
|
||||||
-- | Argument list.
|
-- | Single argument.
|
||||||
{-# DEPRECATED Arguments "Use [Argument] instead" #-}
|
--
|
||||||
type Arguments = [Argument]
|
-- @
|
||||||
|
-- {
|
||||||
-- | Argument.
|
-- user(id: 4) {
|
||||||
|
-- name
|
||||||
|
-- }
|
||||||
|
-- }
|
||||||
|
-- @
|
||||||
|
--
|
||||||
|
-- Here "id" is an argument for the field "user" and its value is 4.
|
||||||
data Argument = Argument Name Value deriving (Eq,Show)
|
data Argument = Argument Name Value deriving (Eq,Show)
|
||||||
|
|
||||||
-- * Fragments
|
-- * Fragments
|
||||||
@ -111,21 +147,18 @@ data FragmentDefinition
|
|||||||
= FragmentDefinition Name TypeCondition [Directive] SelectionSet
|
= FragmentDefinition Name TypeCondition [Directive] SelectionSet
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
{-# DEPRECATED FragmentName "Use Name instead" #-}
|
-- * Inputs
|
||||||
type FragmentName = Name
|
|
||||||
|
|
||||||
-- * Input values
|
|
||||||
|
|
||||||
-- | Input value.
|
-- | Input value.
|
||||||
data Value = ValueVariable Name
|
data Value = Variable Name
|
||||||
| ValueInt Int32
|
| Int Int32
|
||||||
| ValueFloat Double
|
| Float Double
|
||||||
| ValueString Text
|
| String Text
|
||||||
| ValueBoolean Bool
|
| Boolean Bool
|
||||||
| ValueNull
|
| Null
|
||||||
| ValueEnum Name
|
| Enum Name
|
||||||
| ValueList [Value]
|
| List [Value]
|
||||||
| ValueObject [ObjectField]
|
| Object [ObjectField]
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
-- | Key-value pair.
|
-- | Key-value pair.
|
||||||
@ -133,17 +166,12 @@ data Value = ValueVariable Name
|
|||||||
-- A list of 'ObjectField's represents a GraphQL object type.
|
-- A list of 'ObjectField's represents a GraphQL object type.
|
||||||
data ObjectField = ObjectField Name Value deriving (Eq, Show)
|
data ObjectField = ObjectField Name Value deriving (Eq, Show)
|
||||||
|
|
||||||
-- * Variables
|
|
||||||
|
|
||||||
-- | Variable definition list.
|
|
||||||
{-# DEPRECATED VariableDefinitions "Use [VariableDefinition] instead" #-}
|
|
||||||
type VariableDefinitions = [VariableDefinition]
|
|
||||||
|
|
||||||
-- | Variable definition.
|
-- | Variable definition.
|
||||||
data VariableDefinition = VariableDefinition Name Type (Maybe Value)
|
data VariableDefinition = VariableDefinition Name Type (Maybe Value)
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
-- * Input types
|
-- | Type condition.
|
||||||
|
type TypeCondition = Name
|
||||||
|
|
||||||
-- | Type representation.
|
-- | Type representation.
|
||||||
data Type = TypeNamed Name
|
data Type = TypeNamed Name
|
||||||
@ -151,17 +179,7 @@ data Type = TypeNamed Name
|
|||||||
| TypeNonNull NonNullType
|
| TypeNonNull NonNullType
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
|
|
||||||
-- | Helper type to represent Non-Null types and lists of such types.
|
-- | Helper type to represent Non-Null types and lists of such types.
|
||||||
data NonNullType = NonNullTypeNamed Name
|
data NonNullType = NonNullTypeNamed Name
|
||||||
| NonNullTypeList Type
|
| NonNullTypeList Type
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
-- * Directives
|
|
||||||
|
|
||||||
-- | Directive list.
|
|
||||||
{-# DEPRECATED Directives "Use [Directive] instead" #-}
|
|
||||||
type Directives = [Directive]
|
|
||||||
|
|
||||||
-- | Directive.
|
|
||||||
data Directive = Directive Name [Argument] deriving (Eq, Show)
|
|
||||||
|
@ -2,11 +2,12 @@
|
|||||||
module Language.GraphQL.AST.Core
|
module Language.GraphQL.AST.Core
|
||||||
( Alias
|
( Alias
|
||||||
, Argument(..)
|
, Argument(..)
|
||||||
|
, Arguments(..)
|
||||||
|
, Directive(..)
|
||||||
, Document
|
, Document
|
||||||
, Field(..)
|
, Field(..)
|
||||||
, Fragment(..)
|
, Fragment(..)
|
||||||
, Name
|
, Name
|
||||||
, ObjectField(..)
|
|
||||||
, Operation(..)
|
, Operation(..)
|
||||||
, Selection(..)
|
, Selection(..)
|
||||||
, TypeCondition
|
, TypeCondition
|
||||||
@ -14,12 +15,12 @@ module Language.GraphQL.AST.Core
|
|||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Int (Int32)
|
import Data.Int (Int32)
|
||||||
|
import Data.HashMap.Strict (HashMap)
|
||||||
import Data.List.NonEmpty (NonEmpty)
|
import Data.List.NonEmpty (NonEmpty)
|
||||||
import Data.String
|
import Data.Sequence (Seq)
|
||||||
|
import Data.String (IsString(..))
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
|
import Language.GraphQL.AST (Alias, Name, TypeCondition)
|
||||||
-- | Name
|
|
||||||
type Name = Text
|
|
||||||
|
|
||||||
-- | GraphQL document is a non-empty list of operations.
|
-- | GraphQL document is a non-empty list of operations.
|
||||||
type Document = NonEmpty Operation
|
type Document = NonEmpty Operation
|
||||||
@ -28,87 +29,29 @@ type Document = NonEmpty Operation
|
|||||||
--
|
--
|
||||||
-- Currently only queries and mutations are supported.
|
-- Currently only queries and mutations are supported.
|
||||||
data Operation
|
data Operation
|
||||||
= Query (Maybe Text) (NonEmpty Selection)
|
= Query (Maybe Text) (Seq Selection)
|
||||||
| Mutation (Maybe Text) (NonEmpty Selection)
|
| Mutation (Maybe Text) (Seq Selection)
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
-- | A single GraphQL field.
|
-- | Single GraphQL field.
|
||||||
--
|
data Field
|
||||||
-- Only required property of a field, is its name. Optionally it can also have
|
= Field (Maybe Alias) Name [Argument] (Seq Selection)
|
||||||
-- an alias, arguments or a list of subfields.
|
deriving (Eq, Show)
|
||||||
--
|
|
||||||
-- 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.
|
|
||||||
-- * "id: 4" is an argument for "name". "id" and "name don't have any
|
|
||||||
-- arguments.
|
|
||||||
data Field = Field (Maybe Alias) Name [Argument] [Selection] deriving (Eq, Show)
|
|
||||||
|
|
||||||
-- | 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.
|
-- | Single argument.
|
||||||
--
|
|
||||||
-- @
|
|
||||||
-- {
|
|
||||||
-- user(id: 4) {
|
|
||||||
-- name
|
|
||||||
-- }
|
|
||||||
-- }
|
|
||||||
-- @
|
|
||||||
--
|
|
||||||
-- Here "id" is an argument for the field "user" and its value is 4.
|
|
||||||
data Argument = Argument Name Value deriving (Eq, Show)
|
data Argument = Argument Name Value deriving (Eq, Show)
|
||||||
|
|
||||||
-- | Represents accordingly typed GraphQL values.
|
-- | Argument list.
|
||||||
data Value
|
newtype Arguments = Arguments (HashMap Name Value)
|
||||||
= ValueInt Int32
|
|
||||||
-- GraphQL Float is double precision
|
|
||||||
| ValueFloat Double
|
|
||||||
| ValueString Text
|
|
||||||
| ValueBoolean Bool
|
|
||||||
| ValueNull
|
|
||||||
| ValueEnum Name
|
|
||||||
| ValueList [Value]
|
|
||||||
| ValueObject [ObjectField]
|
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
instance IsString Value where
|
-- | Directive.
|
||||||
fromString = ValueString . fromString
|
data Directive = Directive Name Arguments
|
||||||
|
deriving (Eq, Show)
|
||||||
-- | Key-value pair.
|
|
||||||
--
|
|
||||||
-- A list of 'ObjectField's represents a GraphQL object type.
|
|
||||||
data ObjectField = ObjectField Name Value deriving (Eq, Show)
|
|
||||||
|
|
||||||
-- | Type condition.
|
|
||||||
type TypeCondition = Name
|
|
||||||
|
|
||||||
-- | Represents fragments and inline fragments.
|
-- | Represents fragments and inline fragments.
|
||||||
data Fragment
|
data Fragment
|
||||||
= Fragment TypeCondition (NonEmpty Selection)
|
= Fragment TypeCondition (Seq Selection)
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
-- | Single selection element.
|
-- | Single selection element.
|
||||||
@ -116,3 +59,18 @@ data Selection
|
|||||||
= SelectionFragment Fragment
|
= SelectionFragment Fragment
|
||||||
| SelectionField Field
|
| SelectionField Field
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
|
-- | Represents accordingly typed GraphQL values.
|
||||||
|
data Value
|
||||||
|
= Int Int32
|
||||||
|
| Float Double -- ^ GraphQL Float is double precision
|
||||||
|
| String Text
|
||||||
|
| Boolean Bool
|
||||||
|
| Null
|
||||||
|
| Enum Name
|
||||||
|
| List [Value]
|
||||||
|
| Object (HashMap Name Value)
|
||||||
|
deriving (Eq, Show)
|
||||||
|
|
||||||
|
instance IsString Value where
|
||||||
|
fromString = String . fromString
|
||||||
|
308
src/Language/GraphQL/AST/Encoder.hs
Normal file
308
src/Language/GraphQL/AST/Encoder.hs
Normal file
@ -0,0 +1,308 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE ExplicitForAll #-}
|
||||||
|
|
||||||
|
-- | This module defines a minifier and a printer for the @GraphQL@ language.
|
||||||
|
module Language.GraphQL.AST.Encoder
|
||||||
|
( Formatter
|
||||||
|
, definition
|
||||||
|
, directive
|
||||||
|
, document
|
||||||
|
, minified
|
||||||
|
, pretty
|
||||||
|
, type'
|
||||||
|
, value
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Data.Char (ord)
|
||||||
|
import Data.Foldable (fold)
|
||||||
|
import Data.Monoid ((<>))
|
||||||
|
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, hexadecimal)
|
||||||
|
import Data.Text.Lazy.Builder.RealFloat (realFloat)
|
||||||
|
import qualified Language.GraphQL.AST as Full
|
||||||
|
|
||||||
|
-- | 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 = Pretty 0
|
||||||
|
|
||||||
|
-- | Constructs a formatter for minifying.
|
||||||
|
minified :: Formatter
|
||||||
|
minified = Minified
|
||||||
|
|
||||||
|
-- | Converts a 'Full.Document' into a string.
|
||||||
|
document :: Formatter -> Full.Document -> Lazy.Text
|
||||||
|
document formatter defs
|
||||||
|
| Pretty _ <- formatter = Lazy.Text.intercalate "\n" encodeDocument
|
||||||
|
| Minified <-formatter = Lazy.Text.snoc (mconcat encodeDocument) '\n'
|
||||||
|
where
|
||||||
|
encodeDocument = NonEmpty.toList $ definition formatter <$> defs
|
||||||
|
|
||||||
|
-- | Converts a 'Full.Definition' into a string.
|
||||||
|
definition :: Formatter -> Full.Definition -> Lazy.Text
|
||||||
|
definition formatter x
|
||||||
|
| Pretty _ <- formatter = Lazy.Text.snoc (encodeDefinition x) '\n'
|
||||||
|
| Minified <- formatter = encodeDefinition x
|
||||||
|
where
|
||||||
|
encodeDefinition (Full.DefinitionOperation operation)
|
||||||
|
= operationDefinition formatter operation
|
||||||
|
encodeDefinition (Full.DefinitionFragment fragment)
|
||||||
|
= fragmentDefinition formatter fragment
|
||||||
|
|
||||||
|
operationDefinition :: Formatter -> Full.OperationDefinition -> Lazy.Text
|
||||||
|
operationDefinition formatter (Full.OperationSelectionSet sels)
|
||||||
|
= selectionSet formatter sels
|
||||||
|
operationDefinition formatter (Full.OperationDefinition Full.Query name vars dirs sels)
|
||||||
|
= "query " <> node formatter name vars dirs sels
|
||||||
|
operationDefinition formatter (Full.OperationDefinition Full.Mutation name vars dirs sels)
|
||||||
|
= "mutation " <> node formatter name vars dirs sels
|
||||||
|
|
||||||
|
node :: Formatter ->
|
||||||
|
Maybe Full.Name ->
|
||||||
|
[Full.VariableDefinition] ->
|
||||||
|
[Full.Directive] ->
|
||||||
|
Full.SelectionSet ->
|
||||||
|
Lazy.Text
|
||||||
|
node formatter name vars dirs sels
|
||||||
|
= Lazy.Text.fromStrict (fold name)
|
||||||
|
<> optempty (variableDefinitions formatter) vars
|
||||||
|
<> optempty (directives formatter) dirs
|
||||||
|
<> eitherFormat formatter " " mempty
|
||||||
|
<> selectionSet formatter sels
|
||||||
|
|
||||||
|
variableDefinitions :: Formatter -> [Full.VariableDefinition] -> Lazy.Text
|
||||||
|
variableDefinitions formatter
|
||||||
|
= parensCommas formatter $ variableDefinition formatter
|
||||||
|
|
||||||
|
variableDefinition :: Formatter -> Full.VariableDefinition -> Lazy.Text
|
||||||
|
variableDefinition formatter (Full.VariableDefinition var ty dv)
|
||||||
|
= variable var
|
||||||
|
<> eitherFormat formatter ": " ":"
|
||||||
|
<> type' ty
|
||||||
|
<> maybe mempty (defaultValue formatter) dv
|
||||||
|
|
||||||
|
defaultValue :: Formatter -> Full.Value -> Lazy.Text
|
||||||
|
defaultValue formatter val
|
||||||
|
= eitherFormat formatter " = " "="
|
||||||
|
<> value formatter val
|
||||||
|
|
||||||
|
variable :: Full.Name -> Lazy.Text
|
||||||
|
variable var = "$" <> Lazy.Text.fromStrict var
|
||||||
|
|
||||||
|
selectionSet :: Formatter -> Full.SelectionSet -> Lazy.Text
|
||||||
|
selectionSet formatter
|
||||||
|
= bracesList formatter (selection formatter)
|
||||||
|
. NonEmpty.toList
|
||||||
|
|
||||||
|
selectionSetOpt :: Formatter -> Full.SelectionSetOpt -> Lazy.Text
|
||||||
|
selectionSetOpt formatter = bracesList formatter $ selection formatter
|
||||||
|
|
||||||
|
indent :: (Integral a) => a -> Lazy.Text
|
||||||
|
indent indentation = Lazy.Text.replicate (fromIntegral indentation) " "
|
||||||
|
|
||||||
|
selection :: Formatter -> Full.Selection -> Lazy.Text
|
||||||
|
selection formatter = Lazy.Text.append indent' . encodeSelection
|
||||||
|
where
|
||||||
|
encodeSelection (Full.SelectionField field') = field incrementIndent field'
|
||||||
|
encodeSelection (Full.SelectionInlineFragment fragment) =
|
||||||
|
inlineFragment incrementIndent fragment
|
||||||
|
encodeSelection (Full.SelectionFragmentSpread spread) =
|
||||||
|
fragmentSpread incrementIndent spread
|
||||||
|
incrementIndent
|
||||||
|
| Pretty indentation <- formatter = Pretty $ indentation + 1
|
||||||
|
| otherwise = Minified
|
||||||
|
indent'
|
||||||
|
| Pretty indentation <- formatter = indent $ indentation + 1
|
||||||
|
| otherwise = ""
|
||||||
|
|
||||||
|
colon :: Formatter -> Lazy.Text
|
||||||
|
colon formatter = eitherFormat formatter ": " ":"
|
||||||
|
|
||||||
|
field :: Formatter -> Full.Field -> Lazy.Text
|
||||||
|
field formatter (Full.Field alias name args dirs set)
|
||||||
|
= optempty prependAlias (fold alias)
|
||||||
|
<> Lazy.Text.fromStrict name
|
||||||
|
<> optempty (arguments formatter) args
|
||||||
|
<> optempty (directives formatter) dirs
|
||||||
|
<> optempty selectionSetOpt' set
|
||||||
|
where
|
||||||
|
prependAlias aliasName = Lazy.Text.fromStrict aliasName <> colon formatter
|
||||||
|
selectionSetOpt' = (eitherFormat formatter " " "" <>)
|
||||||
|
. selectionSetOpt formatter
|
||||||
|
|
||||||
|
arguments :: Formatter -> [Full.Argument] -> Lazy.Text
|
||||||
|
arguments formatter = parensCommas formatter $ argument formatter
|
||||||
|
|
||||||
|
argument :: Formatter -> Full.Argument -> Lazy.Text
|
||||||
|
argument formatter (Full.Argument name value')
|
||||||
|
= Lazy.Text.fromStrict name
|
||||||
|
<> colon formatter
|
||||||
|
<> value formatter value'
|
||||||
|
|
||||||
|
-- * Fragments
|
||||||
|
|
||||||
|
fragmentSpread :: Formatter -> Full.FragmentSpread -> Lazy.Text
|
||||||
|
fragmentSpread formatter (Full.FragmentSpread name ds)
|
||||||
|
= "..." <> Lazy.Text.fromStrict name <> optempty (directives formatter) ds
|
||||||
|
|
||||||
|
inlineFragment :: Formatter -> Full.InlineFragment -> Lazy.Text
|
||||||
|
inlineFragment formatter (Full.InlineFragment tc dirs sels)
|
||||||
|
= "... on "
|
||||||
|
<> Lazy.Text.fromStrict (fold tc)
|
||||||
|
<> directives formatter dirs
|
||||||
|
<> eitherFormat formatter " " mempty
|
||||||
|
<> selectionSet formatter sels
|
||||||
|
|
||||||
|
fragmentDefinition :: Formatter -> Full.FragmentDefinition -> Lazy.Text
|
||||||
|
fragmentDefinition formatter (Full.FragmentDefinition name tc dirs sels)
|
||||||
|
= "fragment " <> Lazy.Text.fromStrict name
|
||||||
|
<> " on " <> Lazy.Text.fromStrict tc
|
||||||
|
<> optempty (directives formatter) dirs
|
||||||
|
<> eitherFormat formatter " " mempty
|
||||||
|
<> selectionSet formatter sels
|
||||||
|
|
||||||
|
-- * Miscellaneous
|
||||||
|
|
||||||
|
-- | Converts a 'Full.Directive' into a string.
|
||||||
|
directive :: Formatter -> Full.Directive -> Lazy.Text
|
||||||
|
directive formatter (Full.Directive name args)
|
||||||
|
= "@" <> Lazy.Text.fromStrict name <> optempty (arguments formatter) args
|
||||||
|
|
||||||
|
directives :: Formatter -> [Full.Directive] -> Lazy.Text
|
||||||
|
directives Minified = spaces (directive Minified)
|
||||||
|
directives formatter = Lazy.Text.cons ' ' . spaces (directive formatter)
|
||||||
|
|
||||||
|
-- | Converts a 'Full.Value' into a string.
|
||||||
|
value :: Formatter -> Full.Value -> Lazy.Text
|
||||||
|
value _ (Full.Variable x) = variable x
|
||||||
|
value _ (Full.Int x) = Builder.toLazyText $ decimal x
|
||||||
|
value _ (Full.Float x) = Builder.toLazyText $ realFloat x
|
||||||
|
value _ (Full.Boolean x) = booleanValue x
|
||||||
|
value _ Full.Null = mempty
|
||||||
|
value formatter (Full.String string) = stringValue formatter string
|
||||||
|
value _ (Full.Enum x) = Lazy.Text.fromStrict x
|
||||||
|
value formatter (Full.List x) = listValue formatter x
|
||||||
|
value formatter (Full.Object x) = objectValue formatter x
|
||||||
|
|
||||||
|
booleanValue :: Bool -> Lazy.Text
|
||||||
|
booleanValue True = "true"
|
||||||
|
booleanValue False = "false"
|
||||||
|
|
||||||
|
stringValue :: Formatter -> Text -> Lazy.Text
|
||||||
|
stringValue Minified string = Builder.toLazyText
|
||||||
|
$ quote <> Text.foldr (mappend . escape') quote string
|
||||||
|
where
|
||||||
|
quote = Builder.singleton '\"'
|
||||||
|
escape' '\n' = Builder.fromString "\\n"
|
||||||
|
escape' char = escape char
|
||||||
|
stringValue (Pretty indentation) string = byStringType $ Text.lines string
|
||||||
|
where
|
||||||
|
byStringType [] = "\"\""
|
||||||
|
byStringType [line] = Builder.toLazyText
|
||||||
|
$ quote <> Text.foldr (mappend . escape) quote line
|
||||||
|
byStringType lines' = "\"\"\"\n"
|
||||||
|
<> Lazy.Text.unlines (transformLine <$> lines')
|
||||||
|
<> indent indentation
|
||||||
|
<> "\"\"\""
|
||||||
|
transformLine = (indent (indentation + 1) <>)
|
||||||
|
. Lazy.Text.fromStrict
|
||||||
|
. Text.replace "\"\"\"" "\\\"\"\""
|
||||||
|
quote = Builder.singleton '\"'
|
||||||
|
|
||||||
|
escape :: Char -> Builder
|
||||||
|
escape char'
|
||||||
|
| char' == '\\' = Builder.fromString "\\\\"
|
||||||
|
| char' == '\"' = Builder.fromString "\\\""
|
||||||
|
| char' == '\b' = Builder.fromString "\\b"
|
||||||
|
| char' == '\f' = Builder.fromString "\\f"
|
||||||
|
| char' == '\r' = Builder.fromString "\\r"
|
||||||
|
| char' < '\x0010' = unicode "\\u000" char'
|
||||||
|
| char' < '\x0020' = unicode "\\u00" char'
|
||||||
|
| otherwise = Builder.singleton char'
|
||||||
|
where
|
||||||
|
unicode prefix = mappend (Builder.fromString prefix) . (hexadecimal . ord)
|
||||||
|
|
||||||
|
listValue :: Formatter -> [Full.Value] -> Lazy.Text
|
||||||
|
listValue formatter = bracketsCommas formatter $ value formatter
|
||||||
|
|
||||||
|
objectValue :: Formatter -> [Full.ObjectField] -> Lazy.Text
|
||||||
|
objectValue formatter = intercalate $ objectField formatter
|
||||||
|
where
|
||||||
|
intercalate f
|
||||||
|
= braces
|
||||||
|
. Lazy.Text.intercalate (eitherFormat formatter ", " ",")
|
||||||
|
. fmap f
|
||||||
|
|
||||||
|
objectField :: Formatter -> Full.ObjectField -> Lazy.Text
|
||||||
|
objectField formatter (Full.ObjectField name value') =
|
||||||
|
Lazy.Text.fromStrict name <> colon formatter <> value formatter value'
|
||||||
|
|
||||||
|
-- | Converts a 'Full.Type' a type into a string.
|
||||||
|
type' :: Full.Type -> Lazy.Text
|
||||||
|
type' (Full.TypeNamed x) = Lazy.Text.fromStrict x
|
||||||
|
type' (Full.TypeList x) = listType x
|
||||||
|
type' (Full.TypeNonNull x) = nonNullType x
|
||||||
|
|
||||||
|
listType :: Full.Type -> Lazy.Text
|
||||||
|
listType x = brackets (type' x)
|
||||||
|
|
||||||
|
nonNullType :: Full.NonNullType -> Lazy.Text
|
||||||
|
nonNullType (Full.NonNullTypeNamed x) = Lazy.Text.fromStrict x <> "!"
|
||||||
|
nonNullType (Full.NonNullTypeList x) = listType x <> "!"
|
||||||
|
|
||||||
|
-- * Internal
|
||||||
|
|
||||||
|
between :: Char -> Char -> Lazy.Text -> Lazy.Text
|
||||||
|
between open close = Lazy.Text.cons open . (`Lazy.Text.snoc` close)
|
||||||
|
|
||||||
|
parens :: Lazy.Text -> Lazy.Text
|
||||||
|
parens = between '(' ')'
|
||||||
|
|
||||||
|
brackets :: Lazy.Text -> Lazy.Text
|
||||||
|
brackets = between '[' ']'
|
||||||
|
|
||||||
|
braces :: Lazy.Text -> Lazy.Text
|
||||||
|
braces = between '{' '}'
|
||||||
|
|
||||||
|
spaces :: forall a. (a -> Lazy.Text) -> [a] -> Lazy.Text
|
||||||
|
spaces f = Lazy.Text.intercalate "\SP" . fmap f
|
||||||
|
|
||||||
|
parensCommas :: forall a. Formatter -> (a -> Lazy.Text) -> [a] -> Lazy.Text
|
||||||
|
parensCommas formatter f
|
||||||
|
= parens
|
||||||
|
. Lazy.Text.intercalate (eitherFormat formatter ", " ",")
|
||||||
|
. fmap f
|
||||||
|
|
||||||
|
bracketsCommas :: Formatter -> (a -> Lazy.Text) -> [a] -> Lazy.Text
|
||||||
|
bracketsCommas formatter f
|
||||||
|
= brackets
|
||||||
|
. Lazy.Text.intercalate (eitherFormat formatter ", " ",")
|
||||||
|
. fmap f
|
||||||
|
|
||||||
|
bracesList :: forall a. Formatter -> (a -> Lazy.Text) -> [a] -> Lazy.Text
|
||||||
|
bracesList (Pretty intendation) f xs
|
||||||
|
= Lazy.Text.snoc (Lazy.Text.intercalate "\n" content) '\n'
|
||||||
|
<> (Lazy.Text.snoc $ Lazy.Text.replicate (fromIntegral intendation) " ") '}'
|
||||||
|
where
|
||||||
|
content = "{" : fmap f xs
|
||||||
|
bracesList Minified f xs = braces $ Lazy.Text.intercalate "," $ fmap f xs
|
||||||
|
|
||||||
|
optempty :: (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
|
||||||
|
optempty f xs = if xs == mempty then mempty else f xs
|
||||||
|
|
||||||
|
eitherFormat :: forall a. Formatter -> a -> a -> a
|
||||||
|
eitherFormat (Pretty _) x _ = x
|
||||||
|
eitherFormat Minified _ x = x
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
-- | This module defines a bunch of small parsers used to parse individual
|
-- | This module defines a bunch of small parsers used to parse individual
|
||||||
-- lexemes.
|
-- lexemes.
|
||||||
module Language.GraphQL.Lexer
|
module Language.GraphQL.AST.Lexer
|
||||||
( Parser
|
( Parser
|
||||||
, amp
|
, amp
|
||||||
, at
|
, at
|
||||||
@ -89,12 +89,12 @@ symbol :: T.Text -> Parser T.Text
|
|||||||
symbol = Lexer.symbol spaceConsumer
|
symbol = Lexer.symbol spaceConsumer
|
||||||
|
|
||||||
-- | Parser for "!".
|
-- | Parser for "!".
|
||||||
bang :: Parser Char
|
bang :: Parser T.Text
|
||||||
bang = char '!'
|
bang = symbol "!"
|
||||||
|
|
||||||
-- | Parser for "$".
|
-- | Parser for "$".
|
||||||
dollar :: Parser Char
|
dollar :: Parser T.Text
|
||||||
dollar = char '$'
|
dollar = symbol "$"
|
||||||
|
|
||||||
-- | Parser for "@".
|
-- | Parser for "@".
|
||||||
at :: Parser Char
|
at :: Parser Char
|
||||||
@ -134,7 +134,7 @@ braces = between (symbol "{") (symbol "}")
|
|||||||
|
|
||||||
-- | Parser for strings.
|
-- | Parser for strings.
|
||||||
string :: Parser T.Text
|
string :: Parser T.Text
|
||||||
string = between "\"" "\"" stringValue
|
string = between "\"" "\"" stringValue <* spaceConsumer
|
||||||
where
|
where
|
||||||
stringValue = T.pack <$> many stringCharacter
|
stringValue = T.pack <$> many stringCharacter
|
||||||
stringCharacter = satisfy isStringCharacter1
|
stringCharacter = satisfy isStringCharacter1
|
||||||
@ -143,7 +143,7 @@ string = between "\"" "\"" stringValue
|
|||||||
|
|
||||||
-- | Parser for block strings.
|
-- | Parser for block strings.
|
||||||
blockString :: Parser T.Text
|
blockString :: Parser T.Text
|
||||||
blockString = between "\"\"\"" "\"\"\"" stringValue
|
blockString = between "\"\"\"" "\"\"\"" stringValue <* spaceConsumer
|
||||||
where
|
where
|
||||||
stringValue = do
|
stringValue = do
|
||||||
byLine <- sepBy (many blockStringCharacter) lineTerminator
|
byLine <- sepBy (many blockStringCharacter) lineTerminator
|
@ -2,7 +2,7 @@
|
|||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
-- | @GraphQL@ document parser.
|
-- | @GraphQL@ document parser.
|
||||||
module Language.GraphQL.Parser
|
module Language.GraphQL.AST.Parser
|
||||||
( document
|
( document
|
||||||
) where
|
) where
|
||||||
|
|
||||||
@ -11,7 +11,7 @@ import Control.Applicative ( Alternative(..)
|
|||||||
)
|
)
|
||||||
import Data.List.NonEmpty (NonEmpty(..))
|
import Data.List.NonEmpty (NonEmpty(..))
|
||||||
import Language.GraphQL.AST
|
import Language.GraphQL.AST
|
||||||
import Language.GraphQL.Lexer
|
import Language.GraphQL.AST.Lexer
|
||||||
import Text.Megaparsec ( lookAhead
|
import Text.Megaparsec ( lookAhead
|
||||||
, option
|
, option
|
||||||
, try
|
, try
|
||||||
@ -105,16 +105,16 @@ typeCondition = symbol "on" *> name
|
|||||||
-- * Input Values
|
-- * Input Values
|
||||||
|
|
||||||
value :: Parser Value
|
value :: Parser Value
|
||||||
value = ValueVariable <$> variable
|
value = Variable <$> variable
|
||||||
<|> ValueFloat <$> try float
|
<|> Float <$> try float
|
||||||
<|> ValueInt <$> integer
|
<|> Int <$> integer
|
||||||
<|> ValueBoolean <$> booleanValue
|
<|> Boolean <$> booleanValue
|
||||||
<|> ValueNull <$ symbol "null"
|
<|> Null <$ symbol "null"
|
||||||
<|> ValueString <$> blockString
|
<|> String <$> blockString
|
||||||
<|> ValueString <$> string
|
<|> String <$> string
|
||||||
<|> ValueEnum <$> try enumValue
|
<|> Enum <$> try enumValue
|
||||||
<|> ValueList <$> listValue
|
<|> List <$> listValue
|
||||||
<|> ValueObject <$> objectValue
|
<|> Object <$> objectValue
|
||||||
<?> "value error!"
|
<?> "value error!"
|
||||||
where
|
where
|
||||||
booleanValue :: Parser Bool
|
booleanValue :: Parser Bool
|
||||||
@ -152,9 +152,9 @@ defaultValue = equals *> value
|
|||||||
-- * Input Types
|
-- * Input Types
|
||||||
|
|
||||||
type_ :: Parser Type
|
type_ :: Parser Type
|
||||||
type_ = try (TypeNamed <$> name <* but "!")
|
type_ = try (TypeNonNull <$> nonNullType)
|
||||||
<|> TypeList <$> brackets type_
|
<|> TypeList <$> brackets type_
|
||||||
<|> TypeNonNull <$> nonNullType
|
<|> TypeNamed <$> name
|
||||||
<?> "type_ error!"
|
<?> "type_ error!"
|
||||||
|
|
||||||
nonNullType :: Parser NonNullType
|
nonNullType :: Parser NonNullType
|
@ -1,136 +0,0 @@
|
|||||||
{-# LANGUAGE OverloadedStrings #-}
|
|
||||||
|
|
||||||
-- | After the document is parsed, before getting executed the AST is
|
|
||||||
-- transformed into a similar, simpler AST. This module is responsible for
|
|
||||||
-- this transformation.
|
|
||||||
module Language.GraphQL.AST.Transform
|
|
||||||
( document
|
|
||||||
) where
|
|
||||||
|
|
||||||
import Control.Applicative (empty)
|
|
||||||
import Data.Bifunctor (first)
|
|
||||||
import Data.Either (partitionEithers)
|
|
||||||
import Data.Foldable (fold, foldMap)
|
|
||||||
import Data.List.NonEmpty (NonEmpty)
|
|
||||||
import qualified Data.List.NonEmpty as NonEmpty
|
|
||||||
import Data.Monoid (Alt(Alt,getAlt), (<>))
|
|
||||||
import qualified Language.GraphQL.AST as Full
|
|
||||||
import qualified Language.GraphQL.AST.Core as Core
|
|
||||||
import qualified Language.GraphQL.Schema as Schema
|
|
||||||
|
|
||||||
-- | Replaces a fragment name by a list of 'Core.Field'. If the name doesn't
|
|
||||||
-- match an empty list is returned.
|
|
||||||
type Fragmenter = Core.Name -> [Core.Field]
|
|
||||||
|
|
||||||
-- | Rewrites the original syntax tree into an intermediate representation used
|
|
||||||
-- for query execution.
|
|
||||||
document :: Schema.Subs -> Full.Document -> Maybe Core.Document
|
|
||||||
document subs doc = operations subs fr ops
|
|
||||||
where
|
|
||||||
(fr, ops) = first foldFrags
|
|
||||||
. partitionEithers
|
|
||||||
. NonEmpty.toList
|
|
||||||
$ defrag subs
|
|
||||||
<$> doc
|
|
||||||
|
|
||||||
foldFrags :: [Fragmenter] -> Fragmenter
|
|
||||||
foldFrags fs n = getAlt $ foldMap (Alt . ($ n)) fs
|
|
||||||
|
|
||||||
-- * Operation
|
|
||||||
|
|
||||||
-- TODO: Replace Maybe by MonadThrow CustomError
|
|
||||||
operations
|
|
||||||
:: Schema.Subs
|
|
||||||
-> Fragmenter
|
|
||||||
-> [Full.OperationDefinition]
|
|
||||||
-> Maybe Core.Document
|
|
||||||
operations subs fr = NonEmpty.nonEmpty . fmap (operation subs fr)
|
|
||||||
|
|
||||||
operation
|
|
||||||
:: Schema.Subs
|
|
||||||
-> Fragmenter
|
|
||||||
-> Full.OperationDefinition
|
|
||||||
-> Core.Operation
|
|
||||||
operation subs fr (Full.OperationSelectionSet sels) =
|
|
||||||
operation subs fr $ Full.OperationDefinition Full.Query empty empty empty sels
|
|
||||||
-- TODO: Validate Variable definitions with substituter
|
|
||||||
operation subs fr (Full.OperationDefinition Full.Query name _vars _dirs sels) =
|
|
||||||
Core.Query name $ appendSelection subs fr sels
|
|
||||||
operation subs fr (Full.OperationDefinition Full.Mutation name _vars _dirs sels) =
|
|
||||||
Core.Mutation name $ appendSelection subs fr sels
|
|
||||||
|
|
||||||
selection
|
|
||||||
:: Schema.Subs
|
|
||||||
-> Fragmenter
|
|
||||||
-> Full.Selection
|
|
||||||
-> Either [Core.Selection] Core.Selection
|
|
||||||
selection subs fr (Full.SelectionField fld)
|
|
||||||
= Right $ Core.SelectionField $ field subs fr fld
|
|
||||||
selection _ fr (Full.SelectionFragmentSpread (Full.FragmentSpread name _))
|
|
||||||
= Left $ Core.SelectionField <$> fr name
|
|
||||||
selection subs fr (Full.SelectionInlineFragment fragment)
|
|
||||||
| (Full.InlineFragment (Just typeCondition) _ selectionSet) <- fragment
|
|
||||||
= Right
|
|
||||||
$ Core.SelectionFragment
|
|
||||||
$ Core.Fragment typeCondition
|
|
||||||
$ appendSelection subs fr selectionSet
|
|
||||||
| (Full.InlineFragment Nothing _ selectionSet) <- fragment
|
|
||||||
= Left $ NonEmpty.toList $ appendSelection subs fr selectionSet
|
|
||||||
|
|
||||||
-- * Fragment replacement
|
|
||||||
|
|
||||||
-- | Extract Fragments into a single Fragmenter function and a Operation
|
|
||||||
-- Definition.
|
|
||||||
defrag
|
|
||||||
:: Schema.Subs
|
|
||||||
-> Full.Definition
|
|
||||||
-> Either Fragmenter Full.OperationDefinition
|
|
||||||
defrag _ (Full.DefinitionOperation op) =
|
|
||||||
Right op
|
|
||||||
defrag subs (Full.DefinitionFragment fragDef) =
|
|
||||||
Left $ fragmentDefinition subs fragDef
|
|
||||||
|
|
||||||
fragmentDefinition :: Schema.Subs -> Full.FragmentDefinition -> Fragmenter
|
|
||||||
fragmentDefinition subs (Full.FragmentDefinition name _tc _dirs sels) name'
|
|
||||||
| name == name' = selection' <$> do
|
|
||||||
selections <- NonEmpty.toList $ selection subs mempty <$> sels
|
|
||||||
either id pure selections
|
|
||||||
| otherwise = empty
|
|
||||||
where
|
|
||||||
selection' (Core.SelectionField field') = field'
|
|
||||||
selection' _ = error "Fragments within fragments are not supported yet"
|
|
||||||
|
|
||||||
field :: Schema.Subs -> Fragmenter -> Full.Field -> Core.Field
|
|
||||||
field subs fr (Full.Field a n args _dirs sels) =
|
|
||||||
Core.Field a n (fold $ argument subs `traverse` args) (foldr go empty sels)
|
|
||||||
where
|
|
||||||
go :: Full.Selection -> [Core.Selection] -> [Core.Selection]
|
|
||||||
go (Full.SelectionFragmentSpread (Full.FragmentSpread name _dirs)) = ((Core.SelectionField <$> fr name) <>)
|
|
||||||
go sel = (either id pure (selection subs fr sel) <>)
|
|
||||||
|
|
||||||
argument :: Schema.Subs -> Full.Argument -> Maybe Core.Argument
|
|
||||||
argument subs (Full.Argument n v) = Core.Argument n <$> value subs v
|
|
||||||
|
|
||||||
value :: Schema.Subs -> Full.Value -> Maybe Core.Value
|
|
||||||
value subs (Full.ValueVariable n) = subs n
|
|
||||||
value _ (Full.ValueInt i) = pure $ Core.ValueInt i
|
|
||||||
value _ (Full.ValueFloat f) = pure $ Core.ValueFloat f
|
|
||||||
value _ (Full.ValueString x) = pure $ Core.ValueString x
|
|
||||||
value _ (Full.ValueBoolean b) = pure $ Core.ValueBoolean b
|
|
||||||
value _ Full.ValueNull = pure Core.ValueNull
|
|
||||||
value _ (Full.ValueEnum e) = pure $ Core.ValueEnum e
|
|
||||||
value subs (Full.ValueList l) =
|
|
||||||
Core.ValueList <$> traverse (value subs) l
|
|
||||||
value subs (Full.ValueObject o) =
|
|
||||||
Core.ValueObject <$> traverse (objectField subs) o
|
|
||||||
|
|
||||||
objectField :: Schema.Subs -> Full.ObjectField -> Maybe Core.ObjectField
|
|
||||||
objectField subs (Full.ObjectField n v) = Core.ObjectField n <$> value subs v
|
|
||||||
|
|
||||||
appendSelection ::
|
|
||||||
Schema.Subs ->
|
|
||||||
Fragmenter ->
|
|
||||||
NonEmpty Full.Selection ->
|
|
||||||
NonEmpty Core.Selection
|
|
||||||
appendSelection subs fr = NonEmpty.fromList
|
|
||||||
. foldr (either (++) (:) . selection subs fr) []
|
|
@ -1,277 +0,0 @@
|
|||||||
{-# LANGUAGE OverloadedStrings #-}
|
|
||||||
{-# LANGUAGE ExplicitForAll #-}
|
|
||||||
|
|
||||||
-- | This module defines a minifier and a printer for the @GraphQL@ language.
|
|
||||||
module Language.GraphQL.Encoder
|
|
||||||
( Formatter
|
|
||||||
, definition
|
|
||||||
, directive
|
|
||||||
, document
|
|
||||||
, minified
|
|
||||||
, pretty
|
|
||||||
, type'
|
|
||||||
, value
|
|
||||||
) where
|
|
||||||
|
|
||||||
import Data.Foldable (fold)
|
|
||||||
import Data.Monoid ((<>))
|
|
||||||
import qualified Data.List.NonEmpty as NonEmpty (toList)
|
|
||||||
import Data.Text.Lazy (Text)
|
|
||||||
import qualified Data.Text.Lazy as Text.Lazy
|
|
||||||
import Data.Text.Lazy.Builder (toLazyText)
|
|
||||||
import Data.Text.Lazy.Builder.Int (decimal)
|
|
||||||
import Data.Text.Lazy.Builder.RealFloat (realFloat)
|
|
||||||
import Language.GraphQL.AST
|
|
||||||
|
|
||||||
-- | Instructs the encoder whether a GraphQL should be minified or pretty
|
|
||||||
-- printed.
|
|
||||||
--
|
|
||||||
-- Use 'pretty' and 'minified' to construct the formatter.
|
|
||||||
data Formatter
|
|
||||||
= Minified
|
|
||||||
| Pretty Word
|
|
||||||
|
|
||||||
-- | Constructs a formatter for pretty printing.
|
|
||||||
pretty :: Formatter
|
|
||||||
pretty = Pretty 0
|
|
||||||
|
|
||||||
-- | Constructs a formatter for minifying.
|
|
||||||
minified :: Formatter
|
|
||||||
minified = Minified
|
|
||||||
|
|
||||||
-- | Converts a 'Document' into a string.
|
|
||||||
document :: Formatter -> Document -> Text
|
|
||||||
document formatter defs
|
|
||||||
| Pretty _ <- formatter = Text.Lazy.intercalate "\n" encodeDocument
|
|
||||||
| Minified <-formatter = Text.Lazy.snoc (mconcat encodeDocument) '\n'
|
|
||||||
where
|
|
||||||
encodeDocument = NonEmpty.toList $ definition formatter <$> defs
|
|
||||||
|
|
||||||
-- | Converts a 'Definition' into a string.
|
|
||||||
definition :: Formatter -> Definition -> Text
|
|
||||||
definition formatter x
|
|
||||||
| Pretty _ <- formatter = Text.Lazy.snoc (encodeDefinition x) '\n'
|
|
||||||
| Minified <- formatter = encodeDefinition x
|
|
||||||
where
|
|
||||||
encodeDefinition (DefinitionOperation operation)
|
|
||||||
= operationDefinition formatter operation
|
|
||||||
encodeDefinition (DefinitionFragment fragment)
|
|
||||||
= fragmentDefinition formatter fragment
|
|
||||||
|
|
||||||
operationDefinition :: Formatter -> OperationDefinition -> Text
|
|
||||||
operationDefinition formatter (OperationSelectionSet sels)
|
|
||||||
= selectionSet formatter sels
|
|
||||||
operationDefinition formatter (OperationDefinition Query name vars dirs sels)
|
|
||||||
= "query " <> node formatter name vars dirs sels
|
|
||||||
operationDefinition formatter (OperationDefinition Mutation name vars dirs sels)
|
|
||||||
= "mutation " <> node formatter name vars dirs sels
|
|
||||||
|
|
||||||
node :: Formatter
|
|
||||||
-> Maybe Name
|
|
||||||
-> [VariableDefinition]
|
|
||||||
-> [Directive]
|
|
||||||
-> SelectionSet
|
|
||||||
-> Text
|
|
||||||
node formatter name vars dirs sels
|
|
||||||
= Text.Lazy.fromStrict (fold name)
|
|
||||||
<> optempty (variableDefinitions formatter) vars
|
|
||||||
<> optempty (directives formatter) dirs
|
|
||||||
<> eitherFormat formatter " " mempty
|
|
||||||
<> selectionSet formatter sels
|
|
||||||
|
|
||||||
variableDefinitions :: Formatter -> [VariableDefinition] -> Text
|
|
||||||
variableDefinitions formatter
|
|
||||||
= parensCommas formatter $ variableDefinition formatter
|
|
||||||
|
|
||||||
variableDefinition :: Formatter -> VariableDefinition -> Text
|
|
||||||
variableDefinition formatter (VariableDefinition var ty dv)
|
|
||||||
= variable var
|
|
||||||
<> eitherFormat formatter ": " ":"
|
|
||||||
<> type' ty
|
|
||||||
<> maybe mempty (defaultValue formatter) dv
|
|
||||||
|
|
||||||
defaultValue :: Formatter -> Value -> Text
|
|
||||||
defaultValue formatter val
|
|
||||||
= eitherFormat formatter " = " "="
|
|
||||||
<> value formatter val
|
|
||||||
|
|
||||||
variable :: Name -> Text
|
|
||||||
variable var = "$" <> Text.Lazy.fromStrict var
|
|
||||||
|
|
||||||
selectionSet :: Formatter -> SelectionSet -> Text
|
|
||||||
selectionSet formatter
|
|
||||||
= bracesList formatter (selection formatter)
|
|
||||||
. NonEmpty.toList
|
|
||||||
|
|
||||||
selectionSetOpt :: Formatter -> SelectionSetOpt -> Text
|
|
||||||
selectionSetOpt formatter = bracesList formatter $ selection formatter
|
|
||||||
|
|
||||||
selection :: Formatter -> Selection -> Text
|
|
||||||
selection formatter = Text.Lazy.append indent . f
|
|
||||||
where
|
|
||||||
f (SelectionField x) = field incrementIndent x
|
|
||||||
f (SelectionInlineFragment x) = inlineFragment incrementIndent x
|
|
||||||
f (SelectionFragmentSpread x) = fragmentSpread incrementIndent x
|
|
||||||
incrementIndent
|
|
||||||
| Pretty n <- formatter = Pretty $ n + 1
|
|
||||||
| otherwise = Minified
|
|
||||||
indent
|
|
||||||
| Pretty n <- formatter = Text.Lazy.replicate (fromIntegral $ n + 1) " "
|
|
||||||
| otherwise = mempty
|
|
||||||
|
|
||||||
field :: Formatter -> Field -> Text
|
|
||||||
field formatter (Field alias name args dirs selso)
|
|
||||||
= optempty (`Text.Lazy.append` colon) (Text.Lazy.fromStrict $ fold alias)
|
|
||||||
<> Text.Lazy.fromStrict name
|
|
||||||
<> optempty (arguments formatter) args
|
|
||||||
<> optempty (directives formatter) dirs
|
|
||||||
<> selectionSetOpt'
|
|
||||||
where
|
|
||||||
colon = eitherFormat formatter ": " ":"
|
|
||||||
selectionSetOpt'
|
|
||||||
| null selso = mempty
|
|
||||||
| otherwise = eitherFormat formatter " " mempty <> selectionSetOpt formatter selso
|
|
||||||
|
|
||||||
arguments :: Formatter -> [Argument] -> Text
|
|
||||||
arguments formatter = parensCommas formatter $ argument formatter
|
|
||||||
|
|
||||||
argument :: Formatter -> Argument -> Text
|
|
||||||
argument formatter (Argument name v)
|
|
||||||
= Text.Lazy.fromStrict name
|
|
||||||
<> eitherFormat formatter ": " ":"
|
|
||||||
<> value formatter v
|
|
||||||
|
|
||||||
-- * Fragments
|
|
||||||
|
|
||||||
fragmentSpread :: Formatter -> FragmentSpread -> Text
|
|
||||||
fragmentSpread formatter (FragmentSpread name ds)
|
|
||||||
= "..." <> Text.Lazy.fromStrict name <> optempty (directives formatter) ds
|
|
||||||
|
|
||||||
inlineFragment :: Formatter -> InlineFragment -> Text
|
|
||||||
inlineFragment formatter (InlineFragment tc dirs sels)
|
|
||||||
= "... on "
|
|
||||||
<> Text.Lazy.fromStrict (fold tc)
|
|
||||||
<> directives formatter dirs
|
|
||||||
<> eitherFormat formatter " " mempty
|
|
||||||
<> selectionSet formatter sels
|
|
||||||
|
|
||||||
fragmentDefinition :: Formatter -> FragmentDefinition -> Text
|
|
||||||
fragmentDefinition formatter (FragmentDefinition name tc dirs sels)
|
|
||||||
= "fragment " <> Text.Lazy.fromStrict name
|
|
||||||
<> " on " <> Text.Lazy.fromStrict tc
|
|
||||||
<> optempty (directives formatter) dirs
|
|
||||||
<> eitherFormat formatter " " mempty
|
|
||||||
<> selectionSet formatter sels
|
|
||||||
|
|
||||||
-- * Miscellaneous
|
|
||||||
|
|
||||||
-- | Converts a 'Directive' into a string.
|
|
||||||
directive :: Formatter -> Directive -> Text
|
|
||||||
directive formatter (Directive name args)
|
|
||||||
= "@" <> Text.Lazy.fromStrict name <> optempty (arguments formatter) args
|
|
||||||
|
|
||||||
directives :: Formatter -> [Directive] -> Text
|
|
||||||
directives formatter@(Pretty _) = Text.Lazy.cons ' ' . spaces (directive formatter)
|
|
||||||
directives Minified = spaces (directive Minified)
|
|
||||||
|
|
||||||
-- | Converts a 'Value' into a string.
|
|
||||||
value :: Formatter -> Value -> Text
|
|
||||||
value _ (ValueVariable x) = variable x
|
|
||||||
value _ (ValueInt x) = toLazyText $ decimal x
|
|
||||||
value _ (ValueFloat x) = toLazyText $ realFloat x
|
|
||||||
value _ (ValueBoolean x) = booleanValue x
|
|
||||||
value _ ValueNull = mempty
|
|
||||||
value _ (ValueString x) = stringValue $ Text.Lazy.fromStrict x
|
|
||||||
value _ (ValueEnum x) = Text.Lazy.fromStrict x
|
|
||||||
value formatter (ValueList x) = listValue formatter x
|
|
||||||
value formatter (ValueObject x) = objectValue formatter x
|
|
||||||
|
|
||||||
booleanValue :: Bool -> Text
|
|
||||||
booleanValue True = "true"
|
|
||||||
booleanValue False = "false"
|
|
||||||
|
|
||||||
stringValue :: Text -> Text
|
|
||||||
stringValue
|
|
||||||
= quotes
|
|
||||||
. Text.Lazy.replace "\"" "\\\""
|
|
||||||
. Text.Lazy.replace "\\" "\\\\"
|
|
||||||
|
|
||||||
listValue :: Formatter -> [Value] -> Text
|
|
||||||
listValue formatter = bracketsCommas formatter $ value formatter
|
|
||||||
|
|
||||||
objectValue :: Formatter -> [ObjectField] -> Text
|
|
||||||
objectValue formatter = intercalate $ objectField formatter
|
|
||||||
where
|
|
||||||
intercalate f
|
|
||||||
= braces
|
|
||||||
. Text.Lazy.intercalate (eitherFormat formatter ", " ",")
|
|
||||||
. fmap f
|
|
||||||
|
|
||||||
|
|
||||||
objectField :: Formatter -> ObjectField -> Text
|
|
||||||
objectField formatter (ObjectField name v)
|
|
||||||
= Text.Lazy.fromStrict name <> colon <> value formatter v
|
|
||||||
where
|
|
||||||
colon
|
|
||||||
| Pretty _ <- formatter = ": "
|
|
||||||
| Minified <- formatter = ":"
|
|
||||||
|
|
||||||
-- | Converts a 'Type' a type into a string.
|
|
||||||
type' :: Type -> Text
|
|
||||||
type' (TypeNamed x) = Text.Lazy.fromStrict x
|
|
||||||
type' (TypeList x) = listType x
|
|
||||||
type' (TypeNonNull x) = nonNullType x
|
|
||||||
|
|
||||||
listType :: Type -> Text
|
|
||||||
listType x = brackets (type' x)
|
|
||||||
|
|
||||||
nonNullType :: NonNullType -> Text
|
|
||||||
nonNullType (NonNullTypeNamed x) = Text.Lazy.fromStrict x <> "!"
|
|
||||||
nonNullType (NonNullTypeList x) = listType x <> "!"
|
|
||||||
|
|
||||||
-- * Internal
|
|
||||||
|
|
||||||
between :: Char -> Char -> Text -> Text
|
|
||||||
between open close = Text.Lazy.cons open . (`Text.Lazy.snoc` close)
|
|
||||||
|
|
||||||
parens :: Text -> Text
|
|
||||||
parens = between '(' ')'
|
|
||||||
|
|
||||||
brackets :: Text -> Text
|
|
||||||
brackets = between '[' ']'
|
|
||||||
|
|
||||||
braces :: Text -> Text
|
|
||||||
braces = between '{' '}'
|
|
||||||
|
|
||||||
quotes :: Text -> Text
|
|
||||||
quotes = between '"' '"'
|
|
||||||
|
|
||||||
spaces :: forall a. (a -> Text) -> [a] -> Text
|
|
||||||
spaces f = Text.Lazy.intercalate "\SP" . fmap f
|
|
||||||
|
|
||||||
parensCommas :: forall a. Formatter -> (a -> Text) -> [a] -> Text
|
|
||||||
parensCommas formatter f
|
|
||||||
= parens
|
|
||||||
. Text.Lazy.intercalate (eitherFormat formatter ", " ",")
|
|
||||||
. fmap f
|
|
||||||
|
|
||||||
bracketsCommas :: Formatter -> (a -> Text) -> [a] -> Text
|
|
||||||
bracketsCommas formatter f
|
|
||||||
= brackets
|
|
||||||
. Text.Lazy.intercalate (eitherFormat formatter ", " ",")
|
|
||||||
. fmap f
|
|
||||||
|
|
||||||
bracesList :: forall a. Formatter -> (a -> Text) -> [a] -> Text
|
|
||||||
bracesList (Pretty intendation) f xs
|
|
||||||
= Text.Lazy.snoc (Text.Lazy.intercalate "\n" content) '\n'
|
|
||||||
<> (Text.Lazy.snoc $ Text.Lazy.replicate (fromIntegral intendation) " ") '}'
|
|
||||||
where
|
|
||||||
content = "{" : fmap f xs
|
|
||||||
bracesList Minified f xs = braces $ Text.Lazy.intercalate "," $ fmap f xs
|
|
||||||
|
|
||||||
optempty :: (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b
|
|
||||||
optempty f xs = if xs == mempty then mempty else f xs
|
|
||||||
|
|
||||||
eitherFormat :: forall a. Formatter -> a -> a -> a
|
|
||||||
eitherFormat (Pretty _) x _ = x
|
|
||||||
eitherFormat Minified _ x = x
|
|
@ -8,13 +8,14 @@ module Language.GraphQL.Execute
|
|||||||
|
|
||||||
import Control.Monad.IO.Class (MonadIO)
|
import Control.Monad.IO.Class (MonadIO)
|
||||||
import qualified Data.Aeson as Aeson
|
import qualified Data.Aeson as Aeson
|
||||||
|
import Data.Foldable (toList)
|
||||||
import Data.List.NonEmpty (NonEmpty(..))
|
import Data.List.NonEmpty (NonEmpty(..))
|
||||||
import qualified Data.List.NonEmpty as NE
|
import qualified Data.List.NonEmpty as NE
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import qualified Data.Text as Text
|
import qualified Data.Text as Text
|
||||||
import qualified Language.GraphQL.AST as AST
|
import qualified Language.GraphQL.AST as AST
|
||||||
import qualified Language.GraphQL.AST.Core as AST.Core
|
import qualified Language.GraphQL.AST.Core as AST.Core
|
||||||
import qualified Language.GraphQL.AST.Transform as Transform
|
import qualified Language.GraphQL.Execute.Transform as Transform
|
||||||
import Language.GraphQL.Error
|
import Language.GraphQL.Error
|
||||||
import qualified Language.GraphQL.Schema as Schema
|
import qualified Language.GraphQL.Schema as Schema
|
||||||
|
|
||||||
@ -71,6 +72,6 @@ operation :: MonadIO m
|
|||||||
-> AST.Core.Operation
|
-> AST.Core.Operation
|
||||||
-> m Aeson.Value
|
-> m Aeson.Value
|
||||||
operation schema (AST.Core.Query _ flds)
|
operation schema (AST.Core.Query _ flds)
|
||||||
= runCollectErrs (Schema.resolve (NE.toList schema) (NE.toList flds))
|
= runCollectErrs (Schema.resolve (toList schema) flds)
|
||||||
operation schema (AST.Core.Mutation _ flds)
|
operation schema (AST.Core.Mutation _ flds)
|
||||||
= runCollectErrs (Schema.resolve (NE.toList schema) (NE.toList flds))
|
= runCollectErrs (Schema.resolve (toList schema) flds)
|
||||||
|
184
src/Language/GraphQL/Execute/Transform.hs
Normal file
184
src/Language/GraphQL/Execute/Transform.hs
Normal file
@ -0,0 +1,184 @@
|
|||||||
|
{-# LANGUAGE ExplicitForAll #-}
|
||||||
|
{-# LANGUAGE TupleSections #-}
|
||||||
|
|
||||||
|
-- | After the document is parsed, before getting executed the AST is
|
||||||
|
-- transformed into a similar, simpler AST. This module is responsible for
|
||||||
|
-- this transformation.
|
||||||
|
module Language.GraphQL.Execute.Transform
|
||||||
|
( document
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Control.Arrow (first)
|
||||||
|
import Control.Monad (foldM, unless)
|
||||||
|
import Control.Monad.Trans.Class (lift)
|
||||||
|
import Control.Monad.Trans.Reader (ReaderT, ask, runReaderT)
|
||||||
|
import Control.Monad.Trans.State (StateT, evalStateT, gets, modify)
|
||||||
|
import Data.HashMap.Strict (HashMap)
|
||||||
|
import qualified Data.HashMap.Strict as HashMap
|
||||||
|
import qualified Data.List.NonEmpty as NonEmpty
|
||||||
|
import Data.Sequence (Seq, (<|), (><))
|
||||||
|
import qualified Language.GraphQL.AST as Full
|
||||||
|
import qualified Language.GraphQL.AST.Core as Core
|
||||||
|
import qualified Language.GraphQL.Schema as Schema
|
||||||
|
import qualified Language.GraphQL.Type.Directive as Directive
|
||||||
|
|
||||||
|
-- | Associates a fragment name with a list of 'Core.Field's.
|
||||||
|
data Replacement = Replacement
|
||||||
|
{ fragments :: HashMap Core.Name Core.Fragment
|
||||||
|
, fragmentDefinitions :: HashMap Full.Name Full.FragmentDefinition
|
||||||
|
}
|
||||||
|
|
||||||
|
type TransformT a = StateT Replacement (ReaderT Schema.Subs Maybe) a
|
||||||
|
|
||||||
|
liftJust :: forall a. a -> TransformT a
|
||||||
|
liftJust = lift . lift . Just
|
||||||
|
|
||||||
|
-- | Rewrites the original syntax tree into an intermediate representation used
|
||||||
|
-- for query execution.
|
||||||
|
document :: Schema.Subs -> Full.Document -> Maybe Core.Document
|
||||||
|
document subs document' =
|
||||||
|
flip runReaderT subs
|
||||||
|
$ evalStateT (collectFragments >> operations operationDefinitions)
|
||||||
|
$ Replacement HashMap.empty fragmentTable
|
||||||
|
where
|
||||||
|
(fragmentTable, operationDefinitions) = foldr defragment mempty document'
|
||||||
|
defragment (Full.DefinitionOperation definition) acc =
|
||||||
|
(definition :) <$> acc
|
||||||
|
defragment (Full.DefinitionFragment definition) acc =
|
||||||
|
let (Full.FragmentDefinition name _ _ _) = definition
|
||||||
|
in first (HashMap.insert name definition) acc
|
||||||
|
|
||||||
|
-- * Operation
|
||||||
|
|
||||||
|
operations :: [Full.OperationDefinition] -> TransformT Core.Document
|
||||||
|
operations operations' = do
|
||||||
|
coreOperations <- traverse operation operations'
|
||||||
|
lift . lift $ NonEmpty.nonEmpty coreOperations
|
||||||
|
|
||||||
|
operation :: Full.OperationDefinition -> TransformT Core.Operation
|
||||||
|
operation (Full.OperationSelectionSet sels) =
|
||||||
|
operation $ Full.OperationDefinition Full.Query mempty mempty mempty sels
|
||||||
|
-- TODO: Validate Variable definitions with substituter
|
||||||
|
operation (Full.OperationDefinition Full.Query name _vars _dirs sels) =
|
||||||
|
Core.Query name <$> appendSelection sels
|
||||||
|
operation (Full.OperationDefinition Full.Mutation name _vars _dirs sels) =
|
||||||
|
Core.Mutation name <$> appendSelection sels
|
||||||
|
|
||||||
|
-- * Selection
|
||||||
|
|
||||||
|
selection ::
|
||||||
|
Full.Selection ->
|
||||||
|
TransformT (Either (Seq Core.Selection) Core.Selection)
|
||||||
|
selection (Full.SelectionField field') =
|
||||||
|
maybe (Left mempty) (Right . Core.SelectionField) <$> field field'
|
||||||
|
selection (Full.SelectionFragmentSpread fragment) =
|
||||||
|
maybe (Left mempty) (Right . Core.SelectionFragment)
|
||||||
|
<$> fragmentSpread fragment
|
||||||
|
selection (Full.SelectionInlineFragment fragment) =
|
||||||
|
inlineFragment fragment
|
||||||
|
|
||||||
|
appendSelection ::
|
||||||
|
Traversable t =>
|
||||||
|
t Full.Selection ->
|
||||||
|
TransformT (Seq Core.Selection)
|
||||||
|
appendSelection = foldM go mempty
|
||||||
|
where
|
||||||
|
go acc sel = append acc <$> selection sel
|
||||||
|
append acc (Left list) = list >< acc
|
||||||
|
append acc (Right one) = one <| acc
|
||||||
|
|
||||||
|
directives :: [Full.Directive] -> TransformT [Core.Directive]
|
||||||
|
directives = traverse directive
|
||||||
|
where
|
||||||
|
directive (Full.Directive directiveName directiveArguments) =
|
||||||
|
Core.Directive directiveName <$> arguments directiveArguments
|
||||||
|
|
||||||
|
-- * Fragment replacement
|
||||||
|
|
||||||
|
-- | Extract fragment definitions into a single 'HashMap'.
|
||||||
|
collectFragments :: TransformT ()
|
||||||
|
collectFragments = do
|
||||||
|
fragDefs <- gets fragmentDefinitions
|
||||||
|
let nextValue = head $ HashMap.elems fragDefs
|
||||||
|
unless (HashMap.null fragDefs) $ do
|
||||||
|
_ <- fragmentDefinition nextValue
|
||||||
|
collectFragments
|
||||||
|
|
||||||
|
inlineFragment ::
|
||||||
|
Full.InlineFragment ->
|
||||||
|
TransformT (Either (Seq Core.Selection) Core.Selection)
|
||||||
|
inlineFragment (Full.InlineFragment type' directives' selectionSet) = do
|
||||||
|
fragmentDirectives <- Directive.selection <$> directives directives'
|
||||||
|
case fragmentDirectives of
|
||||||
|
Nothing -> pure $ Left mempty
|
||||||
|
_ -> do
|
||||||
|
fragmentSelectionSet <- appendSelection selectionSet
|
||||||
|
pure $ maybe Left selectionFragment type' fragmentSelectionSet
|
||||||
|
where
|
||||||
|
selectionFragment typeName = Right
|
||||||
|
. Core.SelectionFragment
|
||||||
|
. Core.Fragment typeName
|
||||||
|
|
||||||
|
fragmentSpread :: Full.FragmentSpread -> TransformT (Maybe Core.Fragment)
|
||||||
|
fragmentSpread (Full.FragmentSpread name directives') = do
|
||||||
|
spreadDirectives <- Directive.selection <$> directives directives'
|
||||||
|
fragments' <- gets fragments
|
||||||
|
fragment <- maybe lookupDefinition liftJust (HashMap.lookup name fragments')
|
||||||
|
pure $ fragment <$ spreadDirectives
|
||||||
|
where
|
||||||
|
lookupDefinition = do
|
||||||
|
fragmentDefinitions' <- gets fragmentDefinitions
|
||||||
|
found <- lift . lift $ HashMap.lookup name fragmentDefinitions'
|
||||||
|
fragmentDefinition found
|
||||||
|
|
||||||
|
fragmentDefinition ::
|
||||||
|
Full.FragmentDefinition ->
|
||||||
|
TransformT Core.Fragment
|
||||||
|
fragmentDefinition (Full.FragmentDefinition name type' _ selections) = do
|
||||||
|
modify deleteFragmentDefinition
|
||||||
|
fragmentSelection <- appendSelection selections
|
||||||
|
let newValue = Core.Fragment type' fragmentSelection
|
||||||
|
modify $ insertFragment newValue
|
||||||
|
liftJust newValue
|
||||||
|
where
|
||||||
|
deleteFragmentDefinition (Replacement fragments' fragmentDefinitions') =
|
||||||
|
Replacement fragments' $ HashMap.delete name fragmentDefinitions'
|
||||||
|
insertFragment newValue (Replacement fragments' fragmentDefinitions') =
|
||||||
|
let newFragments = HashMap.insert name newValue fragments'
|
||||||
|
in Replacement newFragments fragmentDefinitions'
|
||||||
|
|
||||||
|
field :: Full.Field -> TransformT (Maybe Core.Field)
|
||||||
|
field (Full.Field alias name arguments' directives' selections) = do
|
||||||
|
fieldArguments <- traverse argument arguments'
|
||||||
|
fieldSelections <- appendSelection selections
|
||||||
|
fieldDirectives <- Directive.selection <$> directives directives'
|
||||||
|
let field' = Core.Field alias name fieldArguments fieldSelections
|
||||||
|
pure $ field' <$ fieldDirectives
|
||||||
|
|
||||||
|
arguments :: [Full.Argument] -> TransformT Core.Arguments
|
||||||
|
arguments = fmap Core.Arguments . foldM go HashMap.empty
|
||||||
|
where
|
||||||
|
go arguments' argument' = do
|
||||||
|
(Core.Argument name value') <- argument argument'
|
||||||
|
return $ HashMap.insert name value' arguments'
|
||||||
|
|
||||||
|
argument :: Full.Argument -> TransformT Core.Argument
|
||||||
|
argument (Full.Argument n v) = Core.Argument n <$> value v
|
||||||
|
|
||||||
|
value :: Full.Value -> TransformT Core.Value
|
||||||
|
value (Full.Variable n) = do
|
||||||
|
substitute' <- lift ask
|
||||||
|
lift . lift $ substitute' n
|
||||||
|
value (Full.Int i) = pure $ Core.Int i
|
||||||
|
value (Full.Float f) = pure $ Core.Float f
|
||||||
|
value (Full.String x) = pure $ Core.String x
|
||||||
|
value (Full.Boolean b) = pure $ Core.Boolean b
|
||||||
|
value Full.Null = pure Core.Null
|
||||||
|
value (Full.Enum e) = pure $ Core.Enum e
|
||||||
|
value (Full.List l) =
|
||||||
|
Core.List <$> traverse value l
|
||||||
|
value (Full.Object o) =
|
||||||
|
Core.Object . HashMap.fromList <$> traverse objectField o
|
||||||
|
|
||||||
|
objectField :: Full.ObjectField -> TransformT (Core.Name, Core.Value)
|
||||||
|
objectField (Full.ObjectField name value') = (name,) <$> value value'
|
@ -4,17 +4,12 @@
|
|||||||
-- functions for defining and manipulating schemas.
|
-- functions for defining and manipulating schemas.
|
||||||
module Language.GraphQL.Schema
|
module Language.GraphQL.Schema
|
||||||
( Resolver
|
( Resolver
|
||||||
, Schema
|
|
||||||
, Subs
|
, Subs
|
||||||
, object
|
, object
|
||||||
, objectA
|
, objectA
|
||||||
, scalar
|
, scalar
|
||||||
, scalarA
|
, scalarA
|
||||||
, enum
|
|
||||||
, enumA
|
|
||||||
, resolve
|
, resolve
|
||||||
, wrappedEnum
|
|
||||||
, wrappedEnumA
|
|
||||||
, wrappedObject
|
, wrappedObject
|
||||||
, wrappedObjectA
|
, wrappedObjectA
|
||||||
, wrappedScalar
|
, wrappedScalar
|
||||||
@ -28,23 +23,19 @@ module Language.GraphQL.Schema
|
|||||||
import Control.Monad.IO.Class (MonadIO(..))
|
import Control.Monad.IO.Class (MonadIO(..))
|
||||||
import Control.Monad.Trans.Class (lift)
|
import Control.Monad.Trans.Class (lift)
|
||||||
import Control.Monad.Trans.Except (runExceptT)
|
import Control.Monad.Trans.Except (runExceptT)
|
||||||
|
import Control.Monad.Trans.Reader (runReaderT)
|
||||||
import Data.Foldable (find, fold)
|
import Data.Foldable (find, fold)
|
||||||
import Data.List.NonEmpty (NonEmpty)
|
|
||||||
import Data.Maybe (fromMaybe)
|
import Data.Maybe (fromMaybe)
|
||||||
import qualified Data.Aeson as Aeson
|
import qualified Data.Aeson as Aeson
|
||||||
import Data.HashMap.Strict (HashMap)
|
import Data.HashMap.Strict (HashMap)
|
||||||
import qualified Data.HashMap.Strict as HashMap
|
import qualified Data.HashMap.Strict as HashMap
|
||||||
|
import Data.Sequence (Seq)
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
|
import Language.GraphQL.AST.Core
|
||||||
import Language.GraphQL.Error
|
import Language.GraphQL.Error
|
||||||
import Language.GraphQL.Trans
|
import Language.GraphQL.Trans
|
||||||
import Language.GraphQL.Type
|
import qualified Language.GraphQL.Type as Type
|
||||||
import Language.GraphQL.AST.Core
|
|
||||||
|
|
||||||
{-# DEPRECATED Schema "Use NonEmpty (Resolver m) instead" #-}
|
|
||||||
-- | A GraphQL schema.
|
|
||||||
-- @m@ is usually expected to be an instance of 'MonadIO'.
|
|
||||||
type Schema m = NonEmpty (Resolver m)
|
|
||||||
|
|
||||||
-- | Resolves a 'Field' into an @Aeson.@'Data.Aeson.Types.Object' with error
|
-- | Resolves a 'Field' into an @Aeson.@'Data.Aeson.Types.Object' with error
|
||||||
-- information (if an error has occurred). @m@ is usually expected to be an
|
-- information (if an error has occurred). @m@ is usually expected to be an
|
||||||
@ -69,7 +60,7 @@ objectA name f = Resolver name $ resolveFieldValue f resolveRight
|
|||||||
|
|
||||||
-- | Like 'object' but also taking 'Argument's and can be null or a list of objects.
|
-- | Like 'object' but also taking 'Argument's and can be null or a list of objects.
|
||||||
wrappedObjectA :: MonadIO m
|
wrappedObjectA :: MonadIO m
|
||||||
=> Name -> ([Argument] -> ActionT m (Wrapping [Resolver m])) -> Resolver m
|
=> Name -> ([Argument] -> ActionT m (Type.Wrapping [Resolver m])) -> Resolver m
|
||||||
wrappedObjectA name f = Resolver name $ resolveFieldValue f resolveRight
|
wrappedObjectA name f = Resolver name $ resolveFieldValue f resolveRight
|
||||||
where
|
where
|
||||||
resolveRight fld@(Field _ _ _ sels) resolver
|
resolveRight fld@(Field _ _ _ sels) resolver
|
||||||
@ -77,7 +68,7 @@ wrappedObjectA name f = Resolver name $ resolveFieldValue f resolveRight
|
|||||||
|
|
||||||
-- | Like 'object' but can be null or a list of objects.
|
-- | Like 'object' but can be null or a list of objects.
|
||||||
wrappedObject :: MonadIO m
|
wrappedObject :: MonadIO m
|
||||||
=> Name -> ActionT m (Wrapping [Resolver m]) -> Resolver m
|
=> Name -> ActionT m (Type.Wrapping [Resolver m]) -> Resolver m
|
||||||
wrappedObject name = wrappedObjectA name . const
|
wrappedObject name = wrappedObjectA name . const
|
||||||
|
|
||||||
-- | A scalar represents a primitive value, like a string or an integer.
|
-- | A scalar represents a primitive value, like a string or an integer.
|
||||||
@ -91,54 +82,31 @@ scalarA name f = Resolver name $ resolveFieldValue f resolveRight
|
|||||||
where
|
where
|
||||||
resolveRight fld result = withField (return result) fld
|
resolveRight fld result = withField (return result) fld
|
||||||
|
|
||||||
-- | Lika 'scalar' but also taking 'Argument's and can be null or a list of scalars.
|
-- | Like 'scalar' but also taking 'Argument's and can be null or a list of scalars.
|
||||||
wrappedScalarA :: (MonadIO m, Aeson.ToJSON a)
|
wrappedScalarA :: (MonadIO m, Aeson.ToJSON a)
|
||||||
=> Name -> ([Argument] -> ActionT m (Wrapping a)) -> Resolver m
|
=> Name -> ([Argument] -> ActionT m (Type.Wrapping a)) -> Resolver m
|
||||||
wrappedScalarA name f = Resolver name $ resolveFieldValue f resolveRight
|
wrappedScalarA name f = Resolver name $ resolveFieldValue f resolveRight
|
||||||
where
|
where
|
||||||
resolveRight fld (Named result) = withField (return result) fld
|
resolveRight fld (Type.Named result) = withField (return result) fld
|
||||||
resolveRight fld Null
|
resolveRight fld Type.Null
|
||||||
= return $ HashMap.singleton (aliasOrName fld) Aeson.Null
|
= return $ HashMap.singleton (aliasOrName fld) Aeson.Null
|
||||||
resolveRight fld (List result) = withField (return result) fld
|
resolveRight fld (Type.List result) = withField (return result) fld
|
||||||
|
|
||||||
-- | Like 'scalar' but can be null or a list of scalars.
|
-- | Like 'scalar' but can be null or a list of scalars.
|
||||||
wrappedScalar :: (MonadIO m, Aeson.ToJSON a)
|
wrappedScalar :: (MonadIO m, Aeson.ToJSON a)
|
||||||
=> Name -> ActionT m (Wrapping a) -> Resolver m
|
=> Name -> ActionT m (Type.Wrapping a) -> Resolver m
|
||||||
wrappedScalar name = wrappedScalarA name . const
|
wrappedScalar name = wrappedScalarA name . const
|
||||||
|
|
||||||
{-# DEPRECATED enum "Use scalar instead" #-}
|
|
||||||
enum :: MonadIO m => Name -> ActionT m [Text] -> Resolver m
|
|
||||||
enum name = enumA name . const
|
|
||||||
|
|
||||||
{-# DEPRECATED enumA "Use scalarA instead" #-}
|
|
||||||
enumA :: MonadIO m => Name -> ([Argument] -> ActionT m [Text]) -> Resolver m
|
|
||||||
enumA name f = Resolver name $ resolveFieldValue f resolveRight
|
|
||||||
where
|
|
||||||
resolveRight fld resolver = withField (return resolver) fld
|
|
||||||
|
|
||||||
{-# DEPRECATED wrappedEnumA "Use wrappedScalarA instead" #-}
|
|
||||||
wrappedEnumA :: MonadIO m
|
|
||||||
=> Name -> ([Argument] -> ActionT m (Wrapping [Text])) -> Resolver m
|
|
||||||
wrappedEnumA name f = Resolver name $ resolveFieldValue f resolveRight
|
|
||||||
where
|
|
||||||
resolveRight fld (Named resolver) = withField (return resolver) fld
|
|
||||||
resolveRight fld Null
|
|
||||||
= return $ HashMap.singleton (aliasOrName fld) Aeson.Null
|
|
||||||
resolveRight fld (List resolver) = withField (return resolver) fld
|
|
||||||
|
|
||||||
{-# DEPRECATED wrappedEnum "Use wrappedScalar instead" #-}
|
|
||||||
wrappedEnum :: MonadIO m => Name -> ActionT m (Wrapping [Text]) -> Resolver m
|
|
||||||
wrappedEnum name = wrappedEnumA name . const
|
|
||||||
|
|
||||||
resolveFieldValue :: MonadIO m
|
resolveFieldValue :: MonadIO m
|
||||||
=> ([Argument] -> ActionT m a)
|
=> ([Argument] -> ActionT m a)
|
||||||
-> (Field -> a -> CollectErrsT m (HashMap Text Aeson.Value))
|
-> (Field -> a -> CollectErrsT m (HashMap Text Aeson.Value))
|
||||||
-> Field
|
-> Field
|
||||||
-> CollectErrsT m (HashMap Text Aeson.Value)
|
-> CollectErrsT m (HashMap Text Aeson.Value)
|
||||||
resolveFieldValue f resolveRight fld@(Field _ _ args _) = do
|
resolveFieldValue f resolveRight fld@(Field _ _ args _) = do
|
||||||
result <- lift $ runExceptT . runActionT $ f args
|
result <- lift $ reader . runExceptT . runActionT $ f args
|
||||||
either resolveLeft (resolveRight fld) result
|
either resolveLeft (resolveRight fld) result
|
||||||
where
|
where
|
||||||
|
reader = flip runReaderT $ Context mempty
|
||||||
resolveLeft err = do
|
resolveLeft err = do
|
||||||
_ <- addErrMsg err
|
_ <- addErrMsg err
|
||||||
return $ HashMap.singleton (aliasOrName fld) Aeson.Null
|
return $ HashMap.singleton (aliasOrName fld) Aeson.Null
|
||||||
@ -153,7 +121,7 @@ withField v fld
|
|||||||
-- 'Resolver' to each 'Field'. Resolves into a value containing the
|
-- 'Resolver' to each 'Field'. Resolves into a value containing the
|
||||||
-- resolved 'Field', or a null value and error information.
|
-- resolved 'Field', or a null value and error information.
|
||||||
resolve :: MonadIO m
|
resolve :: MonadIO m
|
||||||
=> [Resolver m] -> [Selection] -> CollectErrsT m Aeson.Value
|
=> [Resolver m] -> Seq Selection -> CollectErrsT m Aeson.Value
|
||||||
resolve resolvers = fmap (Aeson.toJSON . fold) . traverse tryResolvers
|
resolve resolvers = fmap (Aeson.toJSON . fold) . traverse tryResolvers
|
||||||
where
|
where
|
||||||
resolveTypeName (Resolver "__typename" f) = do
|
resolveTypeName (Resolver "__typename" f) = do
|
||||||
@ -163,8 +131,8 @@ resolve resolvers = fmap (Aeson.toJSON . fold) . traverse tryResolvers
|
|||||||
tryResolvers (SelectionField fld@(Field _ name _ _))
|
tryResolvers (SelectionField fld@(Field _ name _ _))
|
||||||
= maybe (errmsg fld) (tryResolver fld) $ find (compareResolvers name) resolvers
|
= maybe (errmsg fld) (tryResolver fld) $ find (compareResolvers name) resolvers
|
||||||
tryResolvers (SelectionFragment (Fragment typeCondition selections')) = do
|
tryResolvers (SelectionFragment (Fragment typeCondition selections')) = do
|
||||||
that <- maybe (return "") resolveTypeName (find (compareResolvers "__typename") resolvers)
|
that <- traverse resolveTypeName (find (compareResolvers "__typename") resolvers)
|
||||||
if Aeson.String typeCondition == that
|
if maybe True (Aeson.String typeCondition ==) that
|
||||||
then fmap fold . traverse tryResolvers $ selections'
|
then fmap fold . traverse tryResolvers $ selections'
|
||||||
else return mempty
|
else return mempty
|
||||||
compareResolvers name (Resolver name' _) = name == name'
|
compareResolvers name (Resolver name' _) = name == name'
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
-- | Monad transformer stack used by the @GraphQL@ resolvers.
|
-- | Monad transformer stack used by the @GraphQL@ resolvers.
|
||||||
module Language.GraphQL.Trans
|
module Language.GraphQL.Trans
|
||||||
( ActionT(..)
|
( ActionT(..)
|
||||||
|
, Context(Context)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Applicative (Alternative(..))
|
import Control.Applicative (Alternative(..))
|
||||||
@ -8,10 +9,19 @@ import Control.Monad (MonadPlus(..))
|
|||||||
import Control.Monad.IO.Class (MonadIO(..))
|
import Control.Monad.IO.Class (MonadIO(..))
|
||||||
import Control.Monad.Trans.Class (MonadTrans(..))
|
import Control.Monad.Trans.Class (MonadTrans(..))
|
||||||
import Control.Monad.Trans.Except (ExceptT)
|
import Control.Monad.Trans.Except (ExceptT)
|
||||||
|
import Control.Monad.Trans.Reader (ReaderT)
|
||||||
|
import Data.HashMap.Strict (HashMap)
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
|
import Language.GraphQL.AST.Core (Name, Value)
|
||||||
|
|
||||||
-- | Monad transformer stack used by the resolvers to provide error handling.
|
-- | Resolution context holds resolver arguments.
|
||||||
newtype ActionT m a = ActionT { runActionT :: ExceptT Text m a }
|
newtype Context = Context (HashMap Name Value)
|
||||||
|
|
||||||
|
-- | Monad transformer stack used by the resolvers to provide error handling
|
||||||
|
-- and resolution context (resolver arguments).
|
||||||
|
newtype ActionT m a = ActionT
|
||||||
|
{ runActionT :: ExceptT Text (ReaderT Context m) a
|
||||||
|
}
|
||||||
|
|
||||||
instance Functor m => Functor (ActionT m) where
|
instance Functor m => Functor (ActionT m) where
|
||||||
fmap f = ActionT . fmap f . runActionT
|
fmap f = ActionT . fmap f . runActionT
|
||||||
@ -25,7 +35,7 @@ instance Monad m => Monad (ActionT m) where
|
|||||||
(ActionT action) >>= f = ActionT $ action >>= runActionT . f
|
(ActionT action) >>= f = ActionT $ action >>= runActionT . f
|
||||||
|
|
||||||
instance MonadTrans ActionT where
|
instance MonadTrans ActionT where
|
||||||
lift = ActionT . lift
|
lift = ActionT . lift . lift
|
||||||
|
|
||||||
instance MonadIO m => MonadIO (ActionT m) where
|
instance MonadIO m => MonadIO (ActionT m) where
|
||||||
liftIO = lift . liftIO
|
liftIO = lift . liftIO
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
-- | Definitions for @GraphQL@ type system.
|
-- | Definitions for @GraphQL@ input types.
|
||||||
module Language.GraphQL.Type
|
module Language.GraphQL.Type
|
||||||
( Wrapping(..)
|
( Wrapping(..)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Aeson as Aeson ( ToJSON
|
import Data.Aeson as Aeson (ToJSON, toJSON)
|
||||||
, toJSON
|
|
||||||
)
|
|
||||||
import qualified Data.Aeson as Aeson
|
import qualified Data.Aeson as Aeson
|
||||||
|
|
||||||
-- | GraphQL distinguishes between "wrapping" and "named" types. Each wrapping
|
-- | GraphQL distinguishes between "wrapping" and "named" types. Each wrapping
|
||||||
|
50
src/Language/GraphQL/Type/Directive.hs
Normal file
50
src/Language/GraphQL/Type/Directive.hs
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
|
module Language.GraphQL.Type.Directive
|
||||||
|
( selection
|
||||||
|
) where
|
||||||
|
|
||||||
|
import qualified Data.HashMap.Strict as HashMap
|
||||||
|
import Language.GraphQL.AST.Core
|
||||||
|
|
||||||
|
-- | Directive processing status.
|
||||||
|
data Status
|
||||||
|
= Skip -- ^ Skip the selection and stop directive processing
|
||||||
|
| Include Directive -- ^ The directive was processed, try other handlers
|
||||||
|
| Continue Directive -- ^ Directive handler mismatch, try other handlers
|
||||||
|
|
||||||
|
-- | Takes a list of directives, handles supported directives and excludes them
|
||||||
|
-- from the result. If the selection should be skipped, returns 'Nothing'.
|
||||||
|
selection :: [Directive] -> Maybe [Directive]
|
||||||
|
selection = foldr go (Just [])
|
||||||
|
where
|
||||||
|
go directive' directives' =
|
||||||
|
case (skip . include) (Continue directive') of
|
||||||
|
(Include _) -> directives'
|
||||||
|
Skip -> Nothing
|
||||||
|
(Continue x) -> (x :) <$> directives'
|
||||||
|
|
||||||
|
handle :: (Directive -> Status) -> Status -> Status
|
||||||
|
handle _ Skip = Skip
|
||||||
|
handle handler (Continue directive) = handler directive
|
||||||
|
handle handler (Include directive) = handler directive
|
||||||
|
|
||||||
|
-- * Directive implementations
|
||||||
|
|
||||||
|
skip :: Status -> Status
|
||||||
|
skip = handle skip'
|
||||||
|
where
|
||||||
|
skip' directive'@(Directive "skip" (Arguments arguments)) =
|
||||||
|
case HashMap.lookup "if" arguments of
|
||||||
|
(Just (Boolean True)) -> Skip
|
||||||
|
_ -> Include directive'
|
||||||
|
skip' directive' = Continue directive'
|
||||||
|
|
||||||
|
include :: Status -> Status
|
||||||
|
include = handle include'
|
||||||
|
where
|
||||||
|
include' directive'@(Directive "include" (Arguments arguments)) =
|
||||||
|
case HashMap.lookup "if" arguments of
|
||||||
|
(Just (Boolean True)) -> Include directive'
|
||||||
|
_ -> Skip
|
||||||
|
include' directive' = Continue directive'
|
@ -1,4 +1,4 @@
|
|||||||
resolver: lts-14.11
|
resolver: lts-14.18
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
- .
|
- .
|
||||||
|
46
tests/Language/GraphQL/AST/EncoderSpec.hs
Normal file
46
tests/Language/GraphQL/AST/EncoderSpec.hs
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE QuasiQuotes #-}
|
||||||
|
module Language.GraphQL.AST.EncoderSpec
|
||||||
|
( spec
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Language.GraphQL.AST
|
||||||
|
import Language.GraphQL.AST.Encoder
|
||||||
|
import Test.Hspec (Spec, context, describe, it, shouldBe)
|
||||||
|
import Text.RawString.QQ (r)
|
||||||
|
|
||||||
|
spec :: Spec
|
||||||
|
spec = do
|
||||||
|
describe "value" $ do
|
||||||
|
context "minified" $ do
|
||||||
|
it "escapes \\" $
|
||||||
|
value minified (String "\\") `shouldBe` "\"\\\\\""
|
||||||
|
it "escapes quotes" $
|
||||||
|
value minified (String "\"") `shouldBe` "\"\\\"\""
|
||||||
|
it "escapes backspace" $
|
||||||
|
value minified (String "a\bc") `shouldBe` "\"a\\bc\""
|
||||||
|
it "escapes Unicode" $
|
||||||
|
value minified (String "\0") `shouldBe` "\"\\u0000\""
|
||||||
|
|
||||||
|
context "pretty" $ do
|
||||||
|
it "uses strings for short string values" $
|
||||||
|
value pretty (String "Short text") `shouldBe` "\"Short text\""
|
||||||
|
it "uses block strings for text with new lines" $
|
||||||
|
value pretty (String "Line 1\nLine 2")
|
||||||
|
`shouldBe` "\"\"\"\n Line 1\n Line 2\n\"\"\""
|
||||||
|
it "escapes \\ in short strings" $
|
||||||
|
value pretty (String "\\") `shouldBe` "\"\\\\\""
|
||||||
|
|
||||||
|
describe "definition" $
|
||||||
|
it "indents block strings in arguments" $
|
||||||
|
let arguments = [Argument "message" (String "line1\nline2")]
|
||||||
|
field = Field Nothing "field" arguments [] []
|
||||||
|
set = OperationSelectionSet $ pure $ SelectionField field
|
||||||
|
operation = DefinitionOperation set
|
||||||
|
in definition pretty operation `shouldBe` [r|{
|
||||||
|
field(message: """
|
||||||
|
line1
|
||||||
|
line2
|
||||||
|
""")
|
||||||
|
}
|
||||||
|
|]
|
@ -1,12 +1,12 @@
|
|||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
{-# LANGUAGE QuasiQuotes #-}
|
{-# LANGUAGE QuasiQuotes #-}
|
||||||
module Language.GraphQL.LexerSpec
|
module Language.GraphQL.AST.LexerSpec
|
||||||
( spec
|
( spec
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import Data.Void (Void)
|
import Data.Void (Void)
|
||||||
import Language.GraphQL.Lexer
|
import Language.GraphQL.AST.Lexer
|
||||||
import Test.Hspec (Spec, context, describe, it)
|
import Test.Hspec (Spec, context, describe, it)
|
||||||
import Test.Hspec.Megaparsec (shouldParse, shouldSucceedOn)
|
import Test.Hspec.Megaparsec (shouldParse, shouldSucceedOn)
|
||||||
import Text.Megaparsec (ParseErrorBundle, parse)
|
import Text.Megaparsec (ParseErrorBundle, parse)
|
||||||
@ -71,8 +71,8 @@ spec = describe "Lexer" $ do
|
|||||||
parse float "" "-1.123e4567" `shouldParse` (-1.123e4567)
|
parse float "" "-1.123e4567" `shouldParse` (-1.123e4567)
|
||||||
|
|
||||||
it "lexes punctuation" $ do
|
it "lexes punctuation" $ do
|
||||||
parse bang "" "!" `shouldParse` '!'
|
parse bang "" "!" `shouldParse` "!"
|
||||||
parse dollar "" "$" `shouldParse` '$'
|
parse dollar "" "$" `shouldParse` "$"
|
||||||
runBetween parens `shouldSucceedOn` "()"
|
runBetween parens `shouldSucceedOn` "()"
|
||||||
parse spread "" "..." `shouldParse` "..."
|
parse spread "" "..." `shouldParse` "..."
|
||||||
parse colon "" ":" `shouldParse` ":"
|
parse colon "" ":" `shouldParse` ":"
|
44
tests/Language/GraphQL/AST/ParserSpec.hs
Normal file
44
tests/Language/GraphQL/AST/ParserSpec.hs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE QuasiQuotes #-}
|
||||||
|
module Language.GraphQL.AST.ParserSpec
|
||||||
|
( spec
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Language.GraphQL.AST.Parser
|
||||||
|
import Test.Hspec (Spec, describe, it)
|
||||||
|
import Test.Hspec.Megaparsec (shouldSucceedOn)
|
||||||
|
import Text.Megaparsec (parse)
|
||||||
|
import Text.RawString.QQ (r)
|
||||||
|
|
||||||
|
spec :: Spec
|
||||||
|
spec = describe "Parser" $ do
|
||||||
|
it "accepts BOM header" $
|
||||||
|
parse document "" `shouldSucceedOn` "\xfeff{foo}"
|
||||||
|
|
||||||
|
it "accepts block strings as argument" $
|
||||||
|
parse document "" `shouldSucceedOn` [r|{
|
||||||
|
hello(text: """Argument""")
|
||||||
|
}|]
|
||||||
|
|
||||||
|
it "accepts strings as argument" $
|
||||||
|
parse document "" `shouldSucceedOn` [r|{
|
||||||
|
hello(text: "Argument")
|
||||||
|
}|]
|
||||||
|
|
||||||
|
it "accepts two required arguments" $
|
||||||
|
parse document "" `shouldSucceedOn` [r|
|
||||||
|
mutation auth($username: String!, $password: String!){
|
||||||
|
test
|
||||||
|
}|]
|
||||||
|
|
||||||
|
it "accepts two string arguments" $
|
||||||
|
parse document "" `shouldSucceedOn` [r|
|
||||||
|
mutation auth{
|
||||||
|
test(username: "username", password: "password")
|
||||||
|
}|]
|
||||||
|
|
||||||
|
it "accepts two block string arguments" $
|
||||||
|
parse document "" `shouldSucceedOn` [r|
|
||||||
|
mutation auth{
|
||||||
|
test(username: """username""", password: """password""")
|
||||||
|
}|]
|
@ -1,21 +0,0 @@
|
|||||||
{-# LANGUAGE OverloadedStrings #-}
|
|
||||||
module Language.GraphQL.EncoderSpec
|
|
||||||
( spec
|
|
||||||
) where
|
|
||||||
|
|
||||||
import Language.GraphQL.AST ( Value(..))
|
|
||||||
import Language.GraphQL.Encoder ( value
|
|
||||||
, minified
|
|
||||||
)
|
|
||||||
import Test.Hspec ( Spec
|
|
||||||
, describe
|
|
||||||
, it
|
|
||||||
, shouldBe
|
|
||||||
)
|
|
||||||
|
|
||||||
spec :: Spec
|
|
||||||
spec = describe "value" $ do
|
|
||||||
it "escapes \\" $
|
|
||||||
value minified (ValueString "\\") `shouldBe` "\"\\\\\""
|
|
||||||
it "escapes quotes" $
|
|
||||||
value minified (ValueString "\"") `shouldBe` "\"\\\"\""
|
|
@ -1,26 +0,0 @@
|
|||||||
{-# LANGUAGE OverloadedStrings #-}
|
|
||||||
{-# LANGUAGE QuasiQuotes #-}
|
|
||||||
module Language.GraphQL.ParserSpec
|
|
||||||
( spec
|
|
||||||
) where
|
|
||||||
|
|
||||||
import Language.GraphQL.Parser (document)
|
|
||||||
import Test.Hspec (Spec, describe, it)
|
|
||||||
import Test.Hspec.Megaparsec (shouldSucceedOn)
|
|
||||||
import Text.Megaparsec (parse)
|
|
||||||
import Text.RawString.QQ (r)
|
|
||||||
|
|
||||||
spec :: Spec
|
|
||||||
spec = describe "Parser" $ do
|
|
||||||
it "accepts BOM header" $
|
|
||||||
parse document "" `shouldSucceedOn` "\xfeff{foo}"
|
|
||||||
|
|
||||||
it "accepts block strings as argument" $
|
|
||||||
parse document "" `shouldSucceedOn` [r|{
|
|
||||||
hello(text: """Argument""")
|
|
||||||
}|]
|
|
||||||
|
|
||||||
it "accepts strings as argument" $
|
|
||||||
parse document "" `shouldSucceedOn` [r|{
|
|
||||||
hello(text: "Argument")
|
|
||||||
}|]
|
|
84
tests/Test/DirectiveSpec.hs
Normal file
84
tests/Test/DirectiveSpec.hs
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE QuasiQuotes #-}
|
||||||
|
module Test.DirectiveSpec
|
||||||
|
( spec
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Data.Aeson (Value, object, (.=))
|
||||||
|
import Data.List.NonEmpty (NonEmpty(..))
|
||||||
|
import Language.GraphQL
|
||||||
|
import qualified Language.GraphQL.Schema as Schema
|
||||||
|
import Test.Hspec (Spec, describe, it, shouldBe)
|
||||||
|
import Text.RawString.QQ (r)
|
||||||
|
|
||||||
|
experimentalResolver :: Schema.Resolver IO
|
||||||
|
experimentalResolver = Schema.scalar "experimentalField" $ pure (5 :: Int)
|
||||||
|
|
||||||
|
emptyObject :: Value
|
||||||
|
emptyObject = object
|
||||||
|
[ "data" .= object []
|
||||||
|
]
|
||||||
|
|
||||||
|
spec :: Spec
|
||||||
|
spec =
|
||||||
|
describe "Directive executor" $ do
|
||||||
|
it "should be able to @skip fields" $ do
|
||||||
|
let query = [r|
|
||||||
|
{
|
||||||
|
experimentalField @skip(if: true)
|
||||||
|
}
|
||||||
|
|]
|
||||||
|
|
||||||
|
actual <- graphql (experimentalResolver :| []) query
|
||||||
|
actual `shouldBe` emptyObject
|
||||||
|
|
||||||
|
it "should not skip fields if @skip is false" $ do
|
||||||
|
let query = [r|
|
||||||
|
{
|
||||||
|
experimentalField @skip(if: false)
|
||||||
|
}
|
||||||
|
|]
|
||||||
|
expected = object
|
||||||
|
[ "data" .= object
|
||||||
|
[ "experimentalField" .= (5 :: Int)
|
||||||
|
]
|
||||||
|
]
|
||||||
|
|
||||||
|
actual <- graphql (experimentalResolver :| []) query
|
||||||
|
actual `shouldBe` expected
|
||||||
|
|
||||||
|
it "should skip fields if @include is false" $ do
|
||||||
|
let query = [r|
|
||||||
|
{
|
||||||
|
experimentalField @include(if: false)
|
||||||
|
}
|
||||||
|
|]
|
||||||
|
|
||||||
|
actual <- graphql (experimentalResolver :| []) query
|
||||||
|
actual `shouldBe` emptyObject
|
||||||
|
|
||||||
|
it "should be able to @skip a fragment spread" $ do
|
||||||
|
let query = [r|
|
||||||
|
{
|
||||||
|
...experimentalFragment @skip(if: true)
|
||||||
|
}
|
||||||
|
|
||||||
|
fragment experimentalFragment on ExperimentalType {
|
||||||
|
experimentalField
|
||||||
|
}
|
||||||
|
|]
|
||||||
|
|
||||||
|
actual <- graphql (experimentalResolver :| []) query
|
||||||
|
actual `shouldBe` emptyObject
|
||||||
|
|
||||||
|
it "should be able to @skip an inline fragment" $ do
|
||||||
|
let query = [r|
|
||||||
|
{
|
||||||
|
... on ExperimentalType @skip(if: true) {
|
||||||
|
experimentalField
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|]
|
||||||
|
|
||||||
|
actual <- graphql (experimentalResolver :| []) query
|
||||||
|
actual `shouldBe` emptyObject
|
@ -10,7 +10,13 @@ import Data.List.NonEmpty (NonEmpty(..))
|
|||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import Language.GraphQL
|
import Language.GraphQL
|
||||||
import qualified Language.GraphQL.Schema as Schema
|
import qualified Language.GraphQL.Schema as Schema
|
||||||
import Test.Hspec (Spec, describe, it, shouldBe, shouldNotSatisfy)
|
import Test.Hspec ( Spec
|
||||||
|
, describe
|
||||||
|
, it
|
||||||
|
, shouldBe
|
||||||
|
, shouldSatisfy
|
||||||
|
, shouldNotSatisfy
|
||||||
|
)
|
||||||
import Text.RawString.QQ (r)
|
import Text.RawString.QQ (r)
|
||||||
|
|
||||||
size :: Schema.Resolver IO
|
size :: Schema.Resolver IO
|
||||||
@ -37,8 +43,13 @@ inlineQuery = [r|{
|
|||||||
}
|
}
|
||||||
}|]
|
}|]
|
||||||
|
|
||||||
|
hasErrors :: Value -> Bool
|
||||||
|
hasErrors (Object object') = HashMap.member "errors" object'
|
||||||
|
hasErrors _ = True
|
||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec = describe "Inline fragment executor" $ do
|
spec = do
|
||||||
|
describe "Inline fragment executor" $ do
|
||||||
it "chooses the first selection if the type matches" $ do
|
it "chooses the first selection if the type matches" $ do
|
||||||
actual <- graphql (garment "Hat" :| []) inlineQuery
|
actual <- graphql (garment "Hat" :| []) inlineQuery
|
||||||
let expected = object
|
let expected = object
|
||||||
@ -92,6 +103,89 @@ spec = describe "Inline fragment executor" $ do
|
|||||||
|
|
||||||
actual <- graphql (size :| []) query
|
actual <- graphql (size :| []) query
|
||||||
actual `shouldNotSatisfy` hasErrors
|
actual `shouldNotSatisfy` hasErrors
|
||||||
where
|
|
||||||
hasErrors (Object object') = HashMap.member "errors" object'
|
describe "Fragment spread executor" $ do
|
||||||
hasErrors _ = True
|
it "evaluates fragment spreads" $ do
|
||||||
|
let query = [r|
|
||||||
|
{
|
||||||
|
...circumferenceFragment
|
||||||
|
}
|
||||||
|
|
||||||
|
fragment circumferenceFragment on Hat {
|
||||||
|
circumference
|
||||||
|
}
|
||||||
|
|]
|
||||||
|
|
||||||
|
actual <- graphql (circumference :| []) query
|
||||||
|
let expected = object
|
||||||
|
[ "data" .= object
|
||||||
|
[ "circumference" .= (60 :: Int)
|
||||||
|
]
|
||||||
|
]
|
||||||
|
in actual `shouldBe` expected
|
||||||
|
|
||||||
|
it "evaluates nested fragments" $ do
|
||||||
|
let query = [r|
|
||||||
|
{
|
||||||
|
garment {
|
||||||
|
...circumferenceFragment
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fragment circumferenceFragment on Hat {
|
||||||
|
...hatFragment
|
||||||
|
}
|
||||||
|
|
||||||
|
fragment hatFragment on Hat {
|
||||||
|
circumference
|
||||||
|
}
|
||||||
|
|]
|
||||||
|
|
||||||
|
actual <- graphql (garment "Hat" :| []) query
|
||||||
|
let expected = object
|
||||||
|
[ "data" .= object
|
||||||
|
[ "garment" .= object
|
||||||
|
[ "circumference" .= (60 :: Int)
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
in actual `shouldBe` expected
|
||||||
|
|
||||||
|
it "rejects recursive fragments" $ do
|
||||||
|
let query = [r|
|
||||||
|
{
|
||||||
|
...circumferenceFragment
|
||||||
|
}
|
||||||
|
|
||||||
|
fragment circumferenceFragment on Hat {
|
||||||
|
...circumferenceFragment
|
||||||
|
}
|
||||||
|
|]
|
||||||
|
|
||||||
|
actual <- graphql (circumference :| []) query
|
||||||
|
actual `shouldSatisfy` hasErrors
|
||||||
|
|
||||||
|
it "considers type condition" $ do
|
||||||
|
let query = [r|
|
||||||
|
{
|
||||||
|
garment {
|
||||||
|
...circumferenceFragment
|
||||||
|
...sizeFragment
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fragment circumferenceFragment on Hat {
|
||||||
|
circumference
|
||||||
|
}
|
||||||
|
fragment sizeFragment on Shirt {
|
||||||
|
size
|
||||||
|
}
|
||||||
|
|]
|
||||||
|
expected = object
|
||||||
|
[ "data" .= object
|
||||||
|
[ "garment" .= object
|
||||||
|
[ "circumference" .= (60 :: Int)
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
actual <- graphql (garment "Hat" :| []) query
|
||||||
|
actual `shouldBe` expected
|
||||||
|
@ -7,8 +7,8 @@ module Test.KitchenSinkSpec
|
|||||||
import qualified Data.Text.IO as Text.IO
|
import qualified Data.Text.IO as Text.IO
|
||||||
import qualified Data.Text.Lazy.IO as Text.Lazy.IO
|
import qualified Data.Text.Lazy.IO as Text.Lazy.IO
|
||||||
import qualified Data.Text.Lazy as Lazy (Text)
|
import qualified Data.Text.Lazy as Lazy (Text)
|
||||||
import qualified Language.GraphQL.Encoder as Encoder
|
import qualified Language.GraphQL.AST.Encoder as Encoder
|
||||||
import qualified Language.GraphQL.Parser as Parser
|
import qualified Language.GraphQL.AST.Parser as Parser
|
||||||
import Paths_graphql (getDataFileName)
|
import Paths_graphql (getDataFileName)
|
||||||
import Test.Hspec (Spec, describe, it)
|
import Test.Hspec (Spec, describe, it)
|
||||||
import Test.Hspec.Megaparsec (parseSatisfies)
|
import Test.Hspec.Megaparsec (parseSatisfies)
|
||||||
|
@ -26,7 +26,7 @@ import Control.Monad.Trans.Except (throwE)
|
|||||||
import Data.Maybe (catMaybes)
|
import Data.Maybe (catMaybes)
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import Language.GraphQL.Trans
|
import Language.GraphQL.Trans
|
||||||
import Language.GraphQL.Type
|
import qualified Language.GraphQL.Type as Type
|
||||||
|
|
||||||
-- * Data
|
-- * Data
|
||||||
-- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsData.js
|
-- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsData.js
|
||||||
@ -191,8 +191,8 @@ getDroid' _ = empty
|
|||||||
getFriends :: Character -> [Character]
|
getFriends :: Character -> [Character]
|
||||||
getFriends char = catMaybes $ liftA2 (<|>) getDroid getHuman <$> friends char
|
getFriends char = catMaybes $ liftA2 (<|>) getDroid getHuman <$> friends char
|
||||||
|
|
||||||
getEpisode :: Int -> Maybe (Wrapping Text)
|
getEpisode :: Int -> Maybe (Type.Wrapping Text)
|
||||||
getEpisode 4 = pure $ Named "NEWHOPE"
|
getEpisode 4 = pure $ Type.Named "NEWHOPE"
|
||||||
getEpisode 5 = pure $ Named "EMPIRE"
|
getEpisode 5 = pure $ Type.Named "EMPIRE"
|
||||||
getEpisode 6 = pure $ Named "JEDI"
|
getEpisode 6 = pure $ Type.Named "JEDI"
|
||||||
getEpisode _ = empty
|
getEpisode _ = empty
|
||||||
|
@ -15,7 +15,7 @@ import Data.List.NonEmpty (NonEmpty(..))
|
|||||||
import Data.Maybe (catMaybes)
|
import Data.Maybe (catMaybes)
|
||||||
import qualified Language.GraphQL.Schema as Schema
|
import qualified Language.GraphQL.Schema as Schema
|
||||||
import Language.GraphQL.Trans
|
import Language.GraphQL.Trans
|
||||||
import Language.GraphQL.Type
|
import qualified Language.GraphQL.Type as Type
|
||||||
import Test.StarWars.Data
|
import Test.StarWars.Data
|
||||||
|
|
||||||
-- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsSchema.js
|
-- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsSchema.js
|
||||||
@ -26,23 +26,23 @@ schema = hero :| [human, droid]
|
|||||||
hero :: MonadIO m => Schema.Resolver m
|
hero :: MonadIO m => Schema.Resolver m
|
||||||
hero = Schema.objectA "hero" $ \case
|
hero = Schema.objectA "hero" $ \case
|
||||||
[] -> character artoo
|
[] -> character artoo
|
||||||
[Schema.Argument "episode" (Schema.ValueEnum "NEWHOPE")] -> character $ getHero 4
|
[Schema.Argument "episode" (Schema.Enum "NEWHOPE")] -> character $ getHero 4
|
||||||
[Schema.Argument "episode" (Schema.ValueEnum "EMPIRE" )] -> character $ getHero 5
|
[Schema.Argument "episode" (Schema.Enum "EMPIRE" )] -> character $ getHero 5
|
||||||
[Schema.Argument "episode" (Schema.ValueEnum "JEDI" )] -> character $ getHero 6
|
[Schema.Argument "episode" (Schema.Enum "JEDI" )] -> character $ getHero 6
|
||||||
_ -> ActionT $ throwE "Invalid arguments."
|
_ -> ActionT $ throwE "Invalid arguments."
|
||||||
|
|
||||||
human :: MonadIO m => Schema.Resolver m
|
human :: MonadIO m => Schema.Resolver m
|
||||||
human = Schema.wrappedObjectA "human" $ \case
|
human = Schema.wrappedObjectA "human" $ \case
|
||||||
[Schema.Argument "id" (Schema.ValueString i)] -> do
|
[Schema.Argument "id" (Schema.String i)] -> do
|
||||||
humanCharacter <- lift $ return $ getHuman i >>= Just
|
humanCharacter <- lift $ return $ getHuman i >>= Just
|
||||||
case humanCharacter of
|
case humanCharacter of
|
||||||
Nothing -> return Null
|
Nothing -> return Type.Null
|
||||||
Just e -> Named <$> character e
|
Just e -> Type.Named <$> character e
|
||||||
_ -> ActionT $ throwE "Invalid arguments."
|
_ -> ActionT $ throwE "Invalid arguments."
|
||||||
|
|
||||||
droid :: MonadIO m => Schema.Resolver m
|
droid :: MonadIO m => Schema.Resolver m
|
||||||
droid = Schema.objectA "droid" $ \case
|
droid = Schema.objectA "droid" $ \case
|
||||||
[Schema.Argument "id" (Schema.ValueString i)] -> character =<< liftIO (getDroid i)
|
[Schema.Argument "id" (Schema.String i)] -> character =<< liftIO (getDroid i)
|
||||||
_ -> ActionT $ throwE "Invalid arguments."
|
_ -> ActionT $ throwE "Invalid arguments."
|
||||||
|
|
||||||
character :: MonadIO m => Character -> ActionT m [Schema.Resolver m]
|
character :: MonadIO m => Character -> ActionT m [Schema.Resolver m]
|
||||||
@ -50,8 +50,8 @@ character char = return
|
|||||||
[ Schema.scalar "id" $ return $ id_ char
|
[ Schema.scalar "id" $ return $ id_ char
|
||||||
, Schema.scalar "name" $ return $ name char
|
, Schema.scalar "name" $ return $ name char
|
||||||
, Schema.wrappedObject "friends"
|
, Schema.wrappedObject "friends"
|
||||||
$ traverse character $ List $ Named <$> getFriends char
|
$ traverse character $ Type.List $ Type.Named <$> getFriends char
|
||||||
, Schema.wrappedScalar "appearsIn" $ return . List
|
, Schema.wrappedScalar "appearsIn" $ return . Type.List
|
||||||
$ catMaybes (getEpisode <$> appearsIn char)
|
$ catMaybes (getEpisode <$> appearsIn char)
|
||||||
, Schema.scalar "secretBackstory" $ secretBackstory char
|
, Schema.scalar "secretBackstory" $ secretBackstory char
|
||||||
, Schema.scalar "homePlanet" $ return $ either mempty homePlanet char
|
, Schema.scalar "homePlanet" $ return $ either mempty homePlanet char
|
||||||
|
Reference in New Issue
Block a user