relay-tools / react-relay-network-layer

ReactRelayNetworkLayer with middlewares and query batching for Relay Classic.
MIT License
277 stars 47 forks source link

Isomorphic rendering and sequential network requests? #17

Closed abhishiv closed 8 years ago

abhishiv commented 8 years ago

Hey @nodkz

I have a very particular use case, and wanted to check if you have any thoughts.

So, I have a react relay isomorphic application which uses this as the network layer both on client and server. On each request I proxy cookies from the frontend to the graphql server using a custom middleware, so it knows who the user is.

However it breaks, when more than once request is made per render cycle on the server. The reason is right now it seems the network layer dispatches request in parallel. This breaks my use case because it results in two users in being created as I'm creating anonymous user like parse does.

Is there a way to dispatch requests sequentially?

nodkz commented 8 years ago

Yep, any middleware with user data makes react-relay-network-layer static. So it can not be used in the parallel requests.

Somewhere I saw an example with seq-queue (sorry, but can not found it now). Where all render tasks put to the sequential queue it and every call reinject network layer. With react-relay-network-layer you may not reinject it, just store cookie in some global var (your middleware should reread this var for every request).

Anyway, dispatch requests sequentially - bad idea. Very slowly and unproductively.

In my case, I do not render pages for authorized users on the server. Only for search engine bots (for anonymous user).

abhishiv commented 8 years ago

Thanks nodkz for you reply.

Yeah, that's what I'm doing. I think i'm able to bypass the issue for now by using a batch endpoint, which results in a single query.

In case any one else stumble upon it, I think you can also patch this function to do serially instead of parallel. https://github.com/nodkz/react-relay-network-layer/blob/2b815d1e9c1f6ee3b9fe2d3ad9f3cafbc6134298/src/relay/queries.js#L5