graphql-java-kickstart / graphql-java-tools

A schema-first tool for graphql-java inspired by graphql-tools for JS
https://www.graphql-java-kickstart.com/tools/
MIT License
810 stars 174 forks source link

DataFetchingEnvironment Context properties are null #233

Open andreaskas opened 5 years ago

andreaskas commented 5 years ago

Hello,

I am trying during a HTTP request or a socket to have a unique id for each request and do some clean-up actions onClose(). In specific check if the browser is still alive before returning the result. My approach was to use the socket object via dataFetchingEnvironment.getContext().getSession() and check if the session is still alive. The problem is by using dataFetchingEnvironment.getContext() all fields are null and the above method will not work.

After some digging i saw that session despite the fact that is available at some point it's not passed during the creation of GraphQLContext object.

Can anyone provide any guidance on how to overcome this? Or any other approach that should do the trick?

Thanks in advance

oliemansm commented 5 years ago

@andreaskas You mentioned this:

After some digging i saw that session despite the fact that is available at some point it's not passed during the creation of GraphQLContext object.

Where is this exactly (class and line number)?

andreaskas commented 5 years ago

Hello @oliemansm ,

I am not sure if this was my finding the time i opened the ticket. But after looking again now i see that in ApolloSubscirptionProtocolHandler.java:82 is the last time the session is available.

For sure i remember that from somewhere the method DefaultGraphQLContextBuilder.build() was called instead of the one with the parameters build(Session session, HandshakeRequest handshakeRequest) (file DefaultGraphQLContextBuilder.java) that session was available from the calling method.

Thanks

andreaskas commented 5 years ago

@oliemansm Regarding your initial question where the session is lost and the default GraphQLContextBuilder.build() is called.

I managed to reproduce the issue. Method from GraphQLInvocationInputFactory.create(GraphQLRequest graphQLRequest) (line 48) is called that calls contextBuilderSupplier.get().build() (build without any arguments). Even this call on create method is invalid as it should use those with the Session or HttpServletRequest/Response parameters.

Also, the issue is happening randomly. Sometimes it calls the correct create methods (from GraphQLInvocationInputFactory) with the correct parameters (Session or HttpServletRequest/Response) and the correct build method (from GraphQLContextBuilder) and some other the create method (from GraphQLInvocationInputFactory) without those parameters.

In any case i can not understand this randomness, so any help by your side is much appreciated. Also, please find bellow the stacktrace during the call to default build()

build:22, DefaultGraphQLContextBuilder (graphql.servlet)
create:52, GraphQLInvocationInputFactory (graphql.servlet)
onMessage:26, FallbackSubscriptionProtocolHandler (graphql.servlet.internal)
onMessage:79, GraphQLWebsocketServlet$1 (graphql.servlet)
onMessage:75, GraphQLWebsocketServlet$1 (graphql.servlet)
sendMessageText:395, WsFrameBase (org.apache.tomcat.websocket)
sendMessageText:119, WsFrameServer (org.apache.tomcat.websocket.server)
processDataText:495, WsFrameBase (org.apache.tomcat.websocket)
processData:294, WsFrameBase (org.apache.tomcat.websocket)
processInputBuffer:133, WsFrameBase (org.apache.tomcat.websocket)
onDataAvailable:82, WsFrameServer (org.apache.tomcat.websocket.server)
doOnDataAvailable:171, WsFrameServer (org.apache.tomcat.websocket.server)
notifyDataAvailable:151, WsFrameServer (org.apache.tomcat.websocket.server)
upgradeDispatch:148, WsHttpUpgradeHandler (org.apache.tomcat.websocket.server)
dispatch:54, UpgradeProcessorInternal (org.apache.coyote.http11.upgrade)
process:53, AbstractProcessorLight (org.apache.coyote)
process:834, AbstractProtocol$ConnectionHandler (org.apache.coyote)
doRun:1417, NioEndpoint$SocketProcessor (org.apache.tomcat.util.net)
run:49, SocketProcessorBase (org.apache.tomcat.util.net)
runWorker:1149, ThreadPoolExecutor (java.util.concurrent)
run:624, ThreadPoolExecutor$Worker (java.util.concurrent)
run:61, TaskThread$WrappingRunnable (org.apache.tomcat.util.threads)
run:748, Thread (java.lang)
oliemansm commented 5 years ago

Thanks for the additional info, will try to find some time to take a look at this.

CLOUGH commented 4 years ago

Was this issue resolved?

v111 commented 2 years ago

@CLOUGH were you able to resolve the issue.... am using jetty server ang env.getContext is null