zino-hofmann / graphql-flutter

A GraphQL client for Flutter, bringing all the features from a modern GraphQL client to one easy to use package.
https://zino-hofmann.github.io/graphql-flutter
MIT License
3.25k stars 620 forks source link

Subscription against backend API spring graphql not working : backend says apollographql/subscriptions-transport-ws is not supported #1257

Closed TeamEasymanage closed 1 year ago

TeamEasymanage commented 1 year ago

Subscription against backend API spring graphql not working : backend says apollographql/subscriptions-transport-ws is not supported

pubspec.yaml has graphql: ^5.1.1

Flutter side gives message: 14 Disconnected from websocket.

And back end API log says: 2022-11-18 13:40:37.063 DEBUG 13532 --- [nio-9080-exec-4] org.apache.tomcat.websocket.WsSession : Created WebSocket session [c] 2022-11-18 13:40:37.063 DEBUG 13532 --- [nio-9080-exec-4] s.w.s.h.LoggingWebSocketHandlerDecorator : New StandardWebSocketSession[id=ad1f45ef-b101-0e88-e310-153f3c4d1f2e, uri=ws://127.0.0.1:9080/graphql] 2022-11-18 13:40:37.063 DEBUG 13532 --- [nio-9080-exec-4] o.s.g.s.webmvc.GraphQlWebSocketHandler : apollographql/subscriptions-transport-ws is not supported, nor maintained. Please, use https://github.com/enisdenjo/graphql-ws. 2022-11-18 13:40:37.063 DEBUG 13532 --- [nio-9080-exec-4] o.s.w.s.adapter.NativeWebSocketSession : Closing StandardWebSocketSession[id=ad1f45ef-b101-0e88-e310-153f3c4d1f2e, uri=ws://127.0.0.1:9080/graphql] 2022-11-18 13:40:37.063 DEBUG 13532 --- [nio-9080-exec-4] org.apache.tomcat.websocket.WsSession : Closing WebSocket session [c]

Please update to supported websocket protocol by spring graphql. Or advise how we can use the graphql-ws protocol in your package. Please note I can use graphql query, mutations against spring-graphql without any problems, only subscription giving problem.

Reference doc links: https://spring.io/projects/spring-graphql https://docs.spring.io/spring-graphql/docs/current/reference/html/

GraphQL Over WebSocket Protocols There are two such protocols, one in the subscriptions-transport-ws library and another in the graphql-ws library. The former is not active and succeeded by the latter. Read this blog post for the history.

TeamEasymanage commented 1 year ago

SOLVES problem by: Modifying websocket protocol in your package (tried using downloaded package and modifying locally) see below:


[package file] graphql\lib\src\links\websocket_link\websocket_link.dart Line 19: Change To

this.subProtocol = GraphQLProtocol.graphqlTransportWs,

Then the connection and Subscription works against Spring Graphql Subscription


Flutter log output info after fix:

Initialising connection in listen New Review: {notifyDgproductTblRecQuery: [{typename: DgproductTblRec, productId: 2, productType: StringVal1, productDesc: StringVal1, productCategory: StringVal1, primarySupplier: , productPicture: null}]} in listen New Review: {notifyDgproductTblRecQuery: [{typename: DgproductTblRec, productId: 2, productType: StringVal1, productDesc: StringVal1, productCategory: StringVal1, primarySupplier: , productPicture: null}]} in listen

API log info:

2022-11-19 10:49:56.208 DEBUG 6416 --- [nio-9080-exec-3] org.apache.tomcat.websocket.WsSession : Created WebSocket session [0] 2022-11-19 10:49:56.214 DEBUG 6416 --- [nio-9080-exec-3] s.w.s.h.LoggingWebSocketHandlerDecorator : New StandardWebSocketSession[id=a8ec596d-ddb1-aad6-54e9-2e2304f7a1aa, uri=ws://127.0.0.1:9080/graphql] 2022-11-19 10:49:56.233 DEBUG 6416 --- [nio-9080-exec-3] o.a.t.websocket.server.WsFrameServer : wsFrameServer.onDataAvailable 2022-11-19 10:49:56.233 DEBUG 6416 --- [nio-9080-exec-3] o.a.tomcat.util.net.SocketWrapperBase : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@14092ac:org.apache.tomcat.util.net.NioChannel@1876b34:java.nio.channels.SocketChannel[connected local=/127.0.0.1:9080 remote=/127.0.0.1:63129]], Read from buffer: [0] 2022-11-19 10:49:56.233 DEBUG 6416 --- [nio-9080-exec-3] org.apache.tomcat.util.net.NioEndpoint : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@14092ac:org.apache.tomcat.util.net.NioChannel@1876b34:java.nio.channels.SocketChannel[connected local=/127.0.0.1:9080 remote=/127.0.0.1:63129]], Read direct from socket: [34] 2022-11-19 10:49:56.233 DEBUG 6416 --- [nio-9080-exec-3] o.a.t.websocket.server.WsFrameServer : Read [34] bytes into input buffer ready for processing 2022-11-19 10:49:56.233 DEBUG 6416 --- [nio-9080-exec-3] o.a.t.websocket.server.WsFrameServer : WebSocket frame received. fin [true], rsv [4], OpCode [1], payload length [28] 2022-11-19 10:49:56.333 DEBUG 6416 --- [nio-9080-exec-3] o.a.tomcat.util.net.SocketWrapperBase : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@14092ac:org.apache.tomcat.util.net.NioChannel@1876b34:java.nio.channels.SocketChannel[connected local=/127.0.0.1:9080 remote=/127.0.0.1:63129]], Read from buffer: [0] 2022-11-19 10:49:56.333 DEBUG 6416 --- [nio-9080-exec-3] org.apache.tomcat.util.net.NioEndpoint : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@14092ac:org.apache.tomcat.util.net.NioChannel@1876b34:java.nio.channels.SocketChannel[connected local=/127.0.0.1:9080 remote=/127.0.0.1:63129]], Read direct from socket: [0] 2022-11-19 10:49:56.333 DEBUG 6416 --- [nio-9080-exec-3] o.a.c.h.u.UpgradeProcessorInternal : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@14092ac:org.apache.tomcat.util.net.NioChannel@1876b34:java.nio.channels.SocketChannel[connected local=/127.0.0.1:9080 remote=/127.0.0.1:63129]], Status in: [OPEN_READ], State out: [UPGRADED] 2022-11-19 10:49:56.333 DEBUG 6416 --- [nio-9080-exec-3] org.apache.tomcat.util.net.NioEndpoint : Registered read interest for [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@14092ac:org.apache.tomcat.util.net.NioChannel@1876b34:java.nio.channels.SocketChannel[connected local=/127.0.0.1:9080 remote=/127.0.0.1:63129]] 2022-11-19 10:49:56.350 DEBUG 6416 --- [nio-9080-exec-1] o.a.t.websocket.server.WsFrameServer : wsFrameServer.onDataAvailable 2022-11-19 10:49:56.352 DEBUG 6416 --- [nio-9080-exec-1] o.a.tomcat.util.net.SocketWrapperBase : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@14092ac:org.apache.tomcat.util.net.NioChannel@1876b34:java.nio.channels.SocketChannel[connected local=/127.0.0.1:9080 remote=/127.0.0.1:63129]], Read from buffer: [0] 2022-11-19 10:49:56.352 DEBUG 6416 --- [nio-9080-exec-1] org.apache.tomcat.util.net.NioEndpoint : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@14092ac:org.apache.tomcat.util.net.NioChannel@1876b34:java.nio.channels.SocketChannel[connected local=/127.0.0.1:9080 remote=/127.0.0.1:63129]], Read direct from socket: [210] 2022-11-19 10:49:56.352 DEBUG 6416 --- [nio-9080-exec-1] o.a.t.websocket.server.WsFrameServer : Read [210] bytes into input buffer ready for processing 2022-11-19 10:49:56.352 DEBUG 6416 --- [nio-9080-exec-1] o.a.t.websocket.server.WsFrameServer : WebSocket frame received. fin [true], rsv [4], OpCode [1], payload length [126] 2022-11-19 10:49:56.352 DEBUG 6416 --- [nio-9080-exec-1] o.s.g.s.webmvc.GraphQlWebSocketHandler : Executing: document='subscription { notifyDgproductTblRecQuery(productId: 2) { __typename productId productType productDesc productCategory primarySupplier productPicture } }', id=3fcb862c-1caa-4b0c-b431-f5ffbe271028

vincenzopalazzo commented 1 year ago

YEah was a protocol problem!

Cool!

Centauride commented 1 year ago

I had the same problem ! I searched for hours what I was doing wrong. Then I came across your solution to change the Subprotocol and it worked, thanks !!

SOLVES problem by: Modifying websocket protocol in your package (tried using downloaded package and modifying locally) see below:

[package file] graphql\lib\src\links\websocket_link\websocket_link.dart Line 19: Change To

this.subProtocol = GraphQLProtocol.graphqlTransportWs,

Then the connection and Subscription works against Spring Graphql Subscription

Flutter log output info after fix:

Initialising connection in listen New Review: {notifyDgproductTblRecQuery: [{typename: DgproductTblRec, productId: 2, productType: StringVal1, productDesc: StringVal1, productCategory: StringVal1, primarySupplier: , productPicture: null}]} in listen New Review: {notifyDgproductTblRecQuery: [{typename: DgproductTblRec, productId: 2, productType: StringVal1, productDesc: StringVal1, productCategory: StringVal1, primarySupplier: , productPicture: null}]} in listen

API log info:

2022-11-19 10:49:56.208 DEBUG 6416 --- [nio-9080-exec-3] org.apache.tomcat.websocket.WsSession : Created WebSocket session [0] 2022-11-19 10:49:56.214 DEBUG 6416 --- [nio-9080-exec-3] s.w.s.h.LoggingWebSocketHandlerDecorator : New StandardWebSocketSession[id=a8ec596d-ddb1-aad6-54e9-2e2304f7a1aa, uri=ws://127.0.0.1:9080/graphql] 2022-11-19 10:49:56.233 DEBUG 6416 --- [nio-9080-exec-3] o.a.t.websocket.server.WsFrameServer : wsFrameServer.onDataAvailable 2022-11-19 10:49:56.233 DEBUG 6416 --- [nio-9080-exec-3] o.a.tomcat.util.net.SocketWrapperBase : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@14092ac:org.apache.tomcat.util.net.NioChannel@1876b34:java.nio.channels.SocketChannel[connected local=/127.0.0.1:9080 remote=/127.0.0.1:63129]], Read from buffer: [0] 2022-11-19 10:49:56.233 DEBUG 6416 --- [nio-9080-exec-3] org.apache.tomcat.util.net.NioEndpoint : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@14092ac:org.apache.tomcat.util.net.NioChannel@1876b34:java.nio.channels.SocketChannel[connected local=/127.0.0.1:9080 remote=/127.0.0.1:63129]], Read direct from socket: [34] 2022-11-19 10:49:56.233 DEBUG 6416 --- [nio-9080-exec-3] o.a.t.websocket.server.WsFrameServer : Read [34] bytes into input buffer ready for processing 2022-11-19 10:49:56.233 DEBUG 6416 --- [nio-9080-exec-3] o.a.t.websocket.server.WsFrameServer : WebSocket frame received. fin [true], rsv [4], OpCode [1], payload length [28] 2022-11-19 10:49:56.333 DEBUG 6416 --- [nio-9080-exec-3] o.a.tomcat.util.net.SocketWrapperBase : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@14092ac:org.apache.tomcat.util.net.NioChannel@1876b34:java.nio.channels.SocketChannel[connected local=/127.0.0.1:9080 remote=/127.0.0.1:63129]], Read from buffer: [0] 2022-11-19 10:49:56.333 DEBUG 6416 --- [nio-9080-exec-3] org.apache.tomcat.util.net.NioEndpoint : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@14092ac:org.apache.tomcat.util.net.NioChannel@1876b34:java.nio.channels.SocketChannel[connected local=/127.0.0.1:9080 remote=/127.0.0.1:63129]], Read direct from socket: [0] 2022-11-19 10:49:56.333 DEBUG 6416 --- [nio-9080-exec-3] o.a.c.h.u.UpgradeProcessorInternal : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@14092ac:org.apache.tomcat.util.net.NioChannel@1876b34:java.nio.channels.SocketChannel[connected local=/127.0.0.1:9080 remote=/127.0.0.1:63129]], Status in: [OPEN_READ], State out: [UPGRADED] 2022-11-19 10:49:56.333 DEBUG 6416 --- [nio-9080-exec-3] org.apache.tomcat.util.net.NioEndpoint : Registered read interest for [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@14092ac:org.apache.tomcat.util.net.NioChannel@1876b34:java.nio.channels.SocketChannel[connected local=/127.0.0.1:9080 remote=/127.0.0.1:63129]] 2022-11-19 10:49:56.350 DEBUG 6416 --- [nio-9080-exec-1] o.a.t.websocket.server.WsFrameServer : wsFrameServer.onDataAvailable 2022-11-19 10:49:56.352 DEBUG 6416 --- [nio-9080-exec-1] o.a.tomcat.util.net.SocketWrapperBase : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@14092ac:org.apache.tomcat.util.net.NioChannel@1876b34:java.nio.channels.SocketChannel[connected local=/127.0.0.1:9080 remote=/127.0.0.1:63129]], Read from buffer: [0] 2022-11-19 10:49:56.352 DEBUG 6416 --- [nio-9080-exec-1] org.apache.tomcat.util.net.NioEndpoint : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@14092ac:org.apache.tomcat.util.net.NioChannel@1876b34:java.nio.channels.SocketChannel[connected local=/127.0.0.1:9080 remote=/127.0.0.1:63129]], Read direct from socket: [210] 2022-11-19 10:49:56.352 DEBUG 6416 --- [nio-9080-exec-1] o.a.t.websocket.server.WsFrameServer : Read [210] bytes into input buffer ready for processing 2022-11-19 10:49:56.352 DEBUG 6416 --- [nio-9080-exec-1] o.a.t.websocket.server.WsFrameServer : WebSocket frame received. fin [true], rsv [4], OpCode [1], payload length [126] 2022-11-19 10:49:56.352 DEBUG 6416 --- [nio-9080-exec-1] o.s.g.s.webmvc.GraphQlWebSocketHandler : Executing: document='subscription { notifyDgproductTblRecQuery(productId: 2) { __typename productId productType productDesc productCategory primarySupplier productPicture } }', id=3fcb862c-1caa-4b0c-b431-f5ffbe271028

TeamEasymanage commented 1 year ago

Glad that it helped you Centauride!

Thanks Vincenzo Palazzo for taking care.