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

Foundational Work For Codegen Rewrite #1817

Merged
merged 13 commits into from
Jun 10, 2021
Merged

Foundational Work For Codegen Rewrite #1817

merged 13 commits into from
Jun 10, 2021

Conversation

AnthonyMDev
Copy link
Contributor

There is a lot in this PR, so bear with me. This makes a few changes to start enabling the work on the Codegen rewrite.

  • Renames ApolloCore to ApolloUtils
    • This library will be imported by Apollo and ApolloCodegenLib. It provides common utility functionality that can be helpful for any of our code.
    • In the future, more of the files in the Utilities folder of Apollo will likely be moved here.
  • Adds the new ApolloModels library.
    • This library will be imported by Apollo and by any generated models that are generated using the new codegen.
    • Contains protocols, model structs, and enums that enable generated code to access data concisely.
    • I'm not sure if I'm happy with the name of this library currently. Looking for suggestions.
  • Includes these 2 new libraries for use with SPM and Cocoapods
    • Need to ensure this works with Carthage currently. (I'll need your help there @designatednerd)
    • Cocoapods and SPM will exclude the files in the ApolloModels/CodegenV1 folder. These are needed for future development work, but don't need to be included in distributed library until version 1.0.0 with the new Codegen.
  • Renamed Reference to CacheReference
    • The name was too vague and confused me.
    • Open to other suggestions here. Maybe EntityReference?
  • InputValue refactored
    • No longer uses JSONEncodable directly.
      • I wanted JSONEncodable to be in Apollo, but InputValue needs to be in ApolloModels So the evaluation functions were moved into an extension that is in Apollo.
      • This evaluation now checks if scalar values conform to JSONEncodable and if they don't (they always should though), it throws an error.
  • Other Minor Improvements/Clean Up
    • Removed custom Dictionary initializer helper function in favor of using standard library function Dictionary(_ elements: uniquingKeysWith:)
    • JSONValueMatcher to wrap the equals(_: Any, _: Any) -> Bool function. You now need to explicitly call JSONValueMatcher.equals(x, y) and we get that function out of the global namespace.
    • Matchable moved into ApolloTestSupport
      • It is only being used for JSONDecodingError and only for test matchers. Moved the extension JSONDecodingError: Matchable into ApolloTestSupport as well.
    • Replaced some unneeded code for transforming dictionaries during execution with use of compactMapValues.
    • Improved OperationMessage by making the message property a let and computing it once in the initializer.

- Setup Package.swift
- Renamed and moved files
- Fixed imports in Apollo
- Refactored call sites
- Renamed Reference to CacheReference
- Use Dictionary(_: uniquingWithKeys:) instead of custom initializer in extension.

TODO:
- Fix call sites and imports in ApolloWebSocket and ApolloSQL
- Cocoapods & Carthage
- Fix call sites in tests
TODO: Fix unneeded import statements and `JSONObject` -> `[String: Any]` conversion
Apollo.xcodeproj/project.pbxproj Outdated Show resolved Hide resolved
Package.swift Outdated Show resolved Hide resolved
Package.swift Outdated Show resolved Hide resolved
Sources/Apollo/Decoding.swift Outdated Show resolved Hide resolved
Sources/Apollo/URLSessionClient.swift Outdated Show resolved Hide resolved
@designatednerd
Copy link
Contributor

Looks like there's a bunch of compilation errors on the codegen tests, I think you can probably find those pretty quick if you compile locally.

I'll have to take a look at Carthage stuff separately, but as long as it compiles locally since carthage uses the underlying xcode project to compile, theoretically it should work correctly.

import Foundation

/// A reference to a cache record.
public struct CacheReference {
Copy link
Contributor

Choose a reason for hiding this comment

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

Apollo Android uses CacheReference too. I'd be in for renaming to EntityReference as we're still at a moment where we can rename stuff on Android. Feels like that would be more adequate. It's a reference to an Entity, not to a Cache. Or maybe ObjectReference ? (We don't have entity anywhere in the Android codebase)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@designatednerd What's your opinion on the naming of this? Entity, Object, something else?

Copy link
Contributor

Choose a reason for hiding this comment

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

@martinbonnin Is your point that it's a reference to an entity within the cache rather than to the cache itself? I think that's valid, but Entity by itself is so overloaded I might go with something like CacheEntityReference.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is your point that it's a reference to an entity within the cache rather than to the cache itself?

Yes it was! Rereading that, I'm certainly overthinking this. CacheReference sounds reasonable.
And now back to overthinking this... What's the difference between a CacheReference and a CacheKey ? Could we merge both somehow?

Copy link
Contributor

Choose a reason for hiding this comment

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

My recollection is that the key is the key used to access stored data vs the reference is the actual stored data - @AnthonyMDev is that about right?

Copy link
Contributor

@martinbonnin martinbonnin Jun 8, 2021

Choose a reason for hiding this comment

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

I got curious and tried to merge CacheKey and CacheReference. Turns out it's working well: apollographql/apollo-kotlin#3157

The only reason I can think of having two different data structures in Kotlin is if we want to make CacheKey a value class that is inlined to a String for performance reasons. If we do that, we can't check the type of CacheKey anymore which means everything becomes a string and we can't serialize the CacheKey to their special String value.

But besides that (which we don't optimize at the moment and I feel should be an implementation details if we ever decide to) I actually like the reduced mental load and API surface.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmmmm. I'm going to look into this. Thanks for that @martijnwalraven

My recollection is that the key is the key used to access stored data vs the reference is the actual stored data - @AnthonyMDev is that about right?

No, the reference isn't the actual stored data. It's just a wrapper type for the CacheKey as a String. It indicates that the raw String data isn't just a regular raw a String property on the object, but is a String representing the reference to the cache key of another object.

Copy link
Contributor

Choose a reason for hiding this comment

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

Gooooot it, thank you for clarifying

@AnthonyMDev AnthonyMDev added this to the June 2021 milestone Jun 10, 2021
@AnthonyMDev AnthonyMDev self-assigned this Jun 10, 2021
@AnthonyMDev AnthonyMDev merged commit 4e2a495 into main Jun 10, 2021
@AnthonyMDev AnthonyMDev deleted the Apollo-Models branch June 10, 2021 19:39
@hwillson hwillson removed this from the MM-2021-06 milestone Jul 29, 2021
@calvincestari calvincestari added the codegen Issues related to or arising from code generation label Jan 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
codegen Issues related to or arising from code generation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants