summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2020-07-11 06:34:10 +0200
committerEugen Wissner <belka@caraus.de>2020-07-11 06:34:10 +0200
commit840e129c4496b4e8145480d2b3c3cb34f505702e (patch)
tree0b79c0f8045d93505d8285ebff327000a6926168 /src
parent04a58be3f86ced396eed26f90643e7c88e7f2b4d (diff)
downloadgraphql-840e129c4496b4e8145480d2b3c3cb34f505702e.tar.gz
Parse subscriptions
Diffstat (limited to 'src')
-rw-r--r--src/Language/GraphQL/AST/Document.hs4
-rw-r--r--src/Language/GraphQL/AST/Encoder.hs17
-rw-r--r--src/Language/GraphQL/AST/Parser.hs3
-rw-r--r--src/Language/GraphQL/Execute.hs4
-rw-r--r--src/Language/GraphQL/Execute/Transform.hs3
5 files changed, 20 insertions, 11 deletions
diff --git a/src/Language/GraphQL/AST/Document.hs b/src/Language/GraphQL/AST/Document.hs
index ed473b7..cd1dbc6 100644
--- a/src/Language/GraphQL/AST/Document.hs
+++ b/src/Language/GraphQL/AST/Document.hs
@@ -99,9 +99,7 @@ data OperationDefinition
-- * mutation - a write operation followed by a fetch.
-- * subscription - a long-lived request that fetches data in response to
-- source events.
---
--- Currently only queries and mutations are supported.
-data OperationType = Query | Mutation deriving (Eq, Show)
+data OperationType = Query | Mutation | Subscription deriving (Eq, Show)
-- ** Selection Sets
diff --git a/src/Language/GraphQL/AST/Encoder.hs b/src/Language/GraphQL/AST/Encoder.hs
index a9f91ec..b55566d 100644
--- a/src/Language/GraphQL/AST/Encoder.hs
+++ b/src/Language/GraphQL/AST/Encoder.hs
@@ -1,5 +1,6 @@
-{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ExplicitForAll #-}
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE LambdaCase #-}
-- | This module defines a minifier and a printer for the @GraphQL@ language.
module Language.GraphQL.AST.Encoder
@@ -65,12 +66,14 @@ definition formatter x
-- | Converts a 'OperationDefinition into a string.
operationDefinition :: Formatter -> OperationDefinition -> Lazy.Text
-operationDefinition formatter (SelectionSet sels)
- = selectionSet formatter sels
-operationDefinition formatter (OperationDefinition Query name vars dirs sels)
- = "query " <> node formatter name vars dirs sels
-operationDefinition formatter (OperationDefinition Mutation name vars dirs sels)
- = "mutation " <> node formatter name vars dirs sels
+operationDefinition formatter = \case
+ SelectionSet sels -> selectionSet formatter sels
+ OperationDefinition Query name vars dirs sels ->
+ "query " <> node formatter name vars dirs sels
+ OperationDefinition Mutation name vars dirs sels ->
+ "mutation " <> node formatter name vars dirs sels
+ OperationDefinition Subscription name vars dirs sels ->
+ "subscription " <> node formatter name vars dirs sels
-- | Converts a Query or Mutation into a string.
node :: Formatter ->
diff --git a/src/Language/GraphQL/AST/Parser.hs b/src/Language/GraphQL/AST/Parser.hs
index 150586e..ea517da 100644
--- a/src/Language/GraphQL/AST/Parser.hs
+++ b/src/Language/GraphQL/AST/Parser.hs
@@ -334,7 +334,8 @@ operationDefinition = SelectionSet <$> selectionSet
operationType :: Parser OperationType
operationType = Query <$ symbol "query"
<|> Mutation <$ symbol "mutation"
- -- <?> Keep default error message
+ <|> Subscription <$ symbol "subscription"
+ <?> "OperationType"
selectionSet :: Parser SelectionSet
selectionSet = braces (NonEmpty.some selection) <?> "SelectionSet"
diff --git a/src/Language/GraphQL/Execute.hs b/src/Language/GraphQL/Execute.hs
index ff1078c..471cd00 100644
--- a/src/Language/GraphQL/Execute.hs
+++ b/src/Language/GraphQL/Execute.hs
@@ -1,3 +1,5 @@
+{-# LANGUAGE OverloadedStrings #-}
+
-- | This module provides functions to execute a @GraphQL@ request.
module Language.GraphQL.Execute
( execute
@@ -42,6 +44,8 @@ executeRequest (Transform.Document types' rootObjectType operation)
executeOperation types' rootObjectType fields
| (Transform.Mutation _ fields) <- operation =
executeOperation types' rootObjectType fields
+ | otherwise =
+ pure $ singleError "This service does not support subscriptions."
-- This is actually executeMutation, but we don't distinguish between queries
-- and mutations yet.
diff --git a/src/Language/GraphQL/Execute/Transform.hs b/src/Language/GraphQL/Execute/Transform.hs
index 30d5130..086af5c 100644
--- a/src/Language/GraphQL/Execute/Transform.hs
+++ b/src/Language/GraphQL/Execute/Transform.hs
@@ -77,6 +77,7 @@ data Selection m
data Operation m
= Query (Maybe Text) (Seq (Selection m))
| Mutation (Maybe Text) (Seq (Selection m))
+ | Subscription (Maybe Text) (Seq (Selection m))
-- | Single GraphQL field.
data Field m = Field
@@ -275,6 +276,8 @@ operation operationDefinition replacement
Query name <$> appendSelection sels
transform (OperationDefinition Full.Mutation name _ _ sels) =
Mutation name <$> appendSelection sels
+ transform (OperationDefinition Full.Subscription name _ _ sels) =
+ Subscription name <$> appendSelection sels
-- * Selection