Swift Codegen, fragments
See original GitHub issueQuestion/Feature request regarding the output of the generated code for swift when fragments are used.
We have a fragment called “FormattedText” to encapsulate complex text fields that have nested bold/italic/link content. And example quest would be
...
...
headingText {
...formattedText
}
...
The returned data is as follows
"headingText": {
"spans": [
{
"text": "Credit card utilization"
}
]
},
The issue arrises with the gerenated code:
...
public let headingText: HeadingText?
...
public struct HeadingText: GraphQLMappable {
public let __typename = "FormattedText"
public let fragments: Fragments
public init(reader: GraphQLResultReader) throws {
let formattedText = try FormattedText(reader: reader)
fragments = Fragments(formattedText: formattedText)
}
public struct Fragments {
public let formattedText: FormattedText
}
}
...
...
//and finally Fragmented Text
public struct FormattedText: GraphQLNamedFragment {
public static let fragmentDefinition =
"fragment formattedText on FormattedText {" +
" spans {" +
" text" +
" }" +
"}"
public static let possibleTypes = ["FormattedText"]
public let __typename = "FormattedText"
public let spans: [Span?]?
public init(reader: GraphQLResultReader) throws {
spans = try reader.optionalList(for: Field(responseName: "spans"))
}
public struct Span: GraphQLMappable {
public let __typename = "FormattedSpan"
public let text: String?
public init(reader: GraphQLResultReader) throws {
text = try reader.optionalValue(for: Field(responseName: "text"))
}
}
}
In order to access this object you would write:
data.headingText?.fragments.formattedText
The feedback is the fragments contatiner necessary? Should the HeadingText class itself not be a FormattedText or more closely implement the FormattedText fragment. This indirection strikes me as unnecessary.
Even the headingText?.__typename == “FormattedText” so this type has been fully specified.
eg:
headingText CKGraphQL.TestCreditFactorsQuery.Data.CreditFactor.HeadingText? some
__typename String "FormattedText"
fragments CKGraphQL.TestCreditFactorsQuery.Data.CreditFactor.HeadingText.Fragments
formattedText CKGraphQL.FormattedText
__typename String "FormattedText"
spans [CKGraphQL.FormattedText.Span?]? 1 value some
[0] CKGraphQL.FormattedText.Span? some
__typename String "FormattedSpan"
text String? "Credit card utilization" some
So my suggestion is, can we make HeadingText more directly inherit from FormattedText?
public struct HeadingText: FormattedText {
What are the conditions under which this fragment structure becomes more useful?
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (11 by maintainers)
Just came across this, sounds like it might solve our issues if it lands!
https://github.com/apple/swift/pull/8718
Hmm, @martijnwalraven in the example above, just using an associated type should correctly constrain things and make it more ergonomic. The following compiles correctly:
Used like so: