-
Notifications
You must be signed in to change notification settings - Fork 738
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SelectionSet Codegen __typename fix - Closes #2955 #2983
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ public class CompilationResult: JavaScriptObject { | |
private enum Constants { | ||
static let LocalCacheMutationDirectiveName = "apollo_client_ios_localCacheMutation" | ||
} | ||
lazy var rootTypes: RootTypeDefinition = self["rootTypes"] | ||
|
||
lazy var referencedTypes: [GraphQLNamedType] = self["referencedTypes"] | ||
|
||
lazy var operations: [OperationDefinition] = self["operations"] | ||
|
@@ -13,6 +15,31 @@ public class CompilationResult: JavaScriptObject { | |
|
||
lazy var schemaDocumentation: String? = self["schemaDocumentation"] | ||
|
||
required init(_ jsValue: JSValue, bridge: JavaScriptBridge) { | ||
super.init(jsValue, bridge: bridge) | ||
processRootTypes() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking through the code, and where/how the |
||
} | ||
|
||
private func processRootTypes() { | ||
let typeList = [rootTypes.queryType.name, rootTypes.mutationType?.name, rootTypes.subscriptionType?.name].compactMap { $0 } | ||
|
||
operations.forEach { op in | ||
op.rootType.isRootFieldType = typeList.contains(op.rootType.name) | ||
} | ||
|
||
fragments.forEach { fragment in | ||
fragment.type.isRootFieldType = typeList.contains(fragment.type.name) | ||
} | ||
} | ||
|
||
public class RootTypeDefinition: JavaScriptObject { | ||
lazy var queryType: GraphQLNamedType = self["queryType"] | ||
|
||
lazy var mutationType: GraphQLNamedType? = self["mutationType"] | ||
|
||
lazy var subscriptionType: GraphQLNamedType? = self["subscriptionType"] | ||
} | ||
|
||
public class OperationDefinition: JavaScriptObject, Equatable { | ||
lazy var name: String = self["name"] | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env bash | ||
|
||
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | ||
output_file="$SCRIPT_DIR/ApolloCodegenFrontendBundle.swift" | ||
$( cd "$SCRIPT_DIR/Javascript" && rollup -c ) | ||
minJS=$(cat "$SCRIPT_DIR/dist/ApolloCodegenFrontend.bundle.js") | ||
printf "%s%s%s" "let ApolloCodegenFrontendBundle: String = #\"" "$minJS" "\"#" > $output_file | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nicely done! |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -336,6 +336,54 @@ class SelectionSetTemplateTests: XCTestCase { | |
// then | ||
expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) | ||
} | ||
|
||
func test__render_selections__givenCustomRootTypes_doesNotGenerateTypenameField() throws { | ||
// given | ||
schemaSDL = """ | ||
schema { | ||
query: RootQueryType | ||
mutation: RootMutationType | ||
} | ||
|
||
type RootQueryType { | ||
allAnimals: [Animal!] | ||
} | ||
|
||
type RootMutationType { | ||
feedAnimal: Animal! | ||
} | ||
|
||
type Animal { | ||
FieldName: String! | ||
} | ||
""" | ||
|
||
document = """ | ||
query TestOperation { | ||
allAnimals { | ||
FieldName | ||
} | ||
} | ||
""" | ||
|
||
let expected = """ | ||
public static var __selections: [Apollo.Selection] { [ | ||
.field("allAnimals", [AllAnimal]?.self), | ||
] } | ||
""" | ||
|
||
// when | ||
try buildSubjectAndOperation(cocoapodsImportStatements: true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be needing the |
||
let allAnimals = try XCTUnwrap( | ||
operation[field: "query"] as? IR.EntityField | ||
) | ||
|
||
let actual = subject.render(field: allAnimals) | ||
print("Actual Output - \(actual)") | ||
|
||
// then | ||
expect(actual).to(equalLineByLine(expected, atLine: 7, ignoringExtraLines: true)) | ||
} | ||
|
||
// MARK: Selections - Fields | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻