facebook / facebook/relay

Provided variables and Live Resolver modules should allow injected dependencies

Open
#4,000 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
19k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

Looking at [how Provided variables modules should be defined](https://relay.dev/docs/next/api-reference/graphql-and-directives/#provided-variables), and [how Live Resolvers modules are defined](https://github.com/captbaritone/redux-to-relay-with-live-resolvers-example/blob/step-1/src/relay/resolvers/RootAllTodosResolver.js), the way it is currently expected to access external services (like the Redux store, or a module to ask for feature flags), is by using singletons at the module level.

This is not compatible with Node.js environments (when doing SSR for example), where the modules are loaded once and multiple requests are handled sharing the same module state.

It would be ideal if the signature for provided variables modules and live resolvers allowed to get access to a "context" object that could be injected in the Relay Environment, so that we could put these services to be accessed by the modules, instead of accessing them as singletons. Something like this:

```
const store = ...
const featureFlags = ...
const environment = new Environment({
moduleContext: { store, featureFlags } // name TBD
});

return


```

And they would be consumed like this:

```
// a Provided Variable
export default {
get(moduleContext): boolean {
return moduleContext.featureFlags.check('todo_should_include_timestamp');
},
};
```

```
// a Live Resolver
export default function RootAllTodosResolver(moduleContext) {
return selectLiveState(moduleContext.store, (state) => {
return state.todos.map((todo) => todo.id);
});
}
```

Would this be something that would be acceptable for contribution? Thank you in advance for reading this!

Contributor guide

Open the contributing guide

Research direction

Start by reading the provided variables and Live Resolvers definitions linked in the issue, then trace how the Relay Environment is constructed and how those modules are invoked. A complete contribution would establish an injected module context for both module types and document or test the resulting behavior, including server-side rendering isolation.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, javascript, node.js, react
Domain
backend, frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.