facebook / facebook/relay

Relay-compiler, types and fragments

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

Description

Simplifying my situation below, but essentially - I have a fragment
```
fragment Location_details on Location {
id
name
lat
lon
}
```

and a query
```
query AvailableLocationsQuery($query: String) {
queryCities(query: $query) {
city
location {
...Location_details
}
}
querySpecialRegions(query: $query) {
specialRegion
location {
...Location_details
}
}
}
```

So, when given a response type, I want to be able to access `response.queryCities.city` as well as `response.queryCities.location.lat`.

However, `relay-compiler` does not emit type definitions for in-fragment items, whether I'm using it with TypeScript or Flow!.

In particular, `response.queryCities.location` contains `__fragments` object which seems to be totally useless at the point of usage; it does not agree with the dev-time typings (`" $fragmentRefs"`, `" $data"`). And obviously the TypeScript compiler cannot infer that `location` would have `lat` inside it.

So the question is:
1. Am I misusing the fragments? I mean, I'm not reusing these fragments in any child components - rather, I simply use them to reduce repeating what I query, as above.
2. Is there a way to make the compiler flatten the fragments, i.e. remove all `fragmentRefs` and instead actually return the hierarchical structure of the stuff I'm asking for?
3. Why are the query results structs typed as they are? (" $data" does not exist in runtime etc)

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.