-
Notifications
You must be signed in to change notification settings - Fork 61
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
[codegen] import directive #236
Closed
hemel
wants to merge
13
commits into
apollographql:main
from
NetflixUI:dev/mel/import_statements_update
Closed
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7dabb6f
[codegen] add import directive
95cf909
[codegen] update codegen test
b7e3d8e
[codege] make importDirective struct private to package
5bc7ad5
[codegen] update tests
640e5c0
[codegen] render multiple import statements correctly
d2dad3f
[codegen] add import directive to fragments and operations, if a refe…
a90e823
[codegen] cleanup when adding additional imports in referenced fragments
4bb9b81
[codegen] pr review comment updates: user ordered set for import modu…
556cd98
[codegen] pr review comment updates: user ordered set for import modu…
4598e53
[codegen] pr review comment updates: user ordered set for import modu…
dfa41c7
[codegen] pr review comment updates: user ordered set for import modu…
615f8b2
[codegen] pr review comment updates: user ordered set for import modu…
031dbf2
[codegen] pr review comment updates: user ordered set for import modu…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import JavaScriptCore | ||
import TemplateString | ||
import OrderedCollections | ||
|
||
/// The output of the frontend compiler. | ||
public final class CompilationResult: JavaScriptObjectDecodable { | ||
|
||
/// String constants used to match JavaScriptObject instances. | ||
fileprivate enum Constants { | ||
enum DirectiveNames { | ||
static let Import = "import" | ||
static let LocalCacheMutation = "apollo_client_ios_localCacheMutation" | ||
static let Defer = "defer" | ||
} | ||
|
@@ -108,6 +110,8 @@ public final class CompilationResult: JavaScriptObjectDecodable { | |
public let filePath: String | ||
|
||
public let isLocalCacheMutation: Bool | ||
|
||
public let moduleImports: OrderedSet<String> | ||
|
||
static func fromJSValue(_ jsValue: JSValue, bridge: isolated JavaScriptBridge) -> Self { | ||
self.init( | ||
|
@@ -145,6 +149,9 @@ public final class CompilationResult: JavaScriptObjectDecodable { | |
self.filePath = filePath | ||
self.isLocalCacheMutation = directives? | ||
.contains { $0.name == Constants.DirectiveNames.LocalCacheMutation } ?? false | ||
|
||
self.moduleImports = OperationDefinition.getImportModuleNames(directives: directives, | ||
referencedFragments: referencedFragments) | ||
} | ||
|
||
public var debugDescription: String { | ||
|
@@ -158,6 +165,17 @@ public final class CompilationResult: JavaScriptObjectDecodable { | |
public static func ==(lhs: OperationDefinition, rhs: OperationDefinition) -> Bool { | ||
return lhs.name == rhs.name | ||
} | ||
|
||
private static func getImportModuleNames(directives: [Directive]?, | ||
referencedFragments: [FragmentDefinition]) -> OrderedSet<String> { | ||
let referencedImports: [String] = referencedFragments | ||
.map { $0.moduleImports } | ||
.flatMap { $0 } | ||
let directiveImports: [String] = directives?.compactMap { ImportDirective(directive: $0)?.moduleName } ?? [] | ||
var ordered = OrderedSet(referencedImports + directiveImports) | ||
ordered.sort() | ||
return ordered | ||
} | ||
} | ||
|
||
public enum OperationType: String, Equatable, Sendable, JavaScriptValueDecodable { | ||
|
@@ -214,15 +232,19 @@ public final class CompilationResult: JavaScriptObjectDecodable { | |
public var isLocalCacheMutation: Bool { | ||
directives?.contains { $0.name == Constants.DirectiveNames.LocalCacheMutation } ?? false | ||
} | ||
|
||
public let moduleImports: OrderedSet<String> | ||
|
||
init(_ jsValue: JSValue, bridge: isolated JavaScriptBridge) { | ||
self.name = jsValue["name"] | ||
self.directives = .fromJSValue(jsValue["directives"], bridge: bridge) | ||
self.type = .fromJSValue(jsValue["typeCondition"], bridge: bridge) | ||
self.selectionSet = .fromJSValue(jsValue["selectionSet"], bridge: bridge) | ||
self.directives = .fromJSValue(jsValue["directives"], bridge: bridge) | ||
self.referencedFragments = .fromJSValue(jsValue["referencedFragments"], bridge: bridge) | ||
self.source = jsValue["source"] | ||
self.filePath = jsValue["filePath"] | ||
self.moduleImports = FragmentDefinition.getImportModuleNames(directives: directives, | ||
referencedFragments: referencedFragments) | ||
} | ||
|
||
/// Initializer to be used for creating mock objects in tests only. | ||
|
@@ -242,6 +264,8 @@ public final class CompilationResult: JavaScriptObjectDecodable { | |
self.referencedFragments = referencedFragments | ||
self.source = source | ||
self.filePath = filePath | ||
self.moduleImports = FragmentDefinition.getImportModuleNames(directives: directives, | ||
referencedFragments: referencedFragments) | ||
} | ||
|
||
public var debugDescription: String { | ||
|
@@ -255,6 +279,18 @@ public final class CompilationResult: JavaScriptObjectDecodable { | |
public static func ==(lhs: FragmentDefinition, rhs: FragmentDefinition) -> Bool { | ||
return lhs.name == rhs.name | ||
} | ||
|
||
private static func getImportModuleNames(directives: [Directive]?, | ||
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. I can add this as a global function in order to avoid any code duplication. For now i left this helper inside the corresponding class. |
||
referencedFragments: [FragmentDefinition]) -> OrderedSet<String> { | ||
let referencedImports: [String] = referencedFragments | ||
.map { $0.moduleImports } | ||
.flatMap { $0 } | ||
let directiveImports: [String] = directives?.compactMap { ImportDirective(directive: $0)?.moduleName } ?? [] | ||
|
||
var ordered = OrderedSet(referencedImports + directiveImports) | ||
ordered.sort() | ||
return ordered | ||
} | ||
} | ||
|
||
public final class SelectionSet: | ||
|
@@ -527,7 +563,7 @@ public final class CompilationResult: JavaScriptObjectDecodable { | |
} | ||
} | ||
|
||
public struct Argument: | ||
public struct Argument: | ||
JavaScriptObjectDecodable, Sendable, Hashable { | ||
public let name: String | ||
|
||
|
@@ -650,7 +686,34 @@ public final class CompilationResult: JavaScriptObjectDecodable { | |
return string | ||
} | ||
} | ||
|
||
|
||
fileprivate struct ImportDirective: Hashable, CustomDebugStringConvertible, Sendable, Equatable { | ||
/// String constants used to match JavaScriptObject instances. | ||
enum Constants { | ||
enum ArgumentNames { | ||
static let Module = "module" | ||
} | ||
} | ||
|
||
let moduleName: String | ||
|
||
init?(directive: Directive) { | ||
guard directive.name == CompilationResult.Constants.DirectiveNames.Import else { | ||
return nil | ||
} | ||
guard let moduleArgument = directive.arguments?.first( | ||
where: { $0.name == Constants.ArgumentNames.Module }), | ||
case let .string(moduleValue) = moduleArgument.value | ||
else { | ||
return nil | ||
} | ||
moduleName = moduleValue | ||
} | ||
|
||
var debugDescription: String { | ||
return "@import(module: \"\(moduleName)\")" | ||
} | ||
} | ||
} | ||
|
||
fileprivate protocol Deferrable { } | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
OrderedSet was not Sendable (resulting in a warning) in version 1.0.4 so i updated collections to 1.0.6 as it was fixed there.
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.
That's great. If we need that patch version in order for this to compile correctly, then we should probably make it the minimum version number in the
Package.swift
file too.