Skip to content

Commit a8cd01f

Browse files
committed
Fix error when single query is anonymous.
``` "{\"query\":\"query {\\n greeting(who: \\\"Tim\\\")\\n}\"}" ``` Notice that the query (immediately after the start of the JSON field `query:`) has no operationName, i.e. it's anonymous. This gets decoded to: ``` Just (GraphQLPostRequest {query = "query {\n greeting(who: \"Tim\")\n}", operationName = "", variables = fromList []}) ``` by a custom/temporary Aeson parser. :blush: I didn't record it, and now it's gone. Something along the lines of: `Just(Error{"query document error!definition error!query"})` Not exactly, but that was the gist of it. Realized that the parser might be choking on the absence of the `operationName`, so tried to apply `optempty` to `nameParser` but Name was not an instance of Monoid. Changed that and ¡viola! it worked (sounds easy, but I learned something about applying Monoid to a newtype, and also picked up a prior mistake where I forgot to import Data.Text). On a side note, the 'custom/temporary' Aeson parser does not yet solve the ambiguous `variables` problem mentioned here: and here: and obliquely here:
1 parent d27f591 commit a8cd01f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/GraphQL/Internal/Name.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Data.Text as T (Text)
2525
import qualified Data.Attoparsec.Text as A
2626
import Test.QuickCheck (Arbitrary(..), elements, listOf)
2727
import Data.String (IsString(..))
28+
import Data.Text as T (Text, append, empty)
2829

2930
import GraphQL.Internal.Syntax.Tokens (tok)
3031

@@ -35,6 +36,20 @@ import GraphQL.Internal.Syntax.Tokens (tok)
3536
-- https://facebook.github.io/graphql/#sec-Names
3637
newtype Name = Name { unName :: T.Text } deriving (Eq, Ord, Show)
3738

39+
instance Monoid Name where
40+
mempty = Name T.empty
41+
-- mappend (Name {a}) mempty = Name {a}
42+
-- mappend mempty (Name {b}) = Name {b}
43+
mappend (Name a1) (Name a2) = Name (T.append a1 a2)
44+
-- mappend = append
45+
-- mconcat = concat
46+
47+
--newtype Any = Any { getAny :: Bool }
48+
49+
--instance Monoid Any where
50+
-- mempty = Any False
51+
-- (Any b1) `mappend` (Any b2) = Any (b1 || b2)
52+
3853
-- | Create a 'Name', panicking if the given text is invalid.
3954
--
4055
-- Prefer 'makeName' to this in all cases.

src/GraphQL/Internal/Syntax/Parser.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ operationDefinition =
5252
<?> "operationDefinition error!"
5353

5454
node :: Parser AST.Node
55-
node = AST.Node <$> nameParser
55+
node = AST.Node <$> optempty nameParser
5656
<*> optempty variableDefinitions
5757
<*> optempty directives
5858
<*> selectionSet

0 commit comments

Comments
 (0)