facebook / facebook/relay

[v14] Provided variables docs are wrong, or doesn't support esmodules

Open
#3,966 3 comments 3 reactions 1 assignee Claimed by @bigfootjon View on GitHub
Dominant language
Rust
Stars
19k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

I believe this is due to the Haste system used at Meta that handles JS modules differently, but the problem is this:

Following [the docs](https://relay.dev/docs/api-reference/graphql-and-directives/#provided-variables) I created a `UserIDArgumentProvider.relayprovider.js` file and used it in a fragment like so:

```
@argumentDefinitions(
id: {
type: "ID!"
provider: "../user/UserIDArgumentProvider.relayprovider"
}
)
```

(note the leading "../" on the provider path. It seems the argument provider import is relative to the output directory, which for me is `/__generated__`, so I needed to get back to `` but this is a tangent and might be worth a separate issue).

I then defined `user/UserIDProvider.relayprovider.js` as the docs indicate:

```
export default {
get(): string {
return "12345";
},
};
```

This compiles just fine, but the problem is at runtime, where this line of code fails:

https://github.com/facebook/relay/blob/17636d63796d6daf8ae9059d5a37f5609067f198/packages/relay-runtime/store/RelayConcreteVariables.js#L102

For my configuration, it should be:

```patch
-operationVariables[varName] = providedVariables[varName].get();
+operationVariables[varName] = providedVariables[varName].default.get();
```

However, I assume that won't work at Meta, so I guess it should support both. I remember this coming up with some other Relay feature and can't remember the resolution to that problem, so I leave that to the maintainers.

The alternative solution is to update the OSS docs to indicate that this is the correct way to write the provider:

```
export default function get(): string {
return "12345";
};
```

This version worked just fine for me.

I'm happy to put up a PR allowing a `default` or fixing the docs, but I'm not sure which is the right solution. Please let me know which way is preferred.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.