Instantiate structs with only one fragment directly
nerdsupremacist opened this issue · comments
Mathias Quintero commented
This example should be valid:
struct CountryMapPin {
@GraphQL(Covid.Country.cases)
var cases: Int
@GraphQL(Covid.Country.info.latitude)
var latitude: Double?
@GraphQL(Covid.Country.info.longitude)
var longitude: Double?
}
struct ContentView: View {
@GraphQL(Covid.countries)
var pins: [CountryMapPin]
var body: some View {
MapView(pins: pins).frame(height: 400)
}
}
Proposal
We solve this by adding the following extension to every struct that has a single fragmentl:
extension CountryMapPin: Fragment {
typealias UnderlyingType = Country.UnderlyingType
}
extension CountryMapPin.Country {
func countryMapPin() -> CountryMapPin {
return CountryMapPin(country: self)
}
}
In code generation add an extra operation to implicitly convert. Create it during resolution.
Mathias Quintero commented
First draft of this proposal is in 157bdb4
It appears to work fine simple scenarios. I have to perform more testing on these scenarios before a release:
- Optionals
- Array
- Array + Optional combinations
- Connections
- Mutations