Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
a5cf0a32e8
|
|||
2f9881bb21
|
|||
bf2e4925b4
|
|||
2321d1a1bc
|
|||
2f19093803
|
|||
0dac9701bc
|
|||
0d25f482dd
|
|||
a2401d563b
|
|||
8503c0f288 | |||
05e6aa4c95 | |||
647547206f
|
|||
0c8edae90a | |||
73585dde85
|
|||
1f7bd92d11 | |||
16cbe3fc28
|
|||
f20cd02048
|
|||
116aa1f6bb
|
|||
df078a59d0
|
|||
930b8f10b7
|
|||
0047a13bc0
|
34
CHANGELOG.md
34
CHANGELOG.md
@ -6,6 +6,37 @@ The format is based on
|
|||||||
and this project adheres to
|
and this project adheres to
|
||||||
[Haskell Package Versioning Policy](https://pvp.haskell.org/).
|
[Haskell Package Versioning Policy](https://pvp.haskell.org/).
|
||||||
|
|
||||||
|
## [1.1.0.0] - 2022-12-24
|
||||||
|
### Changed
|
||||||
|
- Removed deprecated `Language.GraphQL.Error` functions: `addErr`, `addErrMsg`,
|
||||||
|
`singleError`.
|
||||||
|
- Deprecate `Resolution`, `CollectErrsT` and `runCollectErrs` in the `Error`
|
||||||
|
module. It was already noted in the documentation that these symbols are
|
||||||
|
deprecated, now a pragma is added.
|
||||||
|
- `Language.GraphQL`: Added information about the *json* flag and switching to
|
||||||
|
*graphql-spice* for JSON support.
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Partial schema printing: operation type encoder.
|
||||||
|
|
||||||
|
## [1.0.3.0] - 2022-03-27
|
||||||
|
### Fixed
|
||||||
|
- Index position in error path. (Index and Segment paths of a field have been
|
||||||
|
swapped).
|
||||||
|
- Parsing empty list as an argument.
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- quickCheck Parser test for arguments. Arbitrary instances for Language.GraphQL.AST.Document.
|
||||||
|
- Enhanced query error messages. Add tests for these cases.
|
||||||
|
- Allow version 2.0 of the text package.
|
||||||
|
|
||||||
|
## [1.0.2.0] - 2021-12-26
|
||||||
|
### Added
|
||||||
|
- `Serialize` instance for `Type.Definition.Value`.
|
||||||
|
- `VariableValue` instance for `Type.Definition.Value`.
|
||||||
|
- `Json` build flag, enabled by default. JSON and Aeson support can be disabled
|
||||||
|
by disabling this flag.
|
||||||
|
|
||||||
## [1.0.1.0] - 2021-09-27
|
## [1.0.1.0] - 2021-09-27
|
||||||
### Added
|
### Added
|
||||||
- Custom `Show` instance for `Type.Definition.Value` (for error
|
- Custom `Show` instance for `Type.Definition.Value` (for error
|
||||||
@ -459,6 +490,9 @@ and this project adheres to
|
|||||||
### Added
|
### Added
|
||||||
- Data types for the GraphQL language.
|
- Data types for the GraphQL language.
|
||||||
|
|
||||||
|
[1.1.0.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v1.1.0.0&rev_to=v1.0.3.0
|
||||||
|
[1.0.3.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v1.0.3.0&rev_to=v1.0.2.0
|
||||||
|
[1.0.2.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v1.0.2.0&rev_to=v1.0.1.0
|
||||||
[1.0.1.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v1.0.1.0&rev_to=v1.0.0.0
|
[1.0.1.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v1.0.1.0&rev_to=v1.0.0.0
|
||||||
[1.0.0.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v1.0.0.0&rev_to=v0.11.1.0
|
[1.0.0.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v1.0.0.0&rev_to=v0.11.1.0
|
||||||
[0.11.1.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v0.11.1.0&rev_to=v0.11.0.0
|
[0.11.1.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v0.11.1.0&rev_to=v0.11.0.0
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
cabal-version: 2.2
|
cabal-version: 2.4
|
||||||
|
|
||||||
name: graphql
|
name: graphql
|
||||||
version: 1.0.1.0
|
version: 1.1.0.0
|
||||||
synopsis: Haskell GraphQL implementation
|
synopsis: Haskell GraphQL implementation
|
||||||
description: Haskell <https://spec.graphql.org/June2018/ GraphQL> implementation.
|
description: Haskell <https://spec.graphql.org/June2018/ GraphQL> implementation.
|
||||||
category: Language
|
category: Language
|
||||||
@ -11,7 +11,7 @@ author: Danny Navarro <j@dannynavarro.net>,
|
|||||||
Matthías Páll Gissurarson <mpg@mpg.is>,
|
Matthías Páll Gissurarson <mpg@mpg.is>,
|
||||||
Sólrún Halla Einarsdóttir <she@mpg.is>
|
Sólrún Halla Einarsdóttir <she@mpg.is>
|
||||||
maintainer: belka@caraus.de
|
maintainer: belka@caraus.de
|
||||||
copyright: (c) 2019-2021 Eugen Wissner,
|
copyright: (c) 2019-2022 Eugen Wissner,
|
||||||
(c) 2015-2017 J. Daniel Navarro
|
(c) 2015-2017 J. Daniel Navarro
|
||||||
license: MPL-2.0 AND BSD-3-Clause
|
license: MPL-2.0 AND BSD-3-Clause
|
||||||
license-files: LICENSE,
|
license-files: LICENSE,
|
||||||
@ -21,13 +21,18 @@ extra-source-files:
|
|||||||
CHANGELOG.md
|
CHANGELOG.md
|
||||||
README.md
|
README.md
|
||||||
tested-with:
|
tested-with:
|
||||||
GHC == 8.10.7
|
GHC == 8.10.7,
|
||||||
, GHC == 9.0.1
|
GHC == 9.2.4
|
||||||
|
|
||||||
source-repository head
|
source-repository head
|
||||||
type: git
|
type: git
|
||||||
location: git://caraus.tech/pub/graphql.git
|
location: git://caraus.tech/pub/graphql.git
|
||||||
|
|
||||||
|
flag Json
|
||||||
|
description: Whether to build against @aeson 1.x@
|
||||||
|
default: False
|
||||||
|
manual: True
|
||||||
|
|
||||||
library
|
library
|
||||||
exposed-modules:
|
exposed-modules:
|
||||||
Language.GraphQL
|
Language.GraphQL
|
||||||
@ -57,21 +62,26 @@ library
|
|||||||
hs-source-dirs:
|
hs-source-dirs:
|
||||||
src
|
src
|
||||||
ghc-options: -Wall
|
ghc-options: -Wall
|
||||||
|
|
||||||
build-depends:
|
build-depends:
|
||||||
aeson >= 1.5.6 && < 1.6
|
base >= 4.7 && < 5,
|
||||||
, base >= 4.7 && < 5
|
conduit ^>= 1.3.4,
|
||||||
, conduit >= 1.3.4 && < 1.4
|
containers ^>= 0.6.2,
|
||||||
, containers >= 0.6.2 && < 0.7
|
exceptions ^>= 0.10.4,
|
||||||
, exceptions >= 0.10.4 && < 0.11
|
megaparsec >= 9.0 && < 10,
|
||||||
, hspec-expectations >= 0.8.2 && < 0.9
|
parser-combinators >= 1.3 && < 2,
|
||||||
, megaparsec >= 9.0.1 && < 9.1
|
template-haskell >= 2.16 && < 3,
|
||||||
, parser-combinators >= 1.3.0 && < 1.4
|
text >= 1.2 && < 3,
|
||||||
, scientific >= 0.3.7 && < 0.4
|
transformers ^>= 0.5.6,
|
||||||
, template-haskell >= 2.16 && < 2.18
|
unordered-containers ^>= 0.2.14,
|
||||||
, text >= 1.2.4 && < 1.3
|
vector ^>= 0.12.3
|
||||||
, transformers >= 0.5.6 && < 0.6
|
if flag(Json)
|
||||||
, unordered-containers >= 0.2.14 && < 0.3
|
build-depends:
|
||||||
, vector >= 0.12.3 && < 0.13
|
aeson >= 1.5.6 && < 1.6,
|
||||||
|
hspec-expectations >= 0.8.2 && < 0.9,
|
||||||
|
scientific >= 0.3.7 && < 0.4
|
||||||
|
cpp-options: -DWITH_JSON
|
||||||
|
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
|
||||||
test-suite graphql-test
|
test-suite graphql-test
|
||||||
@ -82,29 +92,30 @@ test-suite graphql-test
|
|||||||
Language.GraphQL.AST.EncoderSpec
|
Language.GraphQL.AST.EncoderSpec
|
||||||
Language.GraphQL.AST.LexerSpec
|
Language.GraphQL.AST.LexerSpec
|
||||||
Language.GraphQL.AST.ParserSpec
|
Language.GraphQL.AST.ParserSpec
|
||||||
|
Language.GraphQL.AST.Arbitrary
|
||||||
Language.GraphQL.ErrorSpec
|
Language.GraphQL.ErrorSpec
|
||||||
Language.GraphQL.Execute.CoerceSpec
|
Language.GraphQL.Execute.CoerceSpec
|
||||||
Language.GraphQL.Execute.OrderedMapSpec
|
Language.GraphQL.Execute.OrderedMapSpec
|
||||||
Language.GraphQL.ExecuteSpec
|
Language.GraphQL.ExecuteSpec
|
||||||
Language.GraphQL.Type.OutSpec
|
Language.GraphQL.Type.OutSpec
|
||||||
Language.GraphQL.Validate.RulesSpec
|
Language.GraphQL.Validate.RulesSpec
|
||||||
Test.DirectiveSpec
|
Schemas.HeroSchema
|
||||||
Test.FragmentSpec
|
|
||||||
Test.RootOperationSpec
|
|
||||||
hs-source-dirs:
|
hs-source-dirs:
|
||||||
tests
|
tests
|
||||||
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall
|
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall
|
||||||
|
|
||||||
build-depends:
|
build-depends:
|
||||||
QuickCheck >= 2.14.1 && < 2.15
|
QuickCheck ^>= 2.14.1,
|
||||||
, aeson
|
base,
|
||||||
, base >= 4.8 && < 5
|
conduit,
|
||||||
, conduit
|
exceptions,
|
||||||
, exceptions
|
graphql,
|
||||||
, graphql
|
hspec ^>= 2.9.1,
|
||||||
, hspec >= 2.8.2 && < 2.9
|
hspec-expectations ^>= 0.8.2,
|
||||||
, hspec-megaparsec >= 2.2.0 && < 2.3
|
hspec-megaparsec ^>= 2.2.0,
|
||||||
, megaparsec
|
megaparsec,
|
||||||
, scientific
|
text,
|
||||||
, text
|
unordered-containers,
|
||||||
, unordered-containers
|
containers,
|
||||||
|
vector
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
@ -1,7 +1,31 @@
|
|||||||
|
{-# LANGUAGE CPP #-}
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
{-# LANGUAGE RecordWildCards #-}
|
{-# LANGUAGE RecordWildCards #-}
|
||||||
|
|
||||||
|
#ifdef WITH_JSON
|
||||||
-- | This module provides the functions to parse and execute @GraphQL@ queries.
|
-- | This module provides the functions to parse and execute @GraphQL@ queries.
|
||||||
|
--
|
||||||
|
-- The content of this module depends on the value of the __json__ flag, which
|
||||||
|
-- is currently on by default. This behavior will change in the future, the flag
|
||||||
|
-- will be switched off by default and then removed.
|
||||||
|
--
|
||||||
|
-- This documentation is generated with the enabled __json__ flag and functions
|
||||||
|
-- described here support JSON and are deprecated. JSON instances are provided
|
||||||
|
-- now by an additional package, __graphql-spice__. To start using the new
|
||||||
|
-- package create __cabal.project__ in the root directory of your project with
|
||||||
|
-- the following contents:
|
||||||
|
--
|
||||||
|
-- @
|
||||||
|
-- packages: .
|
||||||
|
-- constraints: graphql -json
|
||||||
|
-- @
|
||||||
|
--
|
||||||
|
-- Then add __graphql-spice__ as dependency.
|
||||||
|
--
|
||||||
|
-- The new version of this module defines only one function, @graphql@, which
|
||||||
|
-- works with the internal GraphQL value representation used by this lbirary.
|
||||||
|
-- Refer to @Language.GraphQL.JSON.graphql@ in __graphql-spice__ for the
|
||||||
|
-- function that accepts and returns JSON.
|
||||||
module Language.GraphQL
|
module Language.GraphQL
|
||||||
( graphql
|
( graphql
|
||||||
, graphqlSubs
|
, graphqlSubs
|
||||||
@ -21,6 +45,7 @@ import qualified Language.GraphQL.Validate as Validate
|
|||||||
import Language.GraphQL.Type.Schema (Schema)
|
import Language.GraphQL.Type.Schema (Schema)
|
||||||
import Text.Megaparsec (parse)
|
import Text.Megaparsec (parse)
|
||||||
|
|
||||||
|
{-# DEPRECATED graphql "Use graphql-spice package instead" #-}
|
||||||
-- | If the text parses correctly as a @GraphQL@ query the query is
|
-- | If the text parses correctly as a @GraphQL@ query the query is
|
||||||
-- executed using the given 'Schema'.
|
-- executed using the given 'Schema'.
|
||||||
graphql :: MonadCatch m
|
graphql :: MonadCatch m
|
||||||
@ -29,6 +54,7 @@ graphql :: MonadCatch m
|
|||||||
-> m (Either (ResponseEventStream m Aeson.Value) Aeson.Object) -- ^ Response.
|
-> m (Either (ResponseEventStream m Aeson.Value) Aeson.Object) -- ^ Response.
|
||||||
graphql schema = graphqlSubs schema mempty mempty
|
graphql schema = graphqlSubs schema mempty mempty
|
||||||
|
|
||||||
|
{-# DEPRECATED graphqlSubs "Use graphql-spice package instead" #-}
|
||||||
-- | If the text parses correctly as a @GraphQL@ query the substitution is
|
-- | If the text parses correctly as a @GraphQL@ query the substitution is
|
||||||
-- applied to the query and the query is then executed using to the given
|
-- applied to the query and the query is then executed using to the given
|
||||||
-- 'Schema'.
|
-- 'Schema'.
|
||||||
@ -73,3 +99,49 @@ graphqlSubs schema operationName variableValues document' =
|
|||||||
[ ("line", Aeson.toJSON line)
|
[ ("line", Aeson.toJSON line)
|
||||||
, ("column", Aeson.toJSON column)
|
, ("column", Aeson.toJSON column)
|
||||||
]
|
]
|
||||||
|
#else
|
||||||
|
-- | This module provides the functions to parse and execute @GraphQL@ queries.
|
||||||
|
module Language.GraphQL
|
||||||
|
( graphql
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Control.Monad.Catch (MonadCatch)
|
||||||
|
import Data.HashMap.Strict (HashMap)
|
||||||
|
import qualified Data.Sequence as Seq
|
||||||
|
import Data.Text (Text)
|
||||||
|
import qualified Data.Text as Text
|
||||||
|
import qualified Language.GraphQL.AST as Full
|
||||||
|
import Language.GraphQL.Error
|
||||||
|
import Language.GraphQL.Execute
|
||||||
|
import qualified Language.GraphQL.Validate as Validate
|
||||||
|
import Language.GraphQL.Type.Schema (Schema)
|
||||||
|
import Prelude hiding (null)
|
||||||
|
import Text.Megaparsec (parse)
|
||||||
|
|
||||||
|
-- | If the text parses correctly as a @GraphQL@ query the query is
|
||||||
|
-- executed using the given 'Schema'.
|
||||||
|
--
|
||||||
|
-- An operation name can be given if the document contains multiple operations.
|
||||||
|
graphql :: (MonadCatch m, VariableValue a, Serialize b)
|
||||||
|
=> Schema m -- ^ Resolvers.
|
||||||
|
-> Maybe Text -- ^ Operation name.
|
||||||
|
-> HashMap Full.Name a -- ^ Variable substitution function.
|
||||||
|
-> Text -- ^ Text representing a @GraphQL@ request document.
|
||||||
|
-> m (Either (ResponseEventStream m b) (Response b)) -- ^ Response.
|
||||||
|
graphql schema operationName variableValues document' =
|
||||||
|
case parse Full.document "" document' of
|
||||||
|
Left errorBundle -> pure <$> parseError errorBundle
|
||||||
|
Right parsed ->
|
||||||
|
case validate parsed of
|
||||||
|
Seq.Empty -> execute schema operationName variableValues parsed
|
||||||
|
errors -> pure $ pure
|
||||||
|
$ Response null
|
||||||
|
$ fromValidationError <$> errors
|
||||||
|
where
|
||||||
|
validate = Validate.document schema Validate.specifiedRules
|
||||||
|
fromValidationError Validate.Error{..} = Error
|
||||||
|
{ message = Text.pack message
|
||||||
|
, locations = locations
|
||||||
|
, path = []
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
@ -49,6 +49,8 @@ module Language.GraphQL.AST.Document
|
|||||||
, Value(..)
|
, Value(..)
|
||||||
, VariableDefinition(..)
|
, VariableDefinition(..)
|
||||||
, escape
|
, escape
|
||||||
|
, showVariableName
|
||||||
|
, showVariable
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Char (ord)
|
import Data.Char (ord)
|
||||||
@ -339,6 +341,12 @@ data VariableDefinition =
|
|||||||
VariableDefinition Name Type (Maybe (Node ConstValue)) Location
|
VariableDefinition Name Type (Maybe (Node ConstValue)) Location
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
|
showVariableName :: VariableDefinition -> String
|
||||||
|
showVariableName (VariableDefinition name _ _ _) = "$" <> Text.unpack name
|
||||||
|
|
||||||
|
showVariable :: VariableDefinition -> String
|
||||||
|
showVariable var@(VariableDefinition _ type' _ _) = showVariableName var <> ":" <> " " <> show type'
|
||||||
|
|
||||||
-- ** Type References
|
-- ** Type References
|
||||||
|
|
||||||
-- | Type representation.
|
-- | Type representation.
|
||||||
|
@ -11,6 +11,7 @@ module Language.GraphQL.AST.Encoder
|
|||||||
, directive
|
, directive
|
||||||
, document
|
, document
|
||||||
, minified
|
, minified
|
||||||
|
, operationType
|
||||||
, pretty
|
, pretty
|
||||||
, type'
|
, type'
|
||||||
, value
|
, value
|
||||||
@ -34,7 +35,7 @@ import qualified Language.GraphQL.AST.Document as Full
|
|||||||
-- Use 'pretty' or 'minified' to construct the formatter.
|
-- Use 'pretty' or 'minified' to construct the formatter.
|
||||||
data Formatter
|
data Formatter
|
||||||
= Minified
|
= Minified
|
||||||
| Pretty Word
|
| Pretty !Word
|
||||||
|
|
||||||
-- | Constructs a formatter for pretty printing.
|
-- | Constructs a formatter for pretty printing.
|
||||||
pretty :: Formatter
|
pretty :: Formatter
|
||||||
@ -101,7 +102,7 @@ variableDefinition formatter variableDefinition' =
|
|||||||
in variable variableName
|
in variable variableName
|
||||||
<> eitherFormat formatter ": " ":"
|
<> eitherFormat formatter ": " ":"
|
||||||
<> type' variableType
|
<> type' variableType
|
||||||
<> maybe mempty (defaultValue formatter) (Full.node <$> defaultValue')
|
<> maybe mempty (defaultValue formatter . Full.node) defaultValue'
|
||||||
|
|
||||||
defaultValue :: Formatter -> Full.ConstValue -> Lazy.Text
|
defaultValue :: Formatter -> Full.ConstValue -> Lazy.Text
|
||||||
defaultValue formatter val
|
defaultValue formatter val
|
||||||
@ -294,6 +295,12 @@ nonNullType :: Full.NonNullType -> Lazy.Text
|
|||||||
nonNullType (Full.NonNullTypeNamed x) = Lazy.Text.fromStrict x <> "!"
|
nonNullType (Full.NonNullTypeNamed x) = Lazy.Text.fromStrict x <> "!"
|
||||||
nonNullType (Full.NonNullTypeList x) = listType x <> "!"
|
nonNullType (Full.NonNullTypeList x) = listType x <> "!"
|
||||||
|
|
||||||
|
-- | Produces lowercase operation type: query, mutation or subscription.
|
||||||
|
operationType :: Formatter -> Full.OperationType -> Lazy.Text
|
||||||
|
operationType _formatter Full.Query = "query"
|
||||||
|
operationType _formatter Full.Mutation = "mutation"
|
||||||
|
operationType _formatter Full.Subscription = "subscription"
|
||||||
|
|
||||||
-- * Internal
|
-- * Internal
|
||||||
|
|
||||||
between :: Char -> Char -> Lazy.Text -> Lazy.Text
|
between :: Char -> Char -> Lazy.Text -> Lazy.Text
|
||||||
|
@ -58,6 +58,7 @@ import qualified Text.Megaparsec.Char.Lexer as Lexer
|
|||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import qualified Data.Text.Lazy as TL
|
import qualified Data.Text.Lazy as TL
|
||||||
|
import Control.Monad (void)
|
||||||
|
|
||||||
-- | Standard parser.
|
-- | Standard parser.
|
||||||
-- Accepts the type of the parsed token.
|
-- Accepts the type of the parsed token.
|
||||||
@ -93,7 +94,7 @@ dollar = symbol "$"
|
|||||||
|
|
||||||
-- | Parser for "@".
|
-- | Parser for "@".
|
||||||
at :: Parser ()
|
at :: Parser ()
|
||||||
at = symbol "@" >> pure ()
|
at = void $ symbol "@"
|
||||||
|
|
||||||
-- | Parser for "&".
|
-- | Parser for "&".
|
||||||
amp :: Parser T.Text
|
amp :: Parser T.Text
|
||||||
@ -101,7 +102,7 @@ amp = symbol "&"
|
|||||||
|
|
||||||
-- | Parser for ":".
|
-- | Parser for ":".
|
||||||
colon :: Parser ()
|
colon :: Parser ()
|
||||||
colon = symbol ":" >> pure ()
|
colon = void $ symbol ":"
|
||||||
|
|
||||||
-- | Parser for "=".
|
-- | Parser for "=".
|
||||||
equals :: Parser T.Text
|
equals :: Parser T.Text
|
||||||
@ -220,7 +221,7 @@ escapeSequence = do
|
|||||||
|
|
||||||
-- | Parser for the "Byte Order Mark".
|
-- | Parser for the "Byte Order Mark".
|
||||||
unicodeBOM :: Parser ()
|
unicodeBOM :: Parser ()
|
||||||
unicodeBOM = optional (char '\xfeff') >> pure ()
|
unicodeBOM = void $ optional $ char '\xfeff'
|
||||||
|
|
||||||
-- | Parses "extend" followed by a 'symbol'. It is used by schema extensions.
|
-- | Parses "extend" followed by a 'symbol'. It is used by schema extensions.
|
||||||
extend :: forall a. Text -> String -> NonEmpty (Parser a) -> Parser a
|
extend :: forall a. Text -> String -> NonEmpty (Parser a) -> Parser a
|
||||||
|
@ -450,8 +450,8 @@ value = Full.Variable <$> variable
|
|||||||
<|> Full.Null <$ nullValue
|
<|> Full.Null <$ nullValue
|
||||||
<|> Full.String <$> stringValue
|
<|> Full.String <$> stringValue
|
||||||
<|> Full.Enum <$> try enumValue
|
<|> Full.Enum <$> try enumValue
|
||||||
<|> Full.List <$> brackets (some $ valueNode value)
|
<|> Full.List <$> brackets (many $ valueNode value)
|
||||||
<|> Full.Object <$> braces (some $ objectField $ valueNode value)
|
<|> Full.Object <$> braces (many $ objectField $ valueNode value)
|
||||||
<?> "Value"
|
<?> "Value"
|
||||||
|
|
||||||
constValue :: Parser Full.ConstValue
|
constValue :: Parser Full.ConstValue
|
||||||
|
@ -15,16 +15,13 @@ module Language.GraphQL.Error
|
|||||||
, ResolverException(..)
|
, ResolverException(..)
|
||||||
, Response(..)
|
, Response(..)
|
||||||
, ResponseEventStream
|
, ResponseEventStream
|
||||||
, addErr
|
|
||||||
, addErrMsg
|
|
||||||
, parseError
|
, parseError
|
||||||
, runCollectErrs
|
, runCollectErrs
|
||||||
, singleError
|
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Conduit
|
import Conduit
|
||||||
import Control.Exception (Exception(..))
|
import Control.Exception (Exception(..))
|
||||||
import Control.Monad.Trans.State (StateT, modify, runStateT)
|
import Control.Monad.Trans.State (StateT, runStateT)
|
||||||
import Data.HashMap.Strict (HashMap)
|
import Data.HashMap.Strict (HashMap)
|
||||||
import Data.Sequence (Seq(..), (|>))
|
import Data.Sequence (Seq(..), (|>))
|
||||||
import qualified Data.Sequence as Seq
|
import qualified Data.Sequence as Seq
|
||||||
@ -103,11 +100,9 @@ instance Exception ResolverException
|
|||||||
|
|
||||||
-- * Deprecated
|
-- * Deprecated
|
||||||
|
|
||||||
|
{-# DEPRECATED runCollectErrs "runCollectErrs was part of the old executor and isn't used anymore" #-}
|
||||||
-- | Runs the given query computation, but collects the errors into an error
|
-- | Runs the given query computation, but collects the errors into an error
|
||||||
-- list, which is then sent back with the data.
|
-- list, which is then sent back with the data.
|
||||||
--
|
|
||||||
-- /runCollectErrs was part of the old executor and isn't used anymore, it will
|
|
||||||
-- be deprecated in the future and removed./
|
|
||||||
runCollectErrs :: (Monad m, Serialize a)
|
runCollectErrs :: (Monad m, Serialize a)
|
||||||
=> HashMap Name (Schema.Type m)
|
=> HashMap Name (Schema.Type m)
|
||||||
-> CollectErrsT m a
|
-> CollectErrsT m a
|
||||||
@ -117,40 +112,13 @@ runCollectErrs types' res = do
|
|||||||
$ Resolution{ errors = Seq.empty, types = types' }
|
$ Resolution{ errors = Seq.empty, types = types' }
|
||||||
pure $ Response dat errors
|
pure $ Response dat errors
|
||||||
|
|
||||||
|
{-# DEPRECATED Resolution "Resolution was part of the old executor and isn't used anymore" #-}
|
||||||
-- | Executor context.
|
-- | Executor context.
|
||||||
--
|
|
||||||
-- /Resolution was part of the old executor and isn't used anymore, it will be
|
|
||||||
-- deprecated in the future and removed./
|
|
||||||
data Resolution m = Resolution
|
data Resolution m = Resolution
|
||||||
{ errors :: Seq Error
|
{ errors :: Seq Error
|
||||||
, types :: HashMap Name (Schema.Type m)
|
, types :: HashMap Name (Schema.Type m)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{-# DEPRECATED CollectErrsT "CollectErrsT was part of the old executor and isn't used anymore" #-}
|
||||||
-- | A wrapper to pass error messages around.
|
-- | A wrapper to pass error messages around.
|
||||||
--
|
|
||||||
-- /CollectErrsT was part of the old executor and isn't used anymore, it will be
|
|
||||||
-- deprecated in the future and removed./
|
|
||||||
type CollectErrsT m = StateT (Resolution m) m
|
type CollectErrsT m = StateT (Resolution m) m
|
||||||
|
|
||||||
-- | Adds an error to the list of errors.
|
|
||||||
{-# DEPRECATED #-}
|
|
||||||
addErr :: Monad m => Error -> CollectErrsT m ()
|
|
||||||
addErr v = modify appender
|
|
||||||
where
|
|
||||||
appender :: Monad m => Resolution m -> Resolution m
|
|
||||||
appender resolution@Resolution{..} = resolution{ errors = errors |> v }
|
|
||||||
|
|
||||||
{-# DEPRECATED #-}
|
|
||||||
makeErrorMessage :: Text -> Error
|
|
||||||
makeErrorMessage s = Error s [] []
|
|
||||||
|
|
||||||
-- | Constructs a response object containing only the error with the given
|
|
||||||
-- message.
|
|
||||||
{-# DEPRECATED #-}
|
|
||||||
singleError :: Serialize a => Text -> Response a
|
|
||||||
singleError message = Response null $ Seq.singleton $ Error message [] []
|
|
||||||
|
|
||||||
-- | Convenience function for just wrapping an error message.
|
|
||||||
{-# DEPRECATED #-}
|
|
||||||
addErrMsg :: (Monad m, Serialize a) => Text -> CollectErrsT m a
|
|
||||||
addErrMsg errorMessage = (addErr . makeErrorMessage) errorMessage >> pure null
|
|
||||||
|
@ -61,6 +61,7 @@ import Language.GraphQL.Error
|
|||||||
, ResponseEventStream
|
, ResponseEventStream
|
||||||
)
|
)
|
||||||
import Prelude hiding (null)
|
import Prelude hiding (null)
|
||||||
|
import Language.GraphQL.AST.Document (showVariableName)
|
||||||
|
|
||||||
newtype ExecutorT m a = ExecutorT
|
newtype ExecutorT m a = ExecutorT
|
||||||
{ runExecutorT :: ReaderT (HashMap Full.Name (Type m)) (WriterT (Seq Error) m) a
|
{ runExecutorT :: ReaderT (HashMap Full.Name (Type m)) (WriterT (Seq Error) m) a
|
||||||
@ -190,32 +191,42 @@ data QueryError
|
|||||||
tell :: Monad m => Seq Error -> ExecutorT m ()
|
tell :: Monad m => Seq Error -> ExecutorT m ()
|
||||||
tell = ExecutorT . lift . Writer.tell
|
tell = ExecutorT . lift . Writer.tell
|
||||||
|
|
||||||
|
operationNameErrorText :: Text
|
||||||
|
operationNameErrorText = Text.unlines
|
||||||
|
[ "Named operations must be provided with the name of the desired operation."
|
||||||
|
, "See https://spec.graphql.org/June2018/#sec-Language.Document description."
|
||||||
|
]
|
||||||
|
|
||||||
queryError :: QueryError -> Error
|
queryError :: QueryError -> Error
|
||||||
queryError OperationNameRequired =
|
queryError OperationNameRequired =
|
||||||
Error{ message = "Operation name is required.", locations = [], path = [] }
|
let queryErrorMessage = "Operation name is required. " <> operationNameErrorText
|
||||||
|
in Error{ message = queryErrorMessage, locations = [], path = [] }
|
||||||
queryError (OperationNotFound operationName) =
|
queryError (OperationNotFound operationName) =
|
||||||
let queryErrorMessage = Text.concat
|
let queryErrorMessage = Text.unlines
|
||||||
|
[ Text.concat
|
||||||
[ "Operation \""
|
[ "Operation \""
|
||||||
, Text.pack operationName
|
, Text.pack operationName
|
||||||
, "\" not found."
|
, "\" is not found in the named operations you've provided. "
|
||||||
|
]
|
||||||
|
, operationNameErrorText
|
||||||
]
|
]
|
||||||
in Error{ message = queryErrorMessage, locations = [], path = [] }
|
in Error{ message = queryErrorMessage, locations = [], path = [] }
|
||||||
queryError (CoercionError variableDefinition) =
|
queryError (CoercionError variableDefinition) =
|
||||||
let Full.VariableDefinition variableName _ _ location = variableDefinition
|
let (Full.VariableDefinition _ _ _ location) = variableDefinition
|
||||||
queryErrorMessage = Text.concat
|
queryErrorMessage = Text.concat
|
||||||
[ "Failed to coerce the variable \""
|
[ "Failed to coerce the variable "
|
||||||
, variableName
|
, Text.pack $ Full.showVariable variableDefinition
|
||||||
, "\"."
|
, "."
|
||||||
]
|
]
|
||||||
in Error{ message = queryErrorMessage, locations = [location], path = [] }
|
in Error{ message = queryErrorMessage, locations = [location], path = [] }
|
||||||
queryError (UnknownInputType variableDefinition) =
|
queryError (UnknownInputType variableDefinition) =
|
||||||
let Full.VariableDefinition variableName variableTypeName _ location = variableDefinition
|
let Full.VariableDefinition _ variableTypeName _ location = variableDefinition
|
||||||
queryErrorMessage = Text.concat
|
queryErrorMessage = Text.concat
|
||||||
[ "Variable \""
|
[ "Variable "
|
||||||
, variableName
|
, Text.pack $ showVariableName variableDefinition
|
||||||
, "\" has unknown type \""
|
, " has unknown type "
|
||||||
, Text.pack $ show variableTypeName
|
, Text.pack $ show variableTypeName
|
||||||
, "\"."
|
, "."
|
||||||
]
|
]
|
||||||
in Error{ message = queryErrorMessage, locations = [location], path = [] }
|
in Error{ message = queryErrorMessage, locations = [location], path = [] }
|
||||||
|
|
||||||
@ -375,6 +386,7 @@ executeField objectValue fields (viewResolver -> resolverPair) errorPath =
|
|||||||
, Handler (resolverHandler fieldLocation)
|
, Handler (resolverHandler fieldLocation)
|
||||||
]
|
]
|
||||||
where
|
where
|
||||||
|
fieldErrorPath = fieldsSegment fields : errorPath
|
||||||
inputCoercionHandler :: (MonadCatch m, Serialize a)
|
inputCoercionHandler :: (MonadCatch m, Serialize a)
|
||||||
=> Full.Location
|
=> Full.Location
|
||||||
-> InputCoercionException
|
-> InputCoercionException
|
||||||
@ -402,17 +414,16 @@ executeField objectValue fields (viewResolver -> resolverPair) errorPath =
|
|||||||
then throwM e
|
then throwM e
|
||||||
else returnError newError
|
else returnError newError
|
||||||
exceptionHandler errorLocation e =
|
exceptionHandler errorLocation e =
|
||||||
let newPath = fieldsSegment fields : errorPath
|
let newError = constructError e errorLocation fieldErrorPath
|
||||||
newError = constructError e errorLocation newPath
|
|
||||||
in if Out.isNonNullType fieldType
|
in if Out.isNonNullType fieldType
|
||||||
then throwM $ FieldException errorLocation newPath e
|
then throwM $ FieldException errorLocation fieldErrorPath e
|
||||||
else returnError newError
|
else returnError newError
|
||||||
returnError newError = tell (Seq.singleton newError) >> pure null
|
returnError newError = tell (Seq.singleton newError) >> pure null
|
||||||
go fieldName inputArguments = do
|
go fieldName inputArguments = do
|
||||||
argumentValues <- coerceArgumentValues argumentTypes inputArguments
|
argumentValues <- coerceArgumentValues argumentTypes inputArguments
|
||||||
resolvedValue <-
|
resolvedValue <-
|
||||||
resolveFieldValue resolveFunction objectValue fieldName argumentValues
|
resolveFieldValue resolveFunction objectValue fieldName argumentValues
|
||||||
completeValue fieldType fields errorPath resolvedValue
|
completeValue fieldType fields fieldErrorPath resolvedValue
|
||||||
(resolverField, resolveFunction) = resolverPair
|
(resolverField, resolveFunction) = resolverPair
|
||||||
Out.Field _ fieldType argumentTypes = resolverField
|
Out.Field _ fieldType argumentTypes = resolverField
|
||||||
|
|
||||||
@ -445,6 +456,7 @@ resolveAbstractType abstractType values'
|
|||||||
_ -> pure Nothing
|
_ -> pure Nothing
|
||||||
| otherwise = pure Nothing
|
| otherwise = pure Nothing
|
||||||
|
|
||||||
|
-- https://spec.graphql.org/October2021/#sec-Value-Completion
|
||||||
completeValue :: (MonadCatch m, Serialize a)
|
completeValue :: (MonadCatch m, Serialize a)
|
||||||
=> Out.Type m
|
=> Out.Type m
|
||||||
-> NonEmpty (Transform.Field m)
|
-> NonEmpty (Transform.Field m)
|
||||||
@ -476,8 +488,7 @@ completeValue outputType@(Out.EnumBaseType enumType) _ _ (Type.Enum enum) =
|
|||||||
$ ValueCompletionException (show outputType)
|
$ ValueCompletionException (show outputType)
|
||||||
$ Type.Enum enum
|
$ Type.Enum enum
|
||||||
completeValue (Out.ObjectBaseType objectType) fields errorPath result
|
completeValue (Out.ObjectBaseType objectType) fields errorPath result
|
||||||
= executeSelectionSet (mergeSelectionSets fields) objectType result
|
= executeSelectionSet (mergeSelectionSets fields) objectType result errorPath
|
||||||
$ fieldsSegment fields : errorPath
|
|
||||||
completeValue outputType@(Out.InterfaceBaseType interfaceType) fields errorPath result
|
completeValue outputType@(Out.InterfaceBaseType interfaceType) fields errorPath result
|
||||||
| Type.Object objectMap <- result = do
|
| Type.Object objectMap <- result = do
|
||||||
let abstractType = Type.Internal.AbstractInterfaceType interfaceType
|
let abstractType = Type.Internal.AbstractInterfaceType interfaceType
|
||||||
|
@ -5,8 +5,14 @@
|
|||||||
{-# LANGUAGE ExplicitForAll #-}
|
{-# LANGUAGE ExplicitForAll #-}
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
{-# LANGUAGE ViewPatterns #-}
|
{-# LANGUAGE ViewPatterns #-}
|
||||||
|
{-# LANGUAGE CPP #-}
|
||||||
|
|
||||||
-- | Types and functions used for input and result coercion.
|
-- | Types and functions used for input and result coercion.
|
||||||
|
--
|
||||||
|
-- JSON instances in this module are only available with the __json__
|
||||||
|
-- flag that is currently on by default, but will be disabled in the future.
|
||||||
|
-- Refer to the documentation in the 'Language.GraphQL' module and to
|
||||||
|
-- the __graphql-spice__ package.
|
||||||
module Language.GraphQL.Execute.Coerce
|
module Language.GraphQL.Execute.Coerce
|
||||||
( Output(..)
|
( Output(..)
|
||||||
, Serialize(..)
|
, Serialize(..)
|
||||||
@ -15,7 +21,10 @@ module Language.GraphQL.Execute.Coerce
|
|||||||
, matchFieldValues
|
, matchFieldValues
|
||||||
) where
|
) where
|
||||||
|
|
||||||
|
#ifdef WITH_JSON
|
||||||
import qualified Data.Aeson as Aeson
|
import qualified Data.Aeson as Aeson
|
||||||
|
import Data.Scientific (toBoundedInteger, toRealFloat)
|
||||||
|
#endif
|
||||||
import Data.Int (Int32)
|
import Data.Int (Int32)
|
||||||
import Data.HashMap.Strict (HashMap)
|
import Data.HashMap.Strict (HashMap)
|
||||||
import qualified Data.HashMap.Strict as HashMap
|
import qualified Data.HashMap.Strict as HashMap
|
||||||
@ -24,7 +33,6 @@ import Data.Text (Text)
|
|||||||
import qualified Data.Text.Lazy as Text.Lazy
|
import qualified Data.Text.Lazy as Text.Lazy
|
||||||
import qualified Data.Text.Lazy.Builder as Text.Builder
|
import qualified Data.Text.Lazy.Builder as Text.Builder
|
||||||
import qualified Data.Text.Lazy.Builder.Int as Text.Builder
|
import qualified Data.Text.Lazy.Builder.Int as Text.Builder
|
||||||
import Data.Scientific (toBoundedInteger, toRealFloat)
|
|
||||||
import Language.GraphQL.AST (Name)
|
import Language.GraphQL.AST (Name)
|
||||||
import Language.GraphQL.Execute.OrderedMap (OrderedMap)
|
import Language.GraphQL.Execute.OrderedMap (OrderedMap)
|
||||||
import qualified Language.GraphQL.Execute.OrderedMap as OrderedMap
|
import qualified Language.GraphQL.Execute.OrderedMap as OrderedMap
|
||||||
@ -61,20 +69,13 @@ class VariableValue a where
|
|||||||
-> a -- ^ Variable value being coerced.
|
-> a -- ^ Variable value being coerced.
|
||||||
-> Maybe Type.Value -- ^ Coerced value on success, 'Nothing' otherwise.
|
-> Maybe Type.Value -- ^ Coerced value on success, 'Nothing' otherwise.
|
||||||
|
|
||||||
instance VariableValue Aeson.Value where
|
instance VariableValue Type.Value where
|
||||||
coerceVariableValue _ Aeson.Null = Just Type.Null
|
coerceVariableValue _ Type.Null = Just Type.Null
|
||||||
coerceVariableValue (In.ScalarBaseType scalarType) value
|
coerceVariableValue (In.ScalarBaseType _) value = Just value
|
||||||
| (Aeson.String stringValue) <- value = Just $ Type.String stringValue
|
coerceVariableValue (In.EnumBaseType _) (Type.Enum stringValue) =
|
||||||
| (Aeson.Bool booleanValue) <- value = Just $ Type.Boolean booleanValue
|
|
||||||
| (Aeson.Number numberValue) <- value
|
|
||||||
, (Type.ScalarType "Float" _) <- scalarType =
|
|
||||||
Just $ Type.Float $ toRealFloat numberValue
|
|
||||||
| (Aeson.Number numberValue) <- value = -- ID or Int
|
|
||||||
Type.Int <$> toBoundedInteger numberValue
|
|
||||||
coerceVariableValue (In.EnumBaseType _) (Aeson.String stringValue) =
|
|
||||||
Just $ Type.Enum stringValue
|
Just $ Type.Enum stringValue
|
||||||
coerceVariableValue (In.InputObjectBaseType objectType) value
|
coerceVariableValue (In.InputObjectBaseType objectType) value
|
||||||
| (Aeson.Object objectValue) <- value = do
|
| (Type.Object objectValue) <- value = do
|
||||||
let (In.InputObjectType _ _ inputFields) = objectType
|
let (In.InputObjectType _ _ inputFields) = objectType
|
||||||
(newObjectValue, resultMap) <- foldWithKey objectValue inputFields
|
(newObjectValue, resultMap) <- foldWithKey objectValue inputFields
|
||||||
if HashMap.null newObjectValue
|
if HashMap.null newObjectValue
|
||||||
@ -94,14 +95,9 @@ instance VariableValue Aeson.Value where
|
|||||||
pure (newObjectValue, insert coerced)
|
pure (newObjectValue, insert coerced)
|
||||||
Nothing -> Just (objectValue, resultMap)
|
Nothing -> Just (objectValue, resultMap)
|
||||||
coerceVariableValue (In.ListBaseType listType) value
|
coerceVariableValue (In.ListBaseType listType) value
|
||||||
| (Aeson.Array arrayValue) <- value =
|
| (Type.List arrayValue) <- value =
|
||||||
Type.List <$> foldr foldVector (Just []) arrayValue
|
Type.List <$> traverse (coerceVariableValue listType) arrayValue
|
||||||
| otherwise = coerceVariableValue listType value
|
| otherwise = coerceVariableValue listType value
|
||||||
where
|
|
||||||
foldVector _ Nothing = Nothing
|
|
||||||
foldVector variableValue (Just list) = do
|
|
||||||
coerced <- coerceVariableValue listType variableValue
|
|
||||||
pure $ coerced : list
|
|
||||||
coerceVariableValue _ _ = Nothing
|
coerceVariableValue _ _ = Nothing
|
||||||
|
|
||||||
-- | Looks up a value by name in the given map, coerces it and inserts into the
|
-- | Looks up a value by name in the given map, coerces it and inserts into the
|
||||||
@ -216,6 +212,28 @@ data Output a
|
|||||||
instance forall a. IsString (Output a) where
|
instance forall a. IsString (Output a) where
|
||||||
fromString = String . fromString
|
fromString = String . fromString
|
||||||
|
|
||||||
|
instance Serialize Type.Value where
|
||||||
|
null = Type.Null
|
||||||
|
serialize (Out.ScalarBaseType scalarType) value
|
||||||
|
| Type.ScalarType "Int" _ <- scalarType
|
||||||
|
, Int int <- value = Just $ Type.Int int
|
||||||
|
| Type.ScalarType "Float" _ <- scalarType
|
||||||
|
, Float float <- value = Just $ Type.Float float
|
||||||
|
| Type.ScalarType "String" _ <- scalarType
|
||||||
|
, String string <- value = Just $ Type.String string
|
||||||
|
| Type.ScalarType "ID" _ <- scalarType
|
||||||
|
, String string <- value = Just $ Type.String string
|
||||||
|
| Type.ScalarType "Boolean" _ <- scalarType
|
||||||
|
, Boolean boolean <- value = Just $ Type.Boolean boolean
|
||||||
|
serialize _ (Enum enum) = Just $ Type.Enum enum
|
||||||
|
serialize _ (List list) = Just $ Type.List list
|
||||||
|
serialize _ (Object object) = Just
|
||||||
|
$ Type.Object
|
||||||
|
$ HashMap.fromList
|
||||||
|
$ OrderedMap.toList object
|
||||||
|
serialize _ _ = Nothing
|
||||||
|
|
||||||
|
#ifdef WITH_JSON
|
||||||
instance Serialize Aeson.Value where
|
instance Serialize Aeson.Value where
|
||||||
serialize (Out.ScalarBaseType scalarType) value
|
serialize (Out.ScalarBaseType scalarType) value
|
||||||
| Type.ScalarType "Int" _ <- scalarType
|
| Type.ScalarType "Int" _ <- scalarType
|
||||||
@ -236,3 +254,47 @@ instance Serialize Aeson.Value where
|
|||||||
$ Aeson.toJSON <$> object
|
$ Aeson.toJSON <$> object
|
||||||
serialize _ _ = Nothing
|
serialize _ _ = Nothing
|
||||||
null = Aeson.Null
|
null = Aeson.Null
|
||||||
|
|
||||||
|
instance VariableValue Aeson.Value where
|
||||||
|
coerceVariableValue _ Aeson.Null = Just Type.Null
|
||||||
|
coerceVariableValue (In.ScalarBaseType scalarType) value
|
||||||
|
| (Aeson.String stringValue) <- value = Just $ Type.String stringValue
|
||||||
|
| (Aeson.Bool booleanValue) <- value = Just $ Type.Boolean booleanValue
|
||||||
|
| (Aeson.Number numberValue) <- value
|
||||||
|
, (Type.ScalarType "Float" _) <- scalarType =
|
||||||
|
Just $ Type.Float $ toRealFloat numberValue
|
||||||
|
| (Aeson.Number numberValue) <- value = -- ID or Int
|
||||||
|
Type.Int <$> toBoundedInteger numberValue
|
||||||
|
coerceVariableValue (In.EnumBaseType _) (Aeson.String stringValue) =
|
||||||
|
Just $ Type.Enum stringValue
|
||||||
|
coerceVariableValue (In.InputObjectBaseType objectType) value
|
||||||
|
| (Aeson.Object objectValue) <- value = do
|
||||||
|
let (In.InputObjectType _ _ inputFields) = objectType
|
||||||
|
(newObjectValue, resultMap) <- foldWithKey objectValue inputFields
|
||||||
|
if HashMap.null newObjectValue
|
||||||
|
then Just $ Type.Object resultMap
|
||||||
|
else Nothing
|
||||||
|
where
|
||||||
|
foldWithKey objectValue = HashMap.foldrWithKey matchFieldValues'
|
||||||
|
$ Just (objectValue, HashMap.empty)
|
||||||
|
matchFieldValues' _ _ Nothing = Nothing
|
||||||
|
matchFieldValues' fieldName inputField (Just (objectValue, resultMap)) =
|
||||||
|
let (In.InputField _ fieldType _) = inputField
|
||||||
|
insert = flip (HashMap.insert fieldName) resultMap
|
||||||
|
newObjectValue = HashMap.delete fieldName objectValue
|
||||||
|
in case HashMap.lookup fieldName objectValue of
|
||||||
|
Just variableValue -> do
|
||||||
|
coerced <- coerceVariableValue fieldType variableValue
|
||||||
|
pure (newObjectValue, insert coerced)
|
||||||
|
Nothing -> Just (objectValue, resultMap)
|
||||||
|
coerceVariableValue (In.ListBaseType listType) value
|
||||||
|
| (Aeson.Array arrayValue) <- value =
|
||||||
|
Type.List <$> foldr foldVector (Just []) arrayValue
|
||||||
|
| otherwise = coerceVariableValue listType value
|
||||||
|
where
|
||||||
|
foldVector _ Nothing = Nothing
|
||||||
|
foldVector variableValue (Just list) = do
|
||||||
|
coerced <- coerceVariableValue listType variableValue
|
||||||
|
pure $ coerced : list
|
||||||
|
coerceVariableValue _ _ = Nothing
|
||||||
|
#endif
|
||||||
|
@ -205,5 +205,5 @@ collectImplementations = HashMap.foldr go HashMap.empty
|
|||||||
let Out.ObjectType _ _ interfaces _ = objectType
|
let Out.ObjectType _ _ interfaces _ = objectType
|
||||||
in foldr (add implementation) accumulator interfaces
|
in foldr (add implementation) accumulator interfaces
|
||||||
go _ accumulator = accumulator
|
go _ accumulator = accumulator
|
||||||
add implementation (Out.InterfaceType typeName _ _ _) accumulator =
|
add implementation (Out.InterfaceType typeName _ _ _) =
|
||||||
HashMap.insertWith (++) typeName [implementation] accumulator
|
HashMap.insertWith (++) typeName [implementation]
|
||||||
|
@ -54,7 +54,7 @@ import Data.HashMap.Strict (HashMap)
|
|||||||
import Data.HashSet (HashSet)
|
import Data.HashSet (HashSet)
|
||||||
import qualified Data.HashSet as HashSet
|
import qualified Data.HashSet as HashSet
|
||||||
import Data.List (groupBy, sortBy, sortOn)
|
import Data.List (groupBy, sortBy, sortOn)
|
||||||
import Data.Maybe (catMaybes, fromMaybe, isJust, isNothing, mapMaybe)
|
import Data.Maybe (fromMaybe, isJust, isNothing, mapMaybe)
|
||||||
import Data.List.NonEmpty (NonEmpty(..))
|
import Data.List.NonEmpty (NonEmpty(..))
|
||||||
import Data.Ord (comparing)
|
import Data.Ord (comparing)
|
||||||
import Data.Sequence (Seq(..), (|>))
|
import Data.Sequence (Seq(..), (|>))
|
||||||
@ -152,7 +152,7 @@ singleFieldSubscriptionsRule = OperationDefinitionRule $ \case
|
|||||||
where
|
where
|
||||||
errorMessage =
|
errorMessage =
|
||||||
"Anonymous Subscription must select only one top level field."
|
"Anonymous Subscription must select only one top level field."
|
||||||
collectFields selectionSet = foldM forEach HashSet.empty selectionSet
|
collectFields = foldM forEach HashSet.empty
|
||||||
forEach accumulator = \case
|
forEach accumulator = \case
|
||||||
Full.FieldSelection fieldSelection -> forField accumulator fieldSelection
|
Full.FieldSelection fieldSelection -> forField accumulator fieldSelection
|
||||||
Full.FragmentSpreadSelection fragmentSelection ->
|
Full.FragmentSpreadSelection fragmentSelection ->
|
||||||
@ -472,7 +472,7 @@ noFragmentCyclesRule = FragmentDefinitionRule $ \case
|
|||||||
collectCycles :: Traversable t
|
collectCycles :: Traversable t
|
||||||
=> t Full.Selection
|
=> t Full.Selection
|
||||||
-> StateT (Int, Full.Name) (ReaderT (Validation m) Seq) (HashMap Full.Name Int)
|
-> StateT (Int, Full.Name) (ReaderT (Validation m) Seq) (HashMap Full.Name Int)
|
||||||
collectCycles selectionSet = foldM forEach HashMap.empty selectionSet
|
collectCycles = foldM forEach HashMap.empty
|
||||||
forEach accumulator = \case
|
forEach accumulator = \case
|
||||||
Full.FieldSelection fieldSelection -> forField accumulator fieldSelection
|
Full.FieldSelection fieldSelection -> forField accumulator fieldSelection
|
||||||
Full.InlineFragmentSelection fragmentSelection ->
|
Full.InlineFragmentSelection fragmentSelection ->
|
||||||
@ -702,8 +702,7 @@ uniqueInputFieldNamesRule =
|
|||||||
where
|
where
|
||||||
go (Full.Node (Full.Object fields) _) = filterFieldDuplicates fields
|
go (Full.Node (Full.Object fields) _) = filterFieldDuplicates fields
|
||||||
go _ = mempty
|
go _ = mempty
|
||||||
filterFieldDuplicates fields =
|
filterFieldDuplicates = filterDuplicates getFieldName "input field"
|
||||||
filterDuplicates getFieldName "input field" fields
|
|
||||||
getFieldName (Full.ObjectField fieldName _ location') = (fieldName, location')
|
getFieldName (Full.ObjectField fieldName _ location') = (fieldName, location')
|
||||||
constGo (Full.Node (Full.ConstObject fields) _) = filterFieldDuplicates fields
|
constGo (Full.Node (Full.ConstObject fields) _) = filterFieldDuplicates fields
|
||||||
constGo _ = mempty
|
constGo _ = mempty
|
||||||
@ -1331,8 +1330,8 @@ variablesInAllowedPositionRule = OperationDefinitionRule $ \case
|
|||||||
-> Type.CompositeType m
|
-> Type.CompositeType m
|
||||||
-> t Full.Selection
|
-> t Full.Selection
|
||||||
-> ValidationState m (Seq Error)
|
-> ValidationState m (Seq Error)
|
||||||
visitSelectionSet variables selectionType selections =
|
visitSelectionSet variables selectionType =
|
||||||
foldM (evaluateSelection variables selectionType) mempty selections
|
foldM (evaluateSelection variables selectionType) mempty
|
||||||
evaluateFieldSelection variables selections accumulator = \case
|
evaluateFieldSelection variables selections accumulator = \case
|
||||||
Just newParentType -> do
|
Just newParentType -> do
|
||||||
let folder = evaluateSelection variables newParentType
|
let folder = evaluateSelection variables newParentType
|
||||||
@ -1552,9 +1551,9 @@ valuesOfCorrectTypeRule = ValueRule go constGo
|
|||||||
toConst Full.Null = Just Full.ConstNull
|
toConst Full.Null = Just Full.ConstNull
|
||||||
toConst (Full.Enum enum) = Just $ Full.ConstEnum enum
|
toConst (Full.Enum enum) = Just $ Full.ConstEnum enum
|
||||||
toConst (Full.List values) =
|
toConst (Full.List values) =
|
||||||
Just $ Full.ConstList $ catMaybes $ toConstNode <$> values
|
Just $ Full.ConstList $ mapMaybe toConstNode values
|
||||||
toConst (Full.Object fields) = Just $ Full.ConstObject
|
toConst (Full.Object fields) = Just $ Full.ConstObject
|
||||||
$ catMaybes $ constObjectField <$> fields
|
$ mapMaybe constObjectField fields
|
||||||
constObjectField Full.ObjectField{..}
|
constObjectField Full.ObjectField{..}
|
||||||
| Just constValue <- toConstNode value =
|
| Just constValue <- toConstNode value =
|
||||||
Just $ Full.ObjectField name constValue location
|
Just $ Full.ObjectField name constValue location
|
||||||
@ -1617,4 +1616,3 @@ valuesOfCorrectTypeRule = ValueRule go constGo
|
|||||||
}
|
}
|
||||||
| otherwise -> mempty
|
| otherwise -> mempty
|
||||||
_ -> checkResult
|
_ -> checkResult
|
||||||
|
|
||||||
|
@ -2,11 +2,14 @@
|
|||||||
v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||||
obtain one at https://mozilla.org/MPL/2.0/. -}
|
obtain one at https://mozilla.org/MPL/2.0/. -}
|
||||||
|
|
||||||
|
{-# LANGUAGE CPP #-}
|
||||||
|
|
||||||
|
#ifdef WITH_JSON
|
||||||
{-# LANGUAGE ExplicitForAll #-}
|
{-# LANGUAGE ExplicitForAll #-}
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
-- | Test helpers.
|
-- | Test helpers.
|
||||||
module Test.Hspec.GraphQL
|
module Test.Hspec.GraphQL {-# DEPRECATED "Use graphql-spice package instead" #-}
|
||||||
( shouldResolve
|
( shouldResolve
|
||||||
, shouldResolveTo
|
, shouldResolveTo
|
||||||
) where
|
) where
|
||||||
@ -39,3 +42,8 @@ shouldResolve executor query = do
|
|||||||
response `shouldNotSatisfy` HashMap.member "errors"
|
response `shouldNotSatisfy` HashMap.member "errors"
|
||||||
_ -> expectationFailure
|
_ -> expectationFailure
|
||||||
"the query is expected to resolve to a value, but it resolved to an event stream"
|
"the query is expected to resolve to a value, but it resolved to an event stream"
|
||||||
|
#else
|
||||||
|
module Test.Hspec.GraphQL {-# DEPRECATED "Use graphql-spice package instead" #-}
|
||||||
|
(
|
||||||
|
) where
|
||||||
|
#endif
|
||||||
|
99
tests/Language/GraphQL/AST/Arbitrary.hs
Normal file
99
tests/Language/GraphQL/AST/Arbitrary.hs
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
|
module Language.GraphQL.AST.Arbitrary where
|
||||||
|
|
||||||
|
import qualified Language.GraphQL.AST.Document as Doc
|
||||||
|
import Test.QuickCheck.Arbitrary (Arbitrary (arbitrary))
|
||||||
|
import Test.QuickCheck (oneof, elements, listOf, resize, NonEmptyList (..))
|
||||||
|
import Test.QuickCheck.Gen (Gen (..))
|
||||||
|
import Data.Text (Text, pack)
|
||||||
|
|
||||||
|
newtype AnyPrintableChar = AnyPrintableChar { getAnyPrintableChar :: Char } deriving (Eq, Show)
|
||||||
|
|
||||||
|
alpha :: String
|
||||||
|
alpha = ['a'..'z'] <> ['A'..'Z']
|
||||||
|
|
||||||
|
num :: String
|
||||||
|
num = ['0'..'9']
|
||||||
|
|
||||||
|
instance Arbitrary AnyPrintableChar where
|
||||||
|
arbitrary = AnyPrintableChar <$> elements chars
|
||||||
|
where
|
||||||
|
chars = alpha <> num <> ['_']
|
||||||
|
|
||||||
|
newtype AnyPrintableText = AnyPrintableText { getAnyPrintableText :: Text } deriving (Eq, Show)
|
||||||
|
|
||||||
|
instance Arbitrary AnyPrintableText where
|
||||||
|
arbitrary = do
|
||||||
|
nonEmptyStr <- getNonEmpty <$> (arbitrary :: Gen (NonEmptyList AnyPrintableChar))
|
||||||
|
pure $ AnyPrintableText (pack $ map getAnyPrintableChar nonEmptyStr)
|
||||||
|
|
||||||
|
-- https://spec.graphql.org/June2018/#Name
|
||||||
|
newtype AnyName = AnyName { getAnyName :: Text } deriving (Eq, Show)
|
||||||
|
|
||||||
|
instance Arbitrary AnyName where
|
||||||
|
arbitrary = do
|
||||||
|
firstChar <- elements $ alpha <> ['_']
|
||||||
|
rest <- (arbitrary :: Gen [AnyPrintableChar])
|
||||||
|
pure $ AnyName (pack $ firstChar : map getAnyPrintableChar rest)
|
||||||
|
|
||||||
|
newtype AnyLocation = AnyLocation { getAnyLocation :: Doc.Location } deriving (Eq, Show)
|
||||||
|
|
||||||
|
instance Arbitrary AnyLocation where
|
||||||
|
arbitrary = AnyLocation <$> (Doc.Location <$> arbitrary <*> arbitrary)
|
||||||
|
|
||||||
|
newtype AnyNode a = AnyNode { getAnyNode :: Doc.Node a } deriving (Eq, Show)
|
||||||
|
|
||||||
|
instance Arbitrary a => Arbitrary (AnyNode a) where
|
||||||
|
arbitrary = do
|
||||||
|
(AnyLocation location') <- arbitrary
|
||||||
|
node' <- flip Doc.Node location' <$> arbitrary
|
||||||
|
pure $ AnyNode node'
|
||||||
|
|
||||||
|
newtype AnyObjectField a = AnyObjectField { getAnyObjectField :: Doc.ObjectField a } deriving (Eq, Show)
|
||||||
|
|
||||||
|
instance Arbitrary a => Arbitrary (AnyObjectField a) where
|
||||||
|
arbitrary = do
|
||||||
|
name' <- getAnyName <$> arbitrary
|
||||||
|
value' <- getAnyNode <$> arbitrary
|
||||||
|
location' <- getAnyLocation <$> arbitrary
|
||||||
|
pure $ AnyObjectField $ Doc.ObjectField name' value' location'
|
||||||
|
|
||||||
|
newtype AnyValue = AnyValue { getAnyValue :: Doc.Value } deriving (Eq, Show)
|
||||||
|
|
||||||
|
instance Arbitrary AnyValue where
|
||||||
|
arbitrary = AnyValue <$> oneof
|
||||||
|
[ variableGen
|
||||||
|
, Doc.Int <$> arbitrary
|
||||||
|
, Doc.Float <$> arbitrary
|
||||||
|
, Doc.String <$> (getAnyPrintableText <$> arbitrary)
|
||||||
|
, Doc.Boolean <$> arbitrary
|
||||||
|
, MkGen $ \_ _ -> Doc.Null
|
||||||
|
, Doc.Enum <$> (getAnyName <$> arbitrary)
|
||||||
|
, Doc.List <$> listGen
|
||||||
|
, Doc.Object <$> objectGen
|
||||||
|
]
|
||||||
|
where
|
||||||
|
variableGen :: Gen Doc.Value
|
||||||
|
variableGen = Doc.Variable <$> (getAnyName <$> arbitrary)
|
||||||
|
listGen :: Gen [Doc.Node Doc.Value]
|
||||||
|
listGen = (resize 5 . listOf) nodeGen
|
||||||
|
nodeGen = do
|
||||||
|
node' <- getAnyNode <$> (arbitrary :: Gen (AnyNode AnyValue))
|
||||||
|
pure (getAnyValue <$> node')
|
||||||
|
objectGen :: Gen [Doc.ObjectField Doc.Value]
|
||||||
|
objectGen = resize 1 $ do
|
||||||
|
list <- getNonEmpty <$> (arbitrary :: Gen (NonEmptyList (AnyObjectField AnyValue)))
|
||||||
|
pure $ map (fmap getAnyValue . getAnyObjectField) list
|
||||||
|
|
||||||
|
newtype AnyArgument a = AnyArgument { getAnyArgument :: Doc.Argument } deriving (Eq, Show)
|
||||||
|
|
||||||
|
instance Arbitrary a => Arbitrary (AnyArgument a) where
|
||||||
|
arbitrary = do
|
||||||
|
name' <- getAnyName <$> arbitrary
|
||||||
|
(AnyValue value') <- arbitrary
|
||||||
|
(AnyLocation location') <- arbitrary
|
||||||
|
pure $ AnyArgument $ Doc.Argument name' (Doc.Node value' location') location'
|
||||||
|
|
||||||
|
printArgument :: AnyArgument AnyValue -> Text
|
||||||
|
printArgument (AnyArgument (Doc.Argument name' (Doc.Node value' _) _)) = name' <> ": " <> (pack . show) value'
|
@ -173,3 +173,8 @@ spec = do
|
|||||||
|] '\n'
|
|] '\n'
|
||||||
actual = definition pretty operation
|
actual = definition pretty operation
|
||||||
in actual `shouldBe` expected
|
in actual `shouldBe` expected
|
||||||
|
|
||||||
|
describe "operationType" $
|
||||||
|
it "produces lowercase mutation operation type" $
|
||||||
|
let actual = operationType pretty Full.Mutation
|
||||||
|
in actual `shouldBe` "mutation"
|
||||||
|
@ -5,19 +5,24 @@ module Language.GraphQL.AST.ParserSpec
|
|||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.List.NonEmpty (NonEmpty(..))
|
import Data.List.NonEmpty (NonEmpty(..))
|
||||||
|
import Data.Text (Text)
|
||||||
|
import qualified Data.Text as Text
|
||||||
import Language.GraphQL.AST.Document
|
import Language.GraphQL.AST.Document
|
||||||
import qualified Language.GraphQL.AST.DirectiveLocation as DirLoc
|
import qualified Language.GraphQL.AST.DirectiveLocation as DirLoc
|
||||||
import Language.GraphQL.AST.Parser
|
import Language.GraphQL.AST.Parser
|
||||||
import Language.GraphQL.TH
|
import Language.GraphQL.TH
|
||||||
import Test.Hspec (Spec, describe, it)
|
import Test.Hspec (Spec, describe, it, context)
|
||||||
import Test.Hspec.Megaparsec (shouldParse, shouldFailOn, shouldSucceedOn)
|
import Test.Hspec.Megaparsec (shouldParse, shouldFailOn, shouldSucceedOn)
|
||||||
import Text.Megaparsec (parse)
|
import Text.Megaparsec (parse)
|
||||||
|
import Test.QuickCheck (property, NonEmptyList (..), mapSize)
|
||||||
|
import Language.GraphQL.AST.Arbitrary
|
||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec = describe "Parser" $ do
|
spec = describe "Parser" $ do
|
||||||
it "accepts BOM header" $
|
it "accepts BOM header" $
|
||||||
parse document "" `shouldSucceedOn` "\xfeff{foo}"
|
parse document "" `shouldSucceedOn` "\xfeff{foo}"
|
||||||
|
|
||||||
|
context "Arguments" $ do
|
||||||
it "accepts block strings as argument" $
|
it "accepts block strings as argument" $
|
||||||
parse document "" `shouldSucceedOn` [gql|{
|
parse document "" `shouldSucceedOn` [gql|{
|
||||||
hello(text: """Argument""")
|
hello(text: """Argument""")
|
||||||
@ -28,6 +33,26 @@ spec = describe "Parser" $ do
|
|||||||
hello(text: "Argument")
|
hello(text: "Argument")
|
||||||
}|]
|
}|]
|
||||||
|
|
||||||
|
it "accepts int as argument1" $
|
||||||
|
parse document "" `shouldSucceedOn` [gql|{
|
||||||
|
user(id: 4)
|
||||||
|
}|]
|
||||||
|
|
||||||
|
it "accepts boolean as argument" $
|
||||||
|
parse document "" `shouldSucceedOn` [gql|{
|
||||||
|
hello(flag: true) { field1 }
|
||||||
|
}|]
|
||||||
|
|
||||||
|
it "accepts float as argument" $
|
||||||
|
parse document "" `shouldSucceedOn` [gql|{
|
||||||
|
body(height: 172.5) { height }
|
||||||
|
}|]
|
||||||
|
|
||||||
|
it "accepts empty list as argument" $
|
||||||
|
parse document "" `shouldSucceedOn` [gql|{
|
||||||
|
query(list: []) { field1 }
|
||||||
|
}|]
|
||||||
|
|
||||||
it "accepts two required arguments" $
|
it "accepts two required arguments" $
|
||||||
parse document "" `shouldSucceedOn` [gql|
|
parse document "" `shouldSucceedOn` [gql|
|
||||||
mutation auth($username: String!, $password: String!){
|
mutation auth($username: String!, $password: String!){
|
||||||
@ -46,6 +71,13 @@ spec = describe "Parser" $ do
|
|||||||
test(username: """username""", password: """password""")
|
test(username: """username""", password: """password""")
|
||||||
}|]
|
}|]
|
||||||
|
|
||||||
|
it "accepts any arguments" $ mapSize (const 10) $ property $ \xs ->
|
||||||
|
let
|
||||||
|
query' :: Text
|
||||||
|
arguments = map printArgument $ getNonEmpty (xs :: NonEmptyList (AnyArgument AnyValue))
|
||||||
|
query' = "query(" <> Text.intercalate ", " arguments <> ")" in
|
||||||
|
parse document "" `shouldSucceedOn` ("{ " <> query' <> " }")
|
||||||
|
|
||||||
it "parses minimal schema definition" $
|
it "parses minimal schema definition" $
|
||||||
parse document "" `shouldSucceedOn` [gql|schema { query: Query }|]
|
parse document "" `shouldSucceedOn` [gql|schema { query: Query }|]
|
||||||
|
|
||||||
@ -95,16 +127,6 @@ spec = describe "Parser" $ do
|
|||||||
}
|
}
|
||||||
|]
|
|]
|
||||||
|
|
||||||
it "parses minimal enum type definition" $
|
|
||||||
parse document "" `shouldSucceedOn` [gql|
|
|
||||||
enum Direction {
|
|
||||||
NORTH
|
|
||||||
EAST
|
|
||||||
SOUTH
|
|
||||||
WEST
|
|
||||||
}
|
|
||||||
|]
|
|
||||||
|
|
||||||
it "parses minimal input object type definition" $
|
it "parses minimal input object type definition" $
|
||||||
parse document "" `shouldSucceedOn` [gql|
|
parse document "" `shouldSucceedOn` [gql|
|
||||||
input Point2D {
|
input Point2D {
|
||||||
@ -202,6 +224,13 @@ spec = describe "Parser" $ do
|
|||||||
}
|
}
|
||||||
|]
|
|]
|
||||||
|
|
||||||
|
it "rejects empty selection set" $
|
||||||
|
parse document "" `shouldFailOn` [gql|
|
||||||
|
query {
|
||||||
|
innerField {}
|
||||||
|
}
|
||||||
|
|]
|
||||||
|
|
||||||
it "parses documents beginning with a comment" $
|
it "parses documents beginning with a comment" $
|
||||||
parse document "" `shouldSucceedOn` [gql|
|
parse document "" `shouldSucceedOn` [gql|
|
||||||
"""
|
"""
|
||||||
|
@ -7,9 +7,9 @@ module Language.GraphQL.ErrorSpec
|
|||||||
( spec
|
( spec
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import qualified Data.Aeson as Aeson
|
|
||||||
import Data.List.NonEmpty (NonEmpty (..))
|
import Data.List.NonEmpty (NonEmpty (..))
|
||||||
import Language.GraphQL.Error
|
import Language.GraphQL.Error
|
||||||
|
import qualified Language.GraphQL.Type as Type
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
( Spec
|
( Spec
|
||||||
, describe
|
, describe
|
||||||
@ -31,6 +31,6 @@ spec = describe "parseError" $
|
|||||||
, pstateTabWidth = mkPos 1
|
, pstateTabWidth = mkPos 1
|
||||||
, pstateLinePrefix = ""
|
, pstateLinePrefix = ""
|
||||||
}
|
}
|
||||||
Response Aeson.Null actual <-
|
Response Type.Null actual <-
|
||||||
parseError (ParseErrorBundle parseErrors posState)
|
parseError (ParseErrorBundle parseErrors posState)
|
||||||
length actual `shouldBe` 1
|
length actual `shouldBe` 1
|
||||||
|
@ -7,12 +7,8 @@ module Language.GraphQL.Execute.CoerceSpec
|
|||||||
( spec
|
( spec
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Aeson as Aeson ((.=))
|
|
||||||
import qualified Data.Aeson as Aeson
|
|
||||||
import qualified Data.Aeson.Types as Aeson
|
|
||||||
import qualified Data.HashMap.Strict as HashMap
|
import qualified Data.HashMap.Strict as HashMap
|
||||||
import Data.Maybe (isNothing)
|
import Data.Maybe (isNothing)
|
||||||
import Data.Scientific (scientific)
|
|
||||||
import qualified Language.GraphQL.Execute.Coerce as Coerce
|
import qualified Language.GraphQL.Execute.Coerce as Coerce
|
||||||
import Language.GraphQL.Type
|
import Language.GraphQL.Type
|
||||||
import qualified Language.GraphQL.Type.In as In
|
import qualified Language.GraphQL.Type.In as In
|
||||||
@ -27,81 +23,11 @@ direction = EnumType "Direction" Nothing $ HashMap.fromList
|
|||||||
, ("WEST", EnumValue Nothing)
|
, ("WEST", EnumValue Nothing)
|
||||||
]
|
]
|
||||||
|
|
||||||
singletonInputObject :: In.Type
|
|
||||||
singletonInputObject = In.NamedInputObjectType type'
|
|
||||||
where
|
|
||||||
type' = In.InputObjectType "ObjectName" Nothing inputFields
|
|
||||||
inputFields = HashMap.singleton "field" field
|
|
||||||
field = In.InputField Nothing (In.NamedScalarType string) Nothing
|
|
||||||
|
|
||||||
namedIdType :: In.Type
|
namedIdType :: In.Type
|
||||||
namedIdType = In.NamedScalarType id
|
namedIdType = In.NamedScalarType id
|
||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec = do
|
spec =
|
||||||
describe "VariableValue Aeson" $ do
|
|
||||||
it "coerces strings" $
|
|
||||||
let expected = Just (String "asdf")
|
|
||||||
actual = Coerce.coerceVariableValue
|
|
||||||
(In.NamedScalarType string) (Aeson.String "asdf")
|
|
||||||
in actual `shouldBe` expected
|
|
||||||
it "coerces non-null strings" $
|
|
||||||
let expected = Just (String "asdf")
|
|
||||||
actual = Coerce.coerceVariableValue
|
|
||||||
(In.NonNullScalarType string) (Aeson.String "asdf")
|
|
||||||
in actual `shouldBe` expected
|
|
||||||
it "coerces booleans" $
|
|
||||||
let expected = Just (Boolean True)
|
|
||||||
actual = Coerce.coerceVariableValue
|
|
||||||
(In.NamedScalarType boolean) (Aeson.Bool True)
|
|
||||||
in actual `shouldBe` expected
|
|
||||||
it "coerces zero to an integer" $
|
|
||||||
let expected = Just (Int 0)
|
|
||||||
actual = Coerce.coerceVariableValue
|
|
||||||
(In.NamedScalarType int) (Aeson.Number 0)
|
|
||||||
in actual `shouldBe` expected
|
|
||||||
it "rejects fractional if an integer is expected" $
|
|
||||||
let actual = Coerce.coerceVariableValue
|
|
||||||
(In.NamedScalarType int) (Aeson.Number $ scientific 14 (-1))
|
|
||||||
in actual `shouldSatisfy` isNothing
|
|
||||||
it "coerces float numbers" $
|
|
||||||
let expected = Just (Float 1.4)
|
|
||||||
actual = Coerce.coerceVariableValue
|
|
||||||
(In.NamedScalarType float) (Aeson.Number $ scientific 14 (-1))
|
|
||||||
in actual `shouldBe` expected
|
|
||||||
it "coerces IDs" $
|
|
||||||
let expected = Just (String "1234")
|
|
||||||
json = Aeson.String "1234"
|
|
||||||
actual = Coerce.coerceVariableValue namedIdType json
|
|
||||||
in actual `shouldBe` expected
|
|
||||||
it "coerces input objects" $
|
|
||||||
let actual = Coerce.coerceVariableValue singletonInputObject
|
|
||||||
$ Aeson.object ["field" .= ("asdf" :: Aeson.Value)]
|
|
||||||
expected = Just $ Object $ HashMap.singleton "field" "asdf"
|
|
||||||
in actual `shouldBe` expected
|
|
||||||
it "skips the field if it is missing in the variables" $
|
|
||||||
let actual = Coerce.coerceVariableValue
|
|
||||||
singletonInputObject Aeson.emptyObject
|
|
||||||
expected = Just $ Object HashMap.empty
|
|
||||||
in actual `shouldBe` expected
|
|
||||||
it "fails if input object value contains extra fields" $
|
|
||||||
let actual = Coerce.coerceVariableValue singletonInputObject
|
|
||||||
$ Aeson.object variableFields
|
|
||||||
variableFields =
|
|
||||||
[ "field" .= ("asdf" :: Aeson.Value)
|
|
||||||
, "extra" .= ("qwer" :: Aeson.Value)
|
|
||||||
]
|
|
||||||
in actual `shouldSatisfy` isNothing
|
|
||||||
it "preserves null" $
|
|
||||||
let actual = Coerce.coerceVariableValue namedIdType Aeson.Null
|
|
||||||
in actual `shouldBe` Just Null
|
|
||||||
it "preserves list order" $
|
|
||||||
let list = Aeson.toJSONList ["asdf" :: Aeson.Value, "qwer"]
|
|
||||||
listType = (In.ListType $ In.NamedScalarType string)
|
|
||||||
actual = Coerce.coerceVariableValue listType list
|
|
||||||
expected = Just $ List [String "asdf", String "qwer"]
|
|
||||||
in actual `shouldBe` expected
|
|
||||||
|
|
||||||
describe "coerceInputLiteral" $ do
|
describe "coerceInputLiteral" $ do
|
||||||
it "coerces enums" $
|
it "coerces enums" $
|
||||||
let expected = Just (Enum "NORTH")
|
let expected = Just (Enum "NORTH")
|
||||||
|
@ -2,17 +2,19 @@
|
|||||||
v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||||
obtain one at https://mozilla.org/MPL/2.0/. -}
|
obtain one at https://mozilla.org/MPL/2.0/. -}
|
||||||
|
|
||||||
|
{-# LANGUAGE DuplicateRecordFields #-}
|
||||||
|
{-# LANGUAGE LambdaCase #-}
|
||||||
|
{-# LANGUAGE NamedFieldPuns #-}
|
||||||
|
{-# LANGUAGE RecordWildCards #-}
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
{-# LANGUAGE QuasiQuotes #-}
|
{-# LANGUAGE QuasiQuotes #-}
|
||||||
|
|
||||||
module Language.GraphQL.ExecuteSpec
|
module Language.GraphQL.ExecuteSpec
|
||||||
( spec
|
( spec
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Exception (Exception(..), SomeException)
|
import Control.Exception (Exception(..))
|
||||||
import Control.Monad.Catch (throwM)
|
import Control.Monad.Catch (throwM)
|
||||||
import Data.Aeson ((.=))
|
|
||||||
import qualified Data.Aeson as Aeson
|
|
||||||
import Data.Aeson.Types (emptyObject)
|
|
||||||
import Data.Conduit
|
import Data.Conduit
|
||||||
import Data.HashMap.Strict (HashMap)
|
import Data.HashMap.Strict (HashMap)
|
||||||
import qualified Data.HashMap.Strict as HashMap
|
import qualified Data.HashMap.Strict as HashMap
|
||||||
@ -23,12 +25,23 @@ import Language.GraphQL.Error
|
|||||||
import Language.GraphQL.Execute (execute)
|
import Language.GraphQL.Execute (execute)
|
||||||
import Language.GraphQL.TH
|
import Language.GraphQL.TH
|
||||||
import qualified Language.GraphQL.Type.Schema as Schema
|
import qualified Language.GraphQL.Type.Schema as Schema
|
||||||
|
import qualified Language.GraphQL.Type as Type
|
||||||
import Language.GraphQL.Type
|
import Language.GraphQL.Type
|
||||||
import qualified Language.GraphQL.Type.In as In
|
import qualified Language.GraphQL.Type.In as In
|
||||||
import qualified Language.GraphQL.Type.Out as Out
|
import qualified Language.GraphQL.Type.Out as Out
|
||||||
import Prelude hiding (id)
|
import Prelude hiding (id)
|
||||||
import Test.Hspec (Spec, context, describe, it, shouldBe)
|
import Test.Hspec (Spec, context, describe, it, shouldBe)
|
||||||
import Text.Megaparsec (parse)
|
import Text.Megaparsec (parse, errorBundlePretty)
|
||||||
|
import Schemas.HeroSchema (heroSchema)
|
||||||
|
import Data.Maybe (fromJust)
|
||||||
|
import qualified Data.Sequence as Seq
|
||||||
|
import Data.Text (Text)
|
||||||
|
import qualified Data.Text as Text
|
||||||
|
import Test.Hspec.Expectations
|
||||||
|
( Expectation
|
||||||
|
, expectationFailure
|
||||||
|
)
|
||||||
|
import Data.Either (fromRight)
|
||||||
|
|
||||||
data PhilosopherException = PhilosopherException
|
data PhilosopherException = PhilosopherException
|
||||||
deriving Show
|
deriving Show
|
||||||
@ -39,7 +52,7 @@ instance Exception PhilosopherException where
|
|||||||
ResolverException resolverException <- fromException e
|
ResolverException resolverException <- fromException e
|
||||||
cast resolverException
|
cast resolverException
|
||||||
|
|
||||||
philosopherSchema :: Schema (Either SomeException)
|
philosopherSchema :: Schema IO
|
||||||
philosopherSchema =
|
philosopherSchema =
|
||||||
schemaWithTypes Nothing queryType Nothing subscriptionRoot extraTypes mempty
|
schemaWithTypes Nothing queryType Nothing subscriptionRoot extraTypes mempty
|
||||||
where
|
where
|
||||||
@ -49,7 +62,7 @@ philosopherSchema =
|
|||||||
, Schema.ObjectType bookCollectionType
|
, Schema.ObjectType bookCollectionType
|
||||||
]
|
]
|
||||||
|
|
||||||
queryType :: Out.ObjectType (Either SomeException)
|
queryType :: Out.ObjectType IO
|
||||||
queryType = Out.ObjectType "Query" Nothing []
|
queryType = Out.ObjectType "Query" Nothing []
|
||||||
$ HashMap.fromList
|
$ HashMap.fromList
|
||||||
[ ("philosopher", ValueResolver philosopherField philosopherResolver)
|
[ ("philosopher", ValueResolver philosopherField philosopherResolver)
|
||||||
@ -65,14 +78,14 @@ queryType = Out.ObjectType "Query" Nothing []
|
|||||||
genresField =
|
genresField =
|
||||||
let fieldType = Out.ListType $ Out.NonNullScalarType string
|
let fieldType = Out.ListType $ Out.NonNullScalarType string
|
||||||
in Out.Field Nothing fieldType HashMap.empty
|
in Out.Field Nothing fieldType HashMap.empty
|
||||||
genresResolver :: Resolve (Either SomeException)
|
genresResolver :: Resolve IO
|
||||||
genresResolver = throwM PhilosopherException
|
genresResolver = throwM PhilosopherException
|
||||||
countField =
|
countField =
|
||||||
let fieldType = Out.NonNullScalarType int
|
let fieldType = Out.NonNullScalarType int
|
||||||
in Out.Field Nothing fieldType HashMap.empty
|
in Out.Field Nothing fieldType HashMap.empty
|
||||||
countResolver = pure ""
|
countResolver = pure ""
|
||||||
|
|
||||||
musicType :: Out.ObjectType (Either SomeException)
|
musicType :: Out.ObjectType IO
|
||||||
musicType = Out.ObjectType "Music" Nothing []
|
musicType = Out.ObjectType "Music" Nothing []
|
||||||
$ HashMap.fromList resolvers
|
$ HashMap.fromList resolvers
|
||||||
where
|
where
|
||||||
@ -82,7 +95,7 @@ musicType = Out.ObjectType "Music" Nothing []
|
|||||||
instrumentResolver = pure $ String "piano"
|
instrumentResolver = pure $ String "piano"
|
||||||
instrumentField = Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty
|
instrumentField = Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty
|
||||||
|
|
||||||
poetryType :: Out.ObjectType (Either SomeException)
|
poetryType :: Out.ObjectType IO
|
||||||
poetryType = Out.ObjectType "Poetry" Nothing []
|
poetryType = Out.ObjectType "Poetry" Nothing []
|
||||||
$ HashMap.fromList resolvers
|
$ HashMap.fromList resolvers
|
||||||
where
|
where
|
||||||
@ -92,10 +105,10 @@ poetryType = Out.ObjectType "Poetry" Nothing []
|
|||||||
genreResolver = pure $ String "Futurism"
|
genreResolver = pure $ String "Futurism"
|
||||||
genreField = Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty
|
genreField = Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty
|
||||||
|
|
||||||
interestType :: Out.UnionType (Either SomeException)
|
interestType :: Out.UnionType IO
|
||||||
interestType = Out.UnionType "Interest" Nothing [musicType, poetryType]
|
interestType = Out.UnionType "Interest" Nothing [musicType, poetryType]
|
||||||
|
|
||||||
philosopherType :: Out.ObjectType (Either SomeException)
|
philosopherType :: Out.ObjectType IO
|
||||||
philosopherType = Out.ObjectType "Philosopher" Nothing []
|
philosopherType = Out.ObjectType "Philosopher" Nothing []
|
||||||
$ HashMap.fromList resolvers
|
$ HashMap.fromList resolvers
|
||||||
where
|
where
|
||||||
@ -136,14 +149,14 @@ philosopherType = Out.ObjectType "Philosopher" Nothing []
|
|||||||
= Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty
|
= Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty
|
||||||
firstLanguageResolver = pure Null
|
firstLanguageResolver = pure Null
|
||||||
|
|
||||||
workType :: Out.InterfaceType (Either SomeException)
|
workType :: Out.InterfaceType IO
|
||||||
workType = Out.InterfaceType "Work" Nothing []
|
workType = Out.InterfaceType "Work" Nothing []
|
||||||
$ HashMap.fromList fields
|
$ HashMap.fromList fields
|
||||||
where
|
where
|
||||||
fields = [("title", titleField)]
|
fields = [("title", titleField)]
|
||||||
titleField = Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty
|
titleField = Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty
|
||||||
|
|
||||||
bookType :: Out.ObjectType (Either SomeException)
|
bookType :: Out.ObjectType IO
|
||||||
bookType = Out.ObjectType "Book" Nothing [workType]
|
bookType = Out.ObjectType "Book" Nothing [workType]
|
||||||
$ HashMap.fromList resolvers
|
$ HashMap.fromList resolvers
|
||||||
where
|
where
|
||||||
@ -153,7 +166,7 @@ bookType = Out.ObjectType "Book" Nothing [workType]
|
|||||||
titleField = Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty
|
titleField = Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty
|
||||||
titleResolver = pure "Also sprach Zarathustra: Ein Buch für Alle und Keinen"
|
titleResolver = pure "Also sprach Zarathustra: Ein Buch für Alle und Keinen"
|
||||||
|
|
||||||
bookCollectionType :: Out.ObjectType (Either SomeException)
|
bookCollectionType :: Out.ObjectType IO
|
||||||
bookCollectionType = Out.ObjectType "Book" Nothing [workType]
|
bookCollectionType = Out.ObjectType "Book" Nothing [workType]
|
||||||
$ HashMap.fromList resolvers
|
$ HashMap.fromList resolvers
|
||||||
where
|
where
|
||||||
@ -163,7 +176,7 @@ bookCollectionType = Out.ObjectType "Book" Nothing [workType]
|
|||||||
titleField = Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty
|
titleField = Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty
|
||||||
titleResolver = pure "The Three Critiques"
|
titleResolver = pure "The Three Critiques"
|
||||||
|
|
||||||
subscriptionType :: Out.ObjectType (Either SomeException)
|
subscriptionType :: Out.ObjectType IO
|
||||||
subscriptionType = Out.ObjectType "Subscription" Nothing []
|
subscriptionType = Out.ObjectType "Subscription" Nothing []
|
||||||
$ HashMap.singleton "newQuote"
|
$ HashMap.singleton "newQuote"
|
||||||
$ EventStreamResolver quoteField (pure $ Object mempty)
|
$ EventStreamResolver quoteField (pure $ Object mempty)
|
||||||
@ -172,7 +185,7 @@ subscriptionType = Out.ObjectType "Subscription" Nothing []
|
|||||||
quoteField =
|
quoteField =
|
||||||
Out.Field Nothing (Out.NonNullObjectType quoteType) HashMap.empty
|
Out.Field Nothing (Out.NonNullObjectType quoteType) HashMap.empty
|
||||||
|
|
||||||
quoteType :: Out.ObjectType (Either SomeException)
|
quoteType :: Out.ObjectType IO
|
||||||
quoteType = Out.ObjectType "Quote" Nothing []
|
quoteType = Out.ObjectType "Quote" Nothing []
|
||||||
$ HashMap.singleton "quote"
|
$ HashMap.singleton "quote"
|
||||||
$ ValueResolver quoteField
|
$ ValueResolver quoteField
|
||||||
@ -181,7 +194,7 @@ quoteType = Out.ObjectType "Quote" Nothing []
|
|||||||
quoteField =
|
quoteField =
|
||||||
Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty
|
Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty
|
||||||
|
|
||||||
schoolType :: EnumType
|
schoolType :: Type.EnumType
|
||||||
schoolType = EnumType "School" Nothing $ HashMap.fromList
|
schoolType = EnumType "School" Nothing $ HashMap.fromList
|
||||||
[ ("NOMINALISM", EnumValue Nothing)
|
[ ("NOMINALISM", EnumValue Nothing)
|
||||||
, ("REALISM", EnumValue Nothing)
|
, ("REALISM", EnumValue Nothing)
|
||||||
@ -189,12 +202,48 @@ schoolType = EnumType "School" Nothing $ HashMap.fromList
|
|||||||
]
|
]
|
||||||
|
|
||||||
type EitherStreamOrValue = Either
|
type EitherStreamOrValue = Either
|
||||||
(ResponseEventStream (Either SomeException) Aeson.Value)
|
(ResponseEventStream IO Type.Value)
|
||||||
(Response Aeson.Value)
|
(Response Type.Value)
|
||||||
|
|
||||||
execute' :: Document -> Either SomeException EitherStreamOrValue
|
-- Asserts that a query resolves to a value.
|
||||||
execute' =
|
shouldResolveTo :: Text.Text -> Response Type.Value -> Expectation
|
||||||
execute philosopherSchema Nothing (mempty :: HashMap Name Aeson.Value)
|
shouldResolveTo querySource expected =
|
||||||
|
case parse document "" querySource of
|
||||||
|
(Right parsedDocument) ->
|
||||||
|
execute philosopherSchema Nothing (mempty :: HashMap Name Type.Value) parsedDocument >>= go
|
||||||
|
(Left errorBundle) -> expectationFailure $ errorBundlePretty errorBundle
|
||||||
|
where
|
||||||
|
go = \case
|
||||||
|
Right result -> shouldBe result expected
|
||||||
|
Left _ -> expectationFailure
|
||||||
|
"the query is expected to resolve to a value, but it resolved to an event stream"
|
||||||
|
|
||||||
|
-- Asserts that the executor produces an error that starts with a string.
|
||||||
|
shouldContainError :: Either (ResponseEventStream IO Type.Value) (Response Type.Value)
|
||||||
|
-> Text
|
||||||
|
-> Expectation
|
||||||
|
shouldContainError streamOrValue expected =
|
||||||
|
case streamOrValue of
|
||||||
|
Right response -> respond response
|
||||||
|
Left _ -> expectationFailure
|
||||||
|
"the query is expected to resolve to a value, but it resolved to an event stream"
|
||||||
|
where
|
||||||
|
startsWith :: Text.Text -> Text.Text -> Bool
|
||||||
|
startsWith xs ys = Text.take (Text.length ys) xs == ys
|
||||||
|
respond :: Response Type.Value -> Expectation
|
||||||
|
respond Response{ errors }
|
||||||
|
| any ((`startsWith` expected) . message) errors = pure ()
|
||||||
|
| otherwise = expectationFailure
|
||||||
|
"the query is expected to execute with errors, but the response doesn't contain any errors"
|
||||||
|
|
||||||
|
parseAndExecute :: Schema IO
|
||||||
|
-> Maybe Text
|
||||||
|
-> HashMap Name Type.Value
|
||||||
|
-> Text
|
||||||
|
-> IO (Either (ResponseEventStream IO Type.Value) (Response Type.Value))
|
||||||
|
parseAndExecute schema' operation variables
|
||||||
|
= either (pure . parseError) (execute schema' operation variables)
|
||||||
|
. parse document ""
|
||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec =
|
spec =
|
||||||
@ -209,38 +258,33 @@ spec =
|
|||||||
...cyclicFragment
|
...cyclicFragment
|
||||||
}
|
}
|
||||||
|]
|
|]
|
||||||
expected = Response emptyObject mempty
|
expected = Response (Object mempty) mempty
|
||||||
Right (Right actual) = either (pure . parseError) execute'
|
in sourceQuery `shouldResolveTo` expected
|
||||||
$ parse document "" sourceQuery
|
|
||||||
in actual `shouldBe` expected
|
|
||||||
|
|
||||||
context "Query" $ do
|
context "Query" $ do
|
||||||
it "skips unknown fields" $
|
it "skips unknown fields" $
|
||||||
let data'' = Aeson.object
|
let data'' = Object
|
||||||
[ "philosopher" .= Aeson.object
|
$ HashMap.singleton "philosopher"
|
||||||
[ "firstName" .= ("Friedrich" :: String)
|
$ Object
|
||||||
]
|
$ HashMap.singleton "firstName"
|
||||||
]
|
$ String "Friedrich"
|
||||||
expected = Response data'' mempty
|
expected = Response data'' mempty
|
||||||
Right (Right actual) = either (pure . parseError) execute'
|
sourceQuery = "{ philosopher { firstName surname } }"
|
||||||
$ parse document "" "{ philosopher { firstName surname } }"
|
in sourceQuery `shouldResolveTo` expected
|
||||||
in actual `shouldBe` expected
|
|
||||||
it "merges selections" $
|
it "merges selections" $
|
||||||
let data'' = Aeson.object
|
let data'' = Object
|
||||||
[ "philosopher" .= Aeson.object
|
$ HashMap.singleton "philosopher"
|
||||||
[ "firstName" .= ("Friedrich" :: String)
|
$ Object
|
||||||
, "lastName" .= ("Nietzsche" :: String)
|
$ HashMap.fromList
|
||||||
]
|
[ ("firstName", String "Friedrich")
|
||||||
|
, ("lastName", String "Nietzsche")
|
||||||
]
|
]
|
||||||
expected = Response data'' mempty
|
expected = Response data'' mempty
|
||||||
Right (Right actual) = either (pure . parseError) execute'
|
sourceQuery = "{ philosopher { firstName } philosopher { lastName } }"
|
||||||
$ parse document "" "{ philosopher { firstName } philosopher { lastName } }"
|
in sourceQuery `shouldResolveTo` expected
|
||||||
in actual `shouldBe` expected
|
|
||||||
|
|
||||||
it "errors on invalid output enum values" $
|
it "errors on invalid output enum values" $
|
||||||
let data'' = Aeson.object
|
let data'' = Object $ HashMap.singleton "philosopher" Null
|
||||||
[ "philosopher" .= Aeson.Null
|
|
||||||
]
|
|
||||||
executionErrors = pure $ Error
|
executionErrors = pure $ Error
|
||||||
{ message =
|
{ message =
|
||||||
"Value completion error. Expected type !School, found: EXISTENTIALISM."
|
"Value completion error. Expected type !School, found: EXISTENTIALISM."
|
||||||
@ -248,14 +292,11 @@ spec =
|
|||||||
, path = [Segment "philosopher", Segment "school"]
|
, path = [Segment "philosopher", Segment "school"]
|
||||||
}
|
}
|
||||||
expected = Response data'' executionErrors
|
expected = Response data'' executionErrors
|
||||||
Right (Right actual) = either (pure . parseError) execute'
|
sourceQuery = "{ philosopher { school } }"
|
||||||
$ parse document "" "{ philosopher { school } }"
|
in sourceQuery `shouldResolveTo` expected
|
||||||
in actual `shouldBe` expected
|
|
||||||
|
|
||||||
it "gives location information for non-null unions" $
|
it "gives location information for non-null unions" $
|
||||||
let data'' = Aeson.object
|
let data'' = Object $ HashMap.singleton "philosopher" Null
|
||||||
[ "philosopher" .= Aeson.Null
|
|
||||||
]
|
|
||||||
executionErrors = pure $ Error
|
executionErrors = pure $ Error
|
||||||
{ message =
|
{ message =
|
||||||
"Value completion error. Expected type !Interest, found: { instrument: \"piano\" }."
|
"Value completion error. Expected type !Interest, found: { instrument: \"piano\" }."
|
||||||
@ -263,14 +304,11 @@ spec =
|
|||||||
, path = [Segment "philosopher", Segment "interest"]
|
, path = [Segment "philosopher", Segment "interest"]
|
||||||
}
|
}
|
||||||
expected = Response data'' executionErrors
|
expected = Response data'' executionErrors
|
||||||
Right (Right actual) = either (pure . parseError) execute'
|
sourceQuery = "{ philosopher { interest } }"
|
||||||
$ parse document "" "{ philosopher { interest } }"
|
in sourceQuery `shouldResolveTo` expected
|
||||||
in actual `shouldBe` expected
|
|
||||||
|
|
||||||
it "gives location information for invalid interfaces" $
|
it "gives location information for invalid interfaces" $
|
||||||
let data'' = Aeson.object
|
let data'' = Object $ HashMap.singleton "philosopher" Null
|
||||||
[ "philosopher" .= Aeson.Null
|
|
||||||
]
|
|
||||||
executionErrors = pure $ Error
|
executionErrors = pure $ Error
|
||||||
{ message
|
{ message
|
||||||
= "Value completion error. Expected type !Work, found:\
|
= "Value completion error. Expected type !Work, found:\
|
||||||
@ -279,14 +317,11 @@ spec =
|
|||||||
, path = [Segment "philosopher", Segment "majorWork"]
|
, path = [Segment "philosopher", Segment "majorWork"]
|
||||||
}
|
}
|
||||||
expected = Response data'' executionErrors
|
expected = Response data'' executionErrors
|
||||||
Right (Right actual) = either (pure . parseError) execute'
|
sourceQuery = "{ philosopher { majorWork { title } } }"
|
||||||
$ parse document "" "{ philosopher { majorWork { title } } }"
|
in sourceQuery `shouldResolveTo` expected
|
||||||
in actual `shouldBe` expected
|
|
||||||
|
|
||||||
it "gives location information for invalid scalar arguments" $
|
it "gives location information for invalid scalar arguments" $
|
||||||
let data'' = Aeson.object
|
let data'' = Object $ HashMap.singleton "philosopher" Null
|
||||||
[ "philosopher" .= Aeson.Null
|
|
||||||
]
|
|
||||||
executionErrors = pure $ Error
|
executionErrors = pure $ Error
|
||||||
{ message =
|
{ message =
|
||||||
"Argument \"id\" has invalid type. Expected type ID, found: True."
|
"Argument \"id\" has invalid type. Expected type ID, found: True."
|
||||||
@ -294,37 +329,30 @@ spec =
|
|||||||
, path = [Segment "philosopher"]
|
, path = [Segment "philosopher"]
|
||||||
}
|
}
|
||||||
expected = Response data'' executionErrors
|
expected = Response data'' executionErrors
|
||||||
Right (Right actual) = either (pure . parseError) execute'
|
sourceQuery = "{ philosopher(id: true) { lastName } }"
|
||||||
$ parse document "" "{ philosopher(id: true) { lastName } }"
|
in sourceQuery `shouldResolveTo` expected
|
||||||
in actual `shouldBe` expected
|
|
||||||
|
|
||||||
it "gives location information for failed result coercion" $
|
it "gives location information for failed result coercion" $
|
||||||
let data'' = Aeson.object
|
let data'' = Object $ HashMap.singleton "philosopher" Null
|
||||||
[ "philosopher" .= Aeson.Null
|
|
||||||
]
|
|
||||||
executionErrors = pure $ Error
|
executionErrors = pure $ Error
|
||||||
{ message = "Unable to coerce result to !Int."
|
{ message = "Unable to coerce result to !Int."
|
||||||
, locations = [Location 1 26]
|
, locations = [Location 1 26]
|
||||||
, path = [Segment "philosopher", Segment "century"]
|
, path = [Segment "philosopher", Segment "century"]
|
||||||
}
|
}
|
||||||
expected = Response data'' executionErrors
|
expected = Response data'' executionErrors
|
||||||
Right (Right actual) = either (pure . parseError) execute'
|
sourceQuery = "{ philosopher(id: \"1\") { century } }"
|
||||||
$ parse document "" "{ philosopher(id: \"1\") { century } }"
|
in sourceQuery `shouldResolveTo` expected
|
||||||
in actual `shouldBe` expected
|
|
||||||
|
|
||||||
it "gives location information for failed result coercion" $
|
it "gives location information for failed result coercion" $
|
||||||
let data'' = Aeson.object
|
let data'' = Object $ HashMap.singleton "genres" Null
|
||||||
[ "genres" .= Aeson.Null
|
|
||||||
]
|
|
||||||
executionErrors = pure $ Error
|
executionErrors = pure $ Error
|
||||||
{ message = "PhilosopherException"
|
{ message = "PhilosopherException"
|
||||||
, locations = [Location 1 3]
|
, locations = [Location 1 3]
|
||||||
, path = [Segment "genres"]
|
, path = [Segment "genres"]
|
||||||
}
|
}
|
||||||
expected = Response data'' executionErrors
|
expected = Response data'' executionErrors
|
||||||
Right (Right actual) = either (pure . parseError) execute'
|
sourceQuery = "{ genres }"
|
||||||
$ parse document "" "{ genres }"
|
in sourceQuery `shouldResolveTo` expected
|
||||||
in actual `shouldBe` expected
|
|
||||||
|
|
||||||
it "sets data to null if a root field isn't nullable" $
|
it "sets data to null if a root field isn't nullable" $
|
||||||
let executionErrors = pure $ Error
|
let executionErrors = pure $ Error
|
||||||
@ -332,34 +360,81 @@ spec =
|
|||||||
, locations = [Location 1 3]
|
, locations = [Location 1 3]
|
||||||
, path = [Segment "count"]
|
, path = [Segment "count"]
|
||||||
}
|
}
|
||||||
expected = Response Aeson.Null executionErrors
|
expected = Response Null executionErrors
|
||||||
Right (Right actual) = either (pure . parseError) execute'
|
sourceQuery = "{ count }"
|
||||||
$ parse document "" "{ count }"
|
in sourceQuery `shouldResolveTo` expected
|
||||||
in actual `shouldBe` expected
|
|
||||||
|
|
||||||
it "detects nullability errors" $
|
it "detects nullability errors" $
|
||||||
let data'' = Aeson.object
|
let data'' = Object $ HashMap.singleton "philosopher" Null
|
||||||
[ "philosopher" .= Aeson.Null
|
|
||||||
]
|
|
||||||
executionErrors = pure $ Error
|
executionErrors = pure $ Error
|
||||||
{ message = "Value completion error. Expected type !String, found: null."
|
{ message = "Value completion error. Expected type !String, found: null."
|
||||||
, locations = [Location 1 26]
|
, locations = [Location 1 26]
|
||||||
, path = [Segment "philosopher", Segment "firstLanguage"]
|
, path = [Segment "philosopher", Segment "firstLanguage"]
|
||||||
}
|
}
|
||||||
expected = Response data'' executionErrors
|
expected = Response data'' executionErrors
|
||||||
Right (Right actual) = either (pure . parseError) execute'
|
sourceQuery = "{ philosopher(id: \"1\") { firstLanguage } }"
|
||||||
$ parse document "" "{ philosopher(id: \"1\") { firstLanguage } }"
|
in sourceQuery `shouldResolveTo` expected
|
||||||
in actual `shouldBe` expected
|
|
||||||
|
context "queryError" $ do
|
||||||
|
let namedQuery name = "query " <> name <> " { philosopher(id: \"1\") { interest } }"
|
||||||
|
twoQueries = namedQuery "A" <> " " <> namedQuery "B"
|
||||||
|
|
||||||
|
it "throws operation name is required error" $ do
|
||||||
|
let expectedErrorMessage = "Operation name is required"
|
||||||
|
actual <- parseAndExecute philosopherSchema Nothing mempty twoQueries
|
||||||
|
actual `shouldContainError` expectedErrorMessage
|
||||||
|
|
||||||
|
it "throws operation not found error" $ do
|
||||||
|
let expectedErrorMessage = "Operation \"C\" is not found"
|
||||||
|
actual <- parseAndExecute philosopherSchema (Just "C") mempty twoQueries
|
||||||
|
actual `shouldContainError` expectedErrorMessage
|
||||||
|
|
||||||
|
it "throws variable coercion error" $ do
|
||||||
|
let data'' = Null
|
||||||
|
executionErrors = pure $ Error
|
||||||
|
{ message = "Failed to coerce the variable $id: String."
|
||||||
|
, locations =[Location 1 7]
|
||||||
|
, path = []
|
||||||
|
}
|
||||||
|
expected = Response data'' executionErrors
|
||||||
|
executeWithVars = execute philosopherSchema Nothing (HashMap.singleton "id" (Type.Int 1))
|
||||||
|
Right actual <- either (pure . parseError) executeWithVars
|
||||||
|
$ parse document "" "query($id: String) { philosopher(id: \"1\") { firstLanguage } }"
|
||||||
|
actual `shouldBe` expected
|
||||||
|
|
||||||
|
it "throws variable unkown input type error" $
|
||||||
|
let data'' = Null
|
||||||
|
executionErrors = pure $ Error
|
||||||
|
{ message = "Variable $id has unknown type Cat."
|
||||||
|
, locations =[Location 1 7]
|
||||||
|
, path = []
|
||||||
|
}
|
||||||
|
expected = Response data'' executionErrors
|
||||||
|
sourceQuery = "query($id: Cat) { philosopher(id: \"1\") { firstLanguage } }"
|
||||||
|
in sourceQuery `shouldResolveTo` expected
|
||||||
|
|
||||||
|
context "Error path" $ do
|
||||||
|
let executeHero :: Document -> IO EitherStreamOrValue
|
||||||
|
executeHero = execute heroSchema Nothing (HashMap.empty :: HashMap Name Type.Value)
|
||||||
|
|
||||||
|
it "at the beggining of the list" $ do
|
||||||
|
Right actual <- either (pure . parseError) executeHero
|
||||||
|
$ parse document "" "{ hero(id: \"1\") { friends { name } } }"
|
||||||
|
let Response _ errors' = actual
|
||||||
|
Error _ _ path' = fromJust $ Seq.lookup 0 errors'
|
||||||
|
expected = [Segment "hero", Segment "friends", Index 0, Segment "name"]
|
||||||
|
in path' `shouldBe` expected
|
||||||
|
|
||||||
context "Subscription" $
|
context "Subscription" $
|
||||||
it "subscribes" $
|
it "subscribes" $ do
|
||||||
let data'' = Aeson.object
|
let data'' = Object
|
||||||
[ "newQuote" .= Aeson.object
|
$ HashMap.singleton "newQuote"
|
||||||
[ "quote" .= ("Naturam expelles furca, tamen usque recurret." :: String)
|
$ Object
|
||||||
]
|
$ HashMap.singleton "quote"
|
||||||
]
|
$ String "Naturam expelles furca, tamen usque recurret."
|
||||||
expected = Response data'' mempty
|
expected = Response data'' mempty
|
||||||
Right (Left stream) = either (pure . parseError) execute'
|
Left stream <- execute philosopherSchema Nothing (mempty :: HashMap Name Type.Value)
|
||||||
|
$ fromRight (error "Parse error")
|
||||||
$ parse document "" "subscription { newQuote { quote } }"
|
$ parse document "" "subscription { newQuote { quote } }"
|
||||||
Right (Just actual) = runConduit $ stream .| await
|
Just actual <- runConduit $ stream .| await
|
||||||
in actual `shouldBe` expected
|
actual `shouldBe` expected
|
||||||
|
70
tests/Schemas/HeroSchema.hs
Normal file
70
tests/Schemas/HeroSchema.hs
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
{- This Source Code Form is subject to the terms of the Mozilla Public License,
|
||||||
|
v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||||
|
obtain one at https://mozilla.org/MPL/2.0/. -}
|
||||||
|
|
||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
|
module Schemas.HeroSchema (heroSchema) where
|
||||||
|
|
||||||
|
import Control.Exception (Exception(..))
|
||||||
|
import Control.Monad.Catch (throwM)
|
||||||
|
import Language.GraphQL.Error (ResolverException (..))
|
||||||
|
import qualified Language.GraphQL.Type.In as In
|
||||||
|
import qualified Language.GraphQL.Type as Type
|
||||||
|
import Language.GraphQL.Type.Schema (schemaWithTypes)
|
||||||
|
import qualified Data.HashMap.Strict as HashMap
|
||||||
|
import Data.Typeable (cast)
|
||||||
|
import qualified Language.GraphQL.Type.Out as Out
|
||||||
|
|
||||||
|
data HeroException = HeroException
|
||||||
|
deriving Show
|
||||||
|
|
||||||
|
instance Exception HeroException where
|
||||||
|
toException = toException. ResolverException
|
||||||
|
fromException e = do
|
||||||
|
ResolverException resolverException <- fromException e
|
||||||
|
cast resolverException
|
||||||
|
|
||||||
|
heroSchema :: Type.Schema IO
|
||||||
|
heroSchema =
|
||||||
|
schemaWithTypes Nothing queryType Nothing Nothing [] mempty
|
||||||
|
|
||||||
|
type ObjectType = Out.ObjectType IO
|
||||||
|
|
||||||
|
queryType :: ObjectType
|
||||||
|
queryType = Out.ObjectType "Query" Nothing []
|
||||||
|
$ HashMap.fromList
|
||||||
|
[ ("hero", Out.ValueResolver heroField heroResolver)
|
||||||
|
]
|
||||||
|
where
|
||||||
|
heroField = Out.Field Nothing (Out.NamedObjectType heroType)
|
||||||
|
$ HashMap.singleton "id"
|
||||||
|
$ In.Argument Nothing (In.NamedScalarType Type.id) Nothing
|
||||||
|
heroResolver = pure $ Type.Object mempty
|
||||||
|
|
||||||
|
stringField :: Out.Field IO
|
||||||
|
stringField = Out.Field Nothing (Out.NonNullScalarType Type.string) HashMap.empty
|
||||||
|
|
||||||
|
heroType :: ObjectType
|
||||||
|
heroType = Out.ObjectType "Hero" Nothing [] $ HashMap.fromList resolvers
|
||||||
|
where
|
||||||
|
resolvers =
|
||||||
|
[ ("id", Out.ValueResolver stringField (pure $ Type.String "4111"))
|
||||||
|
, ("name", Out.ValueResolver stringField (pure $ Type.String "R2D2"))
|
||||||
|
, ("friends", Out.ValueResolver friendsField (pure $ Type.List [luke]))
|
||||||
|
]
|
||||||
|
friendsField = Out.Field Nothing (Out.ListType $ Out.NonNullObjectType lukeType) HashMap.empty
|
||||||
|
-- This list values are ignored because of current realisation (types and resolvers are the same entity)
|
||||||
|
-- The values from lukeType will be used
|
||||||
|
luke = Type.Object $ HashMap.fromList
|
||||||
|
[ ("id", "dfdfdf")
|
||||||
|
, ("name", "dfdfdff")
|
||||||
|
]
|
||||||
|
|
||||||
|
lukeType :: ObjectType
|
||||||
|
lukeType = Out.ObjectType "Luke" Nothing [] $ HashMap.fromList resolvers
|
||||||
|
where
|
||||||
|
resolvers =
|
||||||
|
[ ("id", Out.ValueResolver stringField (pure $ Type.String "1000"))
|
||||||
|
, ("name", Out.ValueResolver stringField (throwM HeroException))
|
||||||
|
]
|
@ -1,92 +0,0 @@
|
|||||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
|
|
||||||
v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
|
||||||
obtain one at https://mozilla.org/MPL/2.0/. -}
|
|
||||||
|
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
|
||||||
{-# LANGUAGE QuasiQuotes #-}
|
|
||||||
module Test.DirectiveSpec
|
|
||||||
( spec
|
|
||||||
) where
|
|
||||||
|
|
||||||
import Data.Aeson (object, (.=))
|
|
||||||
import qualified Data.Aeson as Aeson
|
|
||||||
import qualified Data.HashMap.Strict as HashMap
|
|
||||||
import Language.GraphQL
|
|
||||||
import Language.GraphQL.TH
|
|
||||||
import Language.GraphQL.Type
|
|
||||||
import qualified Language.GraphQL.Type.Out as Out
|
|
||||||
import Test.Hspec (Spec, describe, it)
|
|
||||||
import Test.Hspec.GraphQL
|
|
||||||
|
|
||||||
experimentalResolver :: Schema IO
|
|
||||||
experimentalResolver = schema queryType Nothing Nothing mempty
|
|
||||||
where
|
|
||||||
queryType = Out.ObjectType "Query" Nothing []
|
|
||||||
$ HashMap.singleton "experimentalField"
|
|
||||||
$ Out.ValueResolver (Out.Field Nothing (Out.NamedScalarType int) mempty)
|
|
||||||
$ pure $ Int 5
|
|
||||||
|
|
||||||
emptyObject :: Aeson.Object
|
|
||||||
emptyObject = HashMap.singleton "data" $ object []
|
|
||||||
|
|
||||||
spec :: Spec
|
|
||||||
spec =
|
|
||||||
describe "Directive executor" $ do
|
|
||||||
it "should be able to @skip fields" $ do
|
|
||||||
let sourceQuery = [gql|
|
|
||||||
{
|
|
||||||
experimentalField @skip(if: true)
|
|
||||||
}
|
|
||||||
|]
|
|
||||||
|
|
||||||
actual <- graphql experimentalResolver sourceQuery
|
|
||||||
actual `shouldResolveTo` emptyObject
|
|
||||||
|
|
||||||
it "should not skip fields if @skip is false" $ do
|
|
||||||
let sourceQuery = [gql|
|
|
||||||
{
|
|
||||||
experimentalField @skip(if: false)
|
|
||||||
}
|
|
||||||
|]
|
|
||||||
expected = HashMap.singleton "data"
|
|
||||||
$ object
|
|
||||||
[ "experimentalField" .= (5 :: Int)
|
|
||||||
]
|
|
||||||
actual <- graphql experimentalResolver sourceQuery
|
|
||||||
actual `shouldResolveTo` expected
|
|
||||||
|
|
||||||
it "should skip fields if @include is false" $ do
|
|
||||||
let sourceQuery = [gql|
|
|
||||||
{
|
|
||||||
experimentalField @include(if: false)
|
|
||||||
}
|
|
||||||
|]
|
|
||||||
|
|
||||||
actual <- graphql experimentalResolver sourceQuery
|
|
||||||
actual `shouldResolveTo` emptyObject
|
|
||||||
|
|
||||||
it "should be able to @skip a fragment spread" $ do
|
|
||||||
let sourceQuery = [gql|
|
|
||||||
{
|
|
||||||
...experimentalFragment @skip(if: true)
|
|
||||||
}
|
|
||||||
|
|
||||||
fragment experimentalFragment on Query {
|
|
||||||
experimentalField
|
|
||||||
}
|
|
||||||
|]
|
|
||||||
|
|
||||||
actual <- graphql experimentalResolver sourceQuery
|
|
||||||
actual `shouldResolveTo` emptyObject
|
|
||||||
|
|
||||||
it "should be able to @skip an inline fragment" $ do
|
|
||||||
let sourceQuery = [gql|
|
|
||||||
{
|
|
||||||
... on Query @skip(if: true) {
|
|
||||||
experimentalField
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|]
|
|
||||||
|
|
||||||
actual <- graphql experimentalResolver sourceQuery
|
|
||||||
actual `shouldResolveTo` emptyObject
|
|
@ -1,204 +0,0 @@
|
|||||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
|
|
||||||
v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
|
||||||
obtain one at https://mozilla.org/MPL/2.0/. -}
|
|
||||||
|
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
|
||||||
{-# LANGUAGE QuasiQuotes #-}
|
|
||||||
module Test.FragmentSpec
|
|
||||||
( spec
|
|
||||||
) where
|
|
||||||
|
|
||||||
import Data.Aeson ((.=))
|
|
||||||
import qualified Data.Aeson as Aeson
|
|
||||||
import qualified Data.HashMap.Strict as HashMap
|
|
||||||
import Data.Text (Text)
|
|
||||||
import Language.GraphQL
|
|
||||||
import Language.GraphQL.Type
|
|
||||||
import qualified Language.GraphQL.Type.Out as Out
|
|
||||||
import Language.GraphQL.TH
|
|
||||||
import Test.Hspec (Spec, describe, it)
|
|
||||||
import Test.Hspec.GraphQL
|
|
||||||
|
|
||||||
size :: (Text, Value)
|
|
||||||
size = ("size", String "L")
|
|
||||||
|
|
||||||
circumference :: (Text, Value)
|
|
||||||
circumference = ("circumference", Int 60)
|
|
||||||
|
|
||||||
garment :: Text -> (Text, Value)
|
|
||||||
garment typeName =
|
|
||||||
("garment", Object $ HashMap.fromList
|
|
||||||
[ if typeName == "Hat" then circumference else size
|
|
||||||
, ("__typename", String typeName)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
inlineQuery :: Text
|
|
||||||
inlineQuery = [gql|
|
|
||||||
{
|
|
||||||
garment {
|
|
||||||
... on Hat {
|
|
||||||
circumference
|
|
||||||
}
|
|
||||||
... on Shirt {
|
|
||||||
size
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|]
|
|
||||||
|
|
||||||
shirtType :: Out.ObjectType IO
|
|
||||||
shirtType = Out.ObjectType "Shirt" Nothing [] $ HashMap.fromList
|
|
||||||
[ ("size", sizeFieldType)
|
|
||||||
]
|
|
||||||
|
|
||||||
hatType :: Out.ObjectType IO
|
|
||||||
hatType = Out.ObjectType "Hat" Nothing [] $ HashMap.fromList
|
|
||||||
[ ("size", sizeFieldType)
|
|
||||||
, ("circumference", circumferenceFieldType)
|
|
||||||
]
|
|
||||||
|
|
||||||
circumferenceFieldType :: Out.Resolver IO
|
|
||||||
circumferenceFieldType
|
|
||||||
= Out.ValueResolver (Out.Field Nothing (Out.NamedScalarType int) mempty)
|
|
||||||
$ pure $ snd circumference
|
|
||||||
|
|
||||||
sizeFieldType :: Out.Resolver IO
|
|
||||||
sizeFieldType
|
|
||||||
= Out.ValueResolver (Out.Field Nothing (Out.NamedScalarType string) mempty)
|
|
||||||
$ pure $ snd size
|
|
||||||
|
|
||||||
toSchema :: Text -> (Text, Value) -> Schema IO
|
|
||||||
toSchema t (_, resolve) = schema queryType Nothing Nothing mempty
|
|
||||||
where
|
|
||||||
garmentType = Out.UnionType "Garment" Nothing [hatType, shirtType]
|
|
||||||
typeNameField = Out.Field Nothing (Out.NamedScalarType string) mempty
|
|
||||||
garmentField = Out.Field Nothing (Out.NamedUnionType garmentType) mempty
|
|
||||||
queryType =
|
|
||||||
case t of
|
|
||||||
"circumference" -> hatType
|
|
||||||
"size" -> shirtType
|
|
||||||
_ -> Out.ObjectType "Query" Nothing []
|
|
||||||
$ HashMap.fromList
|
|
||||||
[ ("garment", ValueResolver garmentField (pure resolve))
|
|
||||||
, ("__typename", ValueResolver typeNameField (pure $ String "Shirt"))
|
|
||||||
]
|
|
||||||
|
|
||||||
spec :: Spec
|
|
||||||
spec = do
|
|
||||||
describe "Inline fragment executor" $ do
|
|
||||||
it "chooses the first selection if the type matches" $ do
|
|
||||||
actual <- graphql (toSchema "Hat" $ garment "Hat") inlineQuery
|
|
||||||
let expected = HashMap.singleton "data"
|
|
||||||
$ Aeson.object
|
|
||||||
[ "garment" .= Aeson.object
|
|
||||||
[ "circumference" .= (60 :: Int)
|
|
||||||
]
|
|
||||||
]
|
|
||||||
in actual `shouldResolveTo` expected
|
|
||||||
|
|
||||||
it "chooses the last selection if the type matches" $ do
|
|
||||||
actual <- graphql (toSchema "Shirt" $ garment "Shirt") inlineQuery
|
|
||||||
let expected = HashMap.singleton "data"
|
|
||||||
$ Aeson.object
|
|
||||||
[ "garment" .= Aeson.object
|
|
||||||
[ "size" .= ("L" :: Text)
|
|
||||||
]
|
|
||||||
]
|
|
||||||
in actual `shouldResolveTo` expected
|
|
||||||
|
|
||||||
it "embeds inline fragments without type" $ do
|
|
||||||
let sourceQuery = [gql|
|
|
||||||
{
|
|
||||||
circumference
|
|
||||||
... {
|
|
||||||
size
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|]
|
|
||||||
actual <- graphql (toSchema "circumference" circumference) sourceQuery
|
|
||||||
let expected = HashMap.singleton "data"
|
|
||||||
$ Aeson.object
|
|
||||||
[ "circumference" .= (60 :: Int)
|
|
||||||
, "size" .= ("L" :: Text)
|
|
||||||
]
|
|
||||||
in actual `shouldResolveTo` expected
|
|
||||||
|
|
||||||
it "evaluates fragments on Query" $ do
|
|
||||||
let sourceQuery = [gql|
|
|
||||||
{
|
|
||||||
... {
|
|
||||||
size
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|]
|
|
||||||
in graphql (toSchema "size" size) `shouldResolve` sourceQuery
|
|
||||||
|
|
||||||
describe "Fragment spread executor" $ do
|
|
||||||
it "evaluates fragment spreads" $ do
|
|
||||||
let sourceQuery = [gql|
|
|
||||||
{
|
|
||||||
...circumferenceFragment
|
|
||||||
}
|
|
||||||
|
|
||||||
fragment circumferenceFragment on Hat {
|
|
||||||
circumference
|
|
||||||
}
|
|
||||||
|]
|
|
||||||
|
|
||||||
actual <- graphql (toSchema "circumference" circumference) sourceQuery
|
|
||||||
let expected = HashMap.singleton "data"
|
|
||||||
$ Aeson.object
|
|
||||||
[ "circumference" .= (60 :: Int)
|
|
||||||
]
|
|
||||||
in actual `shouldResolveTo` expected
|
|
||||||
|
|
||||||
it "evaluates nested fragments" $ do
|
|
||||||
let sourceQuery = [gql|
|
|
||||||
{
|
|
||||||
garment {
|
|
||||||
...circumferenceFragment
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fragment circumferenceFragment on Hat {
|
|
||||||
...hatFragment
|
|
||||||
}
|
|
||||||
|
|
||||||
fragment hatFragment on Hat {
|
|
||||||
circumference
|
|
||||||
}
|
|
||||||
|]
|
|
||||||
|
|
||||||
actual <- graphql (toSchema "Hat" $ garment "Hat") sourceQuery
|
|
||||||
let expected = HashMap.singleton "data"
|
|
||||||
$ Aeson.object
|
|
||||||
[ "garment" .= Aeson.object
|
|
||||||
[ "circumference" .= (60 :: Int)
|
|
||||||
]
|
|
||||||
]
|
|
||||||
in actual `shouldResolveTo` expected
|
|
||||||
|
|
||||||
it "considers type condition" $ do
|
|
||||||
let sourceQuery = [gql|
|
|
||||||
{
|
|
||||||
garment {
|
|
||||||
...circumferenceFragment
|
|
||||||
...sizeFragment
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fragment circumferenceFragment on Hat {
|
|
||||||
circumference
|
|
||||||
}
|
|
||||||
fragment sizeFragment on Shirt {
|
|
||||||
size
|
|
||||||
}
|
|
||||||
|]
|
|
||||||
expected = HashMap.singleton "data"
|
|
||||||
$ Aeson.object
|
|
||||||
[ "garment" .= Aeson.object
|
|
||||||
[ "circumference" .= (60 :: Int)
|
|
||||||
]
|
|
||||||
]
|
|
||||||
actual <- graphql (toSchema "Hat" $ garment "Hat") sourceQuery
|
|
||||||
actual `shouldResolveTo` expected
|
|
@ -1,72 +0,0 @@
|
|||||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
|
|
||||||
v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
|
||||||
obtain one at https://mozilla.org/MPL/2.0/. -}
|
|
||||||
|
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
|
||||||
{-# LANGUAGE QuasiQuotes #-}
|
|
||||||
module Test.RootOperationSpec
|
|
||||||
( spec
|
|
||||||
) where
|
|
||||||
|
|
||||||
import Data.Aeson ((.=), object)
|
|
||||||
import qualified Data.HashMap.Strict as HashMap
|
|
||||||
import Language.GraphQL
|
|
||||||
import Test.Hspec (Spec, describe, it)
|
|
||||||
import Language.GraphQL.TH
|
|
||||||
import Language.GraphQL.Type
|
|
||||||
import qualified Language.GraphQL.Type.Out as Out
|
|
||||||
import Test.Hspec.GraphQL
|
|
||||||
|
|
||||||
hatType :: Out.ObjectType IO
|
|
||||||
hatType = Out.ObjectType "Hat" Nothing []
|
|
||||||
$ HashMap.singleton "circumference"
|
|
||||||
$ ValueResolver (Out.Field Nothing (Out.NamedScalarType int) mempty)
|
|
||||||
$ pure $ Int 60
|
|
||||||
|
|
||||||
garmentSchema :: Schema IO
|
|
||||||
garmentSchema = schema queryType (Just mutationType) Nothing mempty
|
|
||||||
where
|
|
||||||
queryType = Out.ObjectType "Query" Nothing [] hatFieldResolver
|
|
||||||
mutationType = Out.ObjectType "Mutation" Nothing [] incrementFieldResolver
|
|
||||||
garment = pure $ Object $ HashMap.fromList
|
|
||||||
[ ("circumference", Int 60)
|
|
||||||
]
|
|
||||||
incrementFieldResolver = HashMap.singleton "incrementCircumference"
|
|
||||||
$ ValueResolver (Out.Field Nothing (Out.NamedScalarType int) mempty)
|
|
||||||
$ pure $ Int 61
|
|
||||||
hatField = Out.Field Nothing (Out.NamedObjectType hatType) mempty
|
|
||||||
hatFieldResolver =
|
|
||||||
HashMap.singleton "garment" $ ValueResolver hatField garment
|
|
||||||
|
|
||||||
spec :: Spec
|
|
||||||
spec =
|
|
||||||
describe "Root operation type" $ do
|
|
||||||
it "returns objects from the root resolvers" $ do
|
|
||||||
let querySource = [gql|
|
|
||||||
{
|
|
||||||
garment {
|
|
||||||
circumference
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|]
|
|
||||||
expected = HashMap.singleton "data"
|
|
||||||
$ object
|
|
||||||
[ "garment" .= object
|
|
||||||
[ "circumference" .= (60 :: Int)
|
|
||||||
]
|
|
||||||
]
|
|
||||||
actual <- graphql garmentSchema querySource
|
|
||||||
actual `shouldResolveTo` expected
|
|
||||||
|
|
||||||
it "chooses Mutation" $ do
|
|
||||||
let querySource = [gql|
|
|
||||||
mutation {
|
|
||||||
incrementCircumference
|
|
||||||
}
|
|
||||||
|]
|
|
||||||
expected = HashMap.singleton "data"
|
|
||||||
$ object
|
|
||||||
[ "incrementCircumference" .= (61 :: Int)
|
|
||||||
]
|
|
||||||
actual <- graphql garmentSchema querySource
|
|
||||||
actual `shouldResolveTo` expected
|
|
Reference in New Issue
Block a user