DX issue while using 3D(data-driven dependencies) feature
- Dominant language
- Rust
- Stars
- 19k
- Forks
- 1.9k
- PR merge metrics
- No merged PRs in 30d
Description
Hello Team,
I am researching about 3D(Data Driven dependencies)
Is 3D suitable for building a generic table where there is a finite set of table cell types, that can repeat?
**Usecase**: Rendering the same data differently using separate JS modules
In the below example components `RelativeDateCell.react` and `DateCell.react` uses unique fragments but depend on the exact same data. This is so that they need to render them differently.
Made up example below.
``` javascript
const data = useFragment(
graphql`
fragment GenericTableResults_content on GenericTableResult {
headers {
title
isSortable
sortDirection
}
rows {
columns {
renderer @match {
...UserNameCell_content @module(name: "UserNameCell.react")
...CreatedDateCell_content @module(name: "DateCell.react")
...RelativeDateCell_content @module(name: "RelativeDateCell.react")
}
}
}
}
`,
content,
);
```
Note: I have used different fragment names and specified different modules but fragment content is the same
```
[ERROR] Error in the project `default`: ✖︎ Invalid @module selection: concrete type 'RelativeDateCell' was matched multiple times at path 'rows.columns.renderer' but with a different fragment or module name.
components/3d/GenericTableResult.js:27:18
26 │ @module(name: "RelativeDateCell")
27 │ ...RelativeDateCell_content
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
28 │ @module(name: "RelativeDateCell.react")
ℹ︎ related location
components/3d/GenericTableResult.js:25:18
24 │ @module(name: "DateCell.react")
25 │ ...RelativeDateCell_content
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26 │ @module(name: "RelativeDateCell")
[ERROR] Compilation failed.
[ERROR] Unable to run relay compiler. Error details:
```
For reference validation code clearly explain this constraint as relay depends on the same js field alias
https://github.com/facebook/relay/blob/5603ef5a5d4113cabb1a79cd0dca630b1800b242/compiler/crates/relay-transforms/src/match_/match_transform.rs#L395-L408
If relay skips this validation the following wrong GraphQL query will get generated (note the same alias with different field arguments)
```
... on GenericDataCell {
...GenericDataCell_content
__module_operation_GenericResults_content: js(module: "GenericDataCell_content$normalization.graphql", id: "GenericResults_content.rows.columns.renderer")
__module_component_GenericResults_content: js(module: "GenericDataCell", id: "GenericResults_content.rows.columns.renderer")
}
... on GenericDataCell {
...GenericDataCell2_content
__module_operation_GenericResults_content: js(module: "GenericDataCell2_content$normalization.graphql", id: "GenericResults_content.rows.columns.renderer")
__module_component_GenericResults_content: js(module: "GenericDataCell2", id: "GenericResults_content.rows.columns.renderer")
}
```
The underlying server will throw error
`Fields \"__module_operation_GenericTableResults_content\" conflict because they have differing arguments. Use different aliases on the fields to fetch both if this was intentional.`
## Why this is not ideal?
This kinda challenges my assumption for using 3D as a mechanism to converge on a subset of generic components and generic schema types I can depend on.
## Workaround
The way I worked around this problem is to use a wrapper type that lets me base the fragment on a new GraphQL type.
But this kinda makes me sad as I now need to touch schema every time I face a situation like so.
## Solution Proposal
Changing https://github.com/facebook/relay/blob/5603ef5a5d4113cabb1a79cd0dca630b1800b242/compiler/crates/relay-transforms/src/match_/match_transform.rs#L586
to include fragment name can help with this?
```rust
- format!("__module_operation_{}", match_directive_key_argument).intern(),
+ format!("__module_operation_{}_{}", match_directive_key_argument, spread.fragment.item.0).intern(),
```
this can make the alias unique
like `__module_operation_GenericResults_content_GenericDataCell2_content: js(module: "GenericDataCell2_content$normalization.graphql", id: "GenericResults_content.rows.columns.renderer")`
Contributor guide
Assessment
This issue has not been assessed yet.