Skip to content
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

Conversation

hemel
Copy link

@hemel hemel commented Jan 11, 2024

This PR handles the new import directive.
If a graphql operation adds this directive, an import statement to the corresponding generated source file and the source of its referenced fragments will be added.

@hemel hemel requested a review from a team as a code owner January 11, 2024 21:13
Copy link

netlify bot commented Jan 11, 2024

👷 Deploy request for eclectic-pie-88a2ba pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 031dbf2

@hemel hemel changed the title import directive [codegen] import directive Jan 11, 2024
Copy link
Contributor

@AnthonyMDev AnthonyMDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some minor improvements to be made here!

I also don't see any new unit tests for this. If you are unsure how to write the tests, let me know and I'll pull this down and write a few tests.

self.referencedFragments = referencedFragments
self.source = source
self.filePath = filePath
self.isLocalCacheMutation = directives?
.contains { $0.name == Constants.DirectiveNames.LocalCacheMutation } ?? false

let referencedImports: [Directive] = self.referencedFragments
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you're doing here, but I don't think we should be adding the referenced fragment's @import directives to the parent's directives. We should add them to the moduleImports but not the actual directives property.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means moduleImports probably just becomes a stored property instead of being computed.

.flatMap { $0 }
self.directives = Array(Set(directives ?? [] + referencedImports))

self.moduleImports = self.directives?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also wondering if we should alphabetize these so that changing the order of Fragment Spreads doesn't change the order of import statements. That could be confusing when you change something in one Fragment definition and then a bunch of operations have their import statements change.

I see the use of Set when setting up the directives here. But if we are going to not add the referenced fragment imports to the directives, we should probably just make moduleImports an OrderedSet to ensure we don't get duplicate import statements.

self.referencedFragments = .fromJSValue(jsValue["referencedFragments"], bridge: bridge)
self.source = jsValue["source"]
self.filePath = jsValue["filePath"]

let directives: [Directive]? = .fromJSValue(jsValue["directives"], bridge: bridge)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. We should also try to break this into a static function so that we can re-use the logic instead of repeating it in both places.

}

var debugDescription: String {
return "Import \"\(moduleName)\""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return "Import \"\(moduleName)\""
return "@import(module: \"\(moduleName)\")"

@@ -7,7 +7,7 @@ enum TemplateTarget: Equatable {
/// Used in schema types files; enum, input object, union, etc.
case schemaFile(type: SchemaFileType)
/// Used in operation files; query, mutation, fragment, etc.
case operationFile
case operationFile(moduleImports: [String]?)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this have a default value of nil so we don't need to provide moduleImports: nil in all of the unit tests?

I can't remember if you can do default values on enum case associated values? 🤔

@AnthonyMDev AnthonyMDev self-assigned this Jan 16, 2024
Hemel Yahya added 2 commits January 16, 2024 13:56
…les in definitions + do not assing to the definitions directives anymore
…les in definitions + do not assing to the definitions directives anymore
@hemel
Copy link
Author

hemel commented Jan 16, 2024

Just some minor improvements to be made here!

I also don't see any new unit tests for this. If you are unsure how to write the tests, let me know and I'll pull this down and write a few tests.

Hello @AnthonyMDev thank you for reviewing the PR. I applied the changes based on your review. In terms of the tests i was not sure where to add them unfortunately. I would greatly appreciate it if you can add some. Thank you in advance.

…les in definitions + do not assing to the definitions directives anymore
"revision" : "937e904258d22af6e447a0b72c0bc67583ef64a2",
"version" : "1.0.4"
"revision" : "d029d9d39c87bed85b1c50adee7c41795261a192",
"version" : "1.0.6"
Copy link
Author

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.

Copy link
Contributor

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.

@@ -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]?,
Copy link
Author

Choose a reason for hiding this comment

The 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.

Hemel Yahya added 3 commits January 16, 2024 16:41
…les in definitions + do not assing to the definitions directives anymore
…les in definitions + do not assing to the definitions directives anymore
…les in definitions + do not assing to the definitions directives anymore
Copy link
Contributor

@AnthonyMDev AnthonyMDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all looks great! I'm going to leave this PR pending until I can take some time to write the unit tests for it. If you get the opportunity to use this branch with your project, it would be great to get some real-world testing too.

I'll get the tests done and have this shipped for the next release we make. And I'll be sure that's before the end of the month for you!

"revision" : "937e904258d22af6e447a0b72c0bc67583ef64a2",
"version" : "1.0.4"
"revision" : "d029d9d39c87bed85b1c50adee7c41795261a192",
"version" : "1.0.6"
Copy link
Contributor

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.

@hemel
Copy link
Author

hemel commented Jan 17, 2024

Ill double check to see if we can use the fork on our side @AnthonyMDev. Thank you again!

Edit: Codegen on our side works as expected in my branch :). The imports are added to all files generated, related to the operations.

@AnthonyMDev
Copy link
Contributor

I've added the tests and have a new PR up here #245. Closing this PR. Thanks @hemel!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants