Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Fix GraphQL subscriptions (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
serras authored Nov 12, 2020
1 parent 94f0e4e commit a53e523
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 7 additions & 2 deletions graphql/src/Mu/GraphQL/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,13 @@ wsGraphQLAppTrans
-> WS.ServerApp
wsGraphQLAppTrans f server q m s conn
= do let headers = WS.requestHeaders $ WS.pendingRequest conn
conn' <- WS.acceptRequest conn
flip protocol conn' $ runSubscriptionPipeline f headers server q m s
case lookup "Sec-WebSocket-Protocol" headers of
Just v
| v == "graphql-ws" || v == "graphql-transport-ws"
-> do conn' <- WS.acceptRequestWith conn (WS.AcceptRequest (Just v) [])
flip protocol conn' $
runSubscriptionPipeline f headers server q m s
_ -> WS.rejectRequest conn "unsupported protocol"

-- | Run a Mu 'graphQLApp' using the given 'Settings'.
--
Expand Down
7 changes: 5 additions & 2 deletions graphql/src/Mu/GraphQL/Subscription/Protocol.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ data ServerMessage
GQLKeepAlive
deriving Show

-- NOTE: using https://github.com/apollographql/subscriptions-transport-ws/blob/master/src/message-types.ts
-- as source of truth for the message types

instance A.FromJSON ClientMessage where
parseJSON = A.withObject "ClientMessage" $ \v -> do
ty :: String <- v .: "type"
Expand Down Expand Up @@ -147,12 +150,12 @@ instance A.ToJSON ServerMessage where
toJSON (GQLConnectionError e)
= A.object [theType "connection_error", "payload" .= e]
toJSON GQLConnectionAck
= A.object [theType "connection_acl"]
= A.object [theType "connection_ack"]
toJSON (GQLData i p)
= A.object [theType "data", "id" .= i, "payload" .= p]
toJSON (GQLError i p)
= A.object [theType "error", "id" .= i, "payload" .= p]
toJSON (GQLComplete i)
= A.object [theType "complete", "id" .= i]
toJSON GQLKeepAlive
= A.object [theType "connection_keep_alive"]
= A.object [theType "ka"]

0 comments on commit a53e523

Please sign in to comment.