Comments (18)
BTW, compare my PR adding this functionality to express-graphql.
from react-relay-network-layer.
Please provide your prior realization with express-graphql
. It helps me point you to right existed solution, or create new one based on your example.
from react-relay-network-layer.
Sorry about how possibly verbose this explanation is, but I'm just trying to provide enough to show you my situation.
Here is a link to where I use the react-relay-network-layer GraphQL-related code, so that I can support batched queries (in a single HTTP request) on the GraphQL side of things ...
https://github.com/ryancole/league/blob/dataloader/src/graphql/server.js#L22-L30
And here is the express-graphql middleware handler in which you would normally handle a single HTTP request to the default graphql-express handler, but in my code I've wrapped using react-relay-network-layer ...
https://github.com/ryancole/league/blob/dataloader/src/graphql/util/handleGraphqlRequest.js#L7
That code above shows where I create my DataLoader instances.
Now, my intention is for these DataLoader instances to be made available to express-graphql via rootValue
but in such a way that one instance of them is used per request. The code that I linked does not work, because the express-graphql middleware function is executed per query, as opposed to per request, when using react-relay-network-layer.
from react-relay-network-layer.
Just another reply trying to explain my issue.
I'm needing to be able to provide rootValue
but a single instance of it to all express-graphql handlers. I essentially need to create a variable, pass it through react-relay-network-layer, and into each request handler.
from react-relay-network-layer.
Awesome explanation, straight what I wanted!
Right now does not exists solution for your case. But it's not hard for implementation.
I now prepare proposal for review.
from react-relay-network-layer.
I think I could just make an express middleware that pins my DataLoaders to the request object, before the react-relay-network-layer code runs, which would make them available to my GraphQL middleware down the line. That wouldn't require any changes to RRNL.
Edit: Although I'm not sure how logical that is, considering the loaders have nothing to do with the request itself and probably don't need to logically be associated with the request object.
from react-relay-network-layer.
Alright well, I went ahead and made this express middleware that creates my DataLoader instances, once per request, and pins them to the request object.
https://github.com/ryancole/league/blob/dataloader/src/graphql/util/expressDataLoaderMiddleware.js
I'm not sure that it is technically logical, but it gets me moving forward. If you think that you've got a better idea about how to possibly pass parameters through react-relay-network-layer's express middleware (possibly HOF) then I'll just keep checking back! Thanks!
from react-relay-network-layer.
Finally, I found the same solution as you provide, based on your first detailed explanation.
I just comitted example: https://github.com/nodkz/react-relay-network-layer/blob/master/examples/dataLoaderPerBatchRequest.js
Please review it and leave a message, if found some errors.
And many thanks for your issue, help and examples. I'll use same code in my project.
If no any additions, you may close issue.
from react-relay-network-layer.
Will be great if you fix typos and misunderstanding places in comments, right via github web-editor https://github.com/nodkz/react-relay-network-layer/edit/master/examples/dataLoaderPerBatchRequest.js
from react-relay-network-layer.
And last question, why do you pass DataLoader to rootValue
instead of context
?
I think that context
is the better place for such per request things by graphql design.
from react-relay-network-layer.
@nodkz Regarding your question about rootValue
versus context
. Based on my understanding, context
is for session based data, such as current user, etc, while rootValue
is for "everything else." The spec leaves this pretty open ended, and I made my decision based on some github comments by graphql contributors. I don't have links handy, but that's my recollection. I consider these data fetching functions to not be session related and more categorized as "everything else." :)
I'll go over your adjustments in just a few minutes.
from react-relay-network-layer.
@ryancole thanks for answer! Really this is a tricky question. I'm confusing, but till not change my decision due context
word meaning 😜 And all that based on user request, should be in context
, even fetchers.
Asked Lee for advice with context
and rootValue
https://twitter.com/nodkz/status/756378662711267328
from react-relay-network-layer.
@nodkz true. I personally don't see the clear difference between context
and rootValue
. That was a somewhat ambiguous change to GraphQL, imo.
@taion I'll check it out, thanks. @nodkz, I do believe this is the PR that taion is talking about: graphql/express-graphql#100
from react-relay-network-layer.
DataLoaders
should be in context
from react-relay-network-layer.
IMO you should really share both rootValue
and context
across all queries in a batch... I guess depending on the semantics you want. Like really a "batched query" is just a single query that happens to have multiple query roots, with some workarounds for various Relay compat reasons.
from react-relay-network-layer.
There are no restrictions in sharing: https://github.com/nodkz/react-relay-network-layer/edit/master/examples/dataLoaderPerBatchRequest.js - one config for all queries per one http request.
We just try choose better place for DL.
from react-relay-network-layer.
No, I mean that there's not really much reason to not always share context and rootValue across all GraphQL requests in a batched HTTP request.
from react-relay-network-layer.
Ah, completely agreed with you.
Too scary and complex config for readme.md. I wanted write something hacky again. But I'll prefer wait your PR: graphql/express-graphql#100 And remove tons of my rubbish code.
Will pray 😉 that @leebyron accept it.
from react-relay-network-layer.
Related Issues (20)
- Throwing errors from middleware HOT 5
- batchUrl property of URL middleware is not passed req HOT 4
- Support max batch size HOT 2
- authMiddleware stop working as of 2.0.0! HOT 2
- __dataID__ unknown field HOT 2
- Rendering partial response with error messages HOT 1
- error: Processing of node_modules/react-relay-network-layer/lib/middleware/gqErrors.js failed. SyntaxError: Unexpected token (80:376) HOT 3
- Syntax Error GraphQL request (1:1) Unexpected EOF HOT 1
- Chinese Characters Issue HOT 3
- When will subscriptions be supported? HOT 2
- How to pass data from express req object to the req received by middleware HOT 3
- Relay Modern equivalent? HOT 6
- Redirect to login page and network layer HOT 1
- Token not refreshing after user logs out and login again with different user. HOT 4
- regeneratorRuntime not defined in lib/fetchWithMiddleware.js HOT 2
- Batching: make query id optional HOT 1
- Cannot access response header from middleware HOT 1
- Retry middleware not retrying HOT 8
- One of the sources for assign has an enumerable key on the prototype chain HOT 4
- Flowtype failing on my repo
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-relay-network-layer.