facebook / facebook/relay

When running a test over code that uses both 2 queries with different aliases 2 different values are returned

Open
#4,367 0 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

### Overview
If you have a component that uses a value from relay, then a child component of this component that uses the same value but alias's it to fit the component requirements then when making a mock via relay-test-utils then you will receive 2 different mocks for the same value. This is seems because in real life these are the same values

### Example

Say we have a schema like this
```
animalFamily {
id
name
animalsConnection(first: 100) {
edges {
node {
name
weight
}
}
}
}
```

and a parent component called AnimalList:
```
const AnimalList = ({mainQueryFragment}) => {

const data = useFragment(
graphql`
fragment animalFamily_AnimalList on AnimalFamily {
...animalsFragment
name
animalsConnection(first:100) {
edges {
node {
weight
}
}
}
}
`,
mainQueryFragment,
)

const totalWeight = data.animalsConnection.edges.reduce((total, node) => total + node.weight, 0)

return (


{data.name}


Total Weight


{totalWeight}




)
}

}
```

and a child component called Animals:
```
const Animals = ({animalsFragment}) => {

const data = useFragment(
graphql`
fragment animalsFragment on AnimalFamily {
animalsConnection(first:100) {
edges {
node {
name
animalWeight: weight
}
}
}
}
`,
animalsFragment,
)

const totalWeight = data.animalsConnection.edges.reduce((total, node) => total + node.animalWeight, 0)

return (



    {data.edges.map((animalNode) => {
  • {animalNode.name}

  • })}

The weight of the above animals is: {totalWeight}



)
}

}
```

So now if you made a test to test `AnimalList.js` and used `RelayMockEnvironment` from `relay-test-utils` then it will mock all the values. BUT it will mock a different value for the alias'ed weight compared to where we are not aliasing the weight in the parent component, meaning if we want to test that the 2 total weights are the same it will fail because they will be different values.

I realise in this example there is not a real need to alias, and it doesn't make business sense here to show the total weight twice but in our real life example being able to alias components across our repo without worrying if we're using that same value somewhere else and whether its alias'd would definately improve our dev quality of life.

Currently our work around is just to not alias anything, but aliasing is super useful and makes much cleaner code

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.