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

Refactor FragmentProtocols to remove HasFragments #2071

Merged
merged 3 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ let package = Package(
dependencies: [
.package(
url: "https://github.com/stephencelis/SQLite.swift.git",
.upToNextMinor(from: "0.13.1"))
.upToNextMinor(from: "0.13.1")),
Copy link
Member

Choose a reason for hiding this comment

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

I was wondering how this was working but then realized that this is the 1.0 branch where main works fine without the , because there is no next package. This highlights the importance of making sure the package manager samples work with this branch prior to an alpha release.

.package(
url: "https://github.com/mattt/InflectorKit",
.upToNextMinor(from: "1.0.0")),
Expand Down Expand Up @@ -81,7 +81,6 @@ let package = Package(
],
resources: [
.copy("Frontend/dist/ApolloCodegenFrontend.bundle.js"),
.copy("Frontend/dist/ApolloCodegenFrontend.bundle.js.map")
]),
.target(
name: "ApolloSQLite",
Expand Down
2 changes: 1 addition & 1 deletion Sources/Apollo/GraphQLSelectionSetMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ final class GraphQLSelectionSetMapper<SelectionSet: AnySelectionSet>: GraphQLRes
}

func finish(rootValue: JSONObject) -> SelectionSet {
return SelectionSet.init(data: ResponseDict(rootValue))
return SelectionSet.init(data: DataDict(rootValue))
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// A structure that wraps the underlying data dictionary used by `SelectionSet`s.
public struct ResponseDict {
public struct DataDict {

let data: JSONObject

Expand Down Expand Up @@ -41,21 +41,21 @@ public struct ResponseDict {

public subscript<T: AnySelectionSet>(_ key: String) -> T {
let objectData = data[key] as! JSONObject
return T.init(data: ResponseDict(objectData))
return T.init(data: DataDict(objectData))
}

public subscript<T: AnySelectionSet>(_ key: String) -> T? {
guard let objectData = data[key] as? JSONObject else { return nil }
return T.init(data: ResponseDict(objectData))
return T.init(data: DataDict(objectData))
}

public subscript<T: AnySelectionSet>(_ key: String) -> [T] {
let objectData = data[key] as! [JSONObject]
return objectData.map { T.init(data: ResponseDict($0)) }
return objectData.map { T.init(data: DataDict($0)) }
}

public subscript<T: AnySelectionSet>(_ key: String) -> [T]? {
guard let objectData = data[key] as? [JSONObject] else { return nil }
return objectData.map { T.init(data: ResponseDict($0)) }
return objectData.map { T.init(data: DataDict($0)) }
}
}
37 changes: 22 additions & 15 deletions Sources/ApolloAPI/FragmentProtocols.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
// MARK: - Fragment

/// A protocol representing a fragment that a `ResponseObject` object may be converted to.
/// A protocol representing a fragment that a `SelectionSet` object may be converted to.
///
/// A `ResponseObject` that conforms to `HasFragments` can be converted to
/// any `Fragment` included in it's `Fragments` object via its `fragments` property.
///
/// - SeeAlso: `HasFragments`, `ToFragments`
/// A `SelectionSet` can be converted to any `Fragment` included in it's `Fragments` object via
/// its `fragments` property.
public protocol Fragment: AnySelectionSet {
static var fragmentDefinition: String { get }
}

// MARK: - HasFragments

/// A protocol that a `ResponseObject` that contains fragments should conform to.
public protocol HasFragments: AnySelectionSet {
public protocol FragmentContainer {
var data: DataDict { get }

/// A type representing all of the fragments contained on the `ResponseObject`.
associatedtype Fragments: ResponseObject
init(data: DataDict)
}

public extension HasFragments {
/// A `FieldData` object that contains accessors for all of the fragments
/// the object can be converted to.
var fragments: Fragments { Fragments(data: data) }
public extension FragmentContainer {

/// Converts a `SelectionSet` to a `Fragment` given a generic fragment type.
///
/// - Warning: This function is not supported for use outside of generated call sites.
/// Generated call sites are guaranteed by the GraphQL compiler to be safe.
/// Unsupported usage may result in unintended consequences including crashes.
#warning("TODO: Audit all _ prefixed things to see if they should be available using ApolloExtension.")
func _toFragment<T: Fragment>() -> T {
return T.init(data: data)
}
}

/// A `FragmentContainer` to be used by `SelectionSet`s that have no fragments.
/// This is the default `FragmentContainer` for a `SelectionSet` that does not specify a
/// `Fragments` type.
public enum NoFragments {}
26 changes: 0 additions & 26 deletions Sources/ApolloAPI/ResponseObject.swift

This file was deleted.

16 changes: 15 additions & 1 deletion Sources/ApolloAPI/SelectionSet.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MARK: - Type Erased SelectionSets

public protocol AnySelectionSet: ResponseObject {
public protocol AnySelectionSet {
static var schema: SchemaConfiguration.Type { get }

static var selections: [Selection] { get }
Expand All @@ -9,6 +9,10 @@ public protocol AnySelectionSet: ResponseObject {
///
/// This may be a concrete type (`Object`) or an abstract type (`Interface`, or `Union`).
static var __parentType: ParentType { get }

var data: DataDict { get }

init(data: DataDict)
}

public extension AnySelectionSet {
Expand Down Expand Up @@ -48,6 +52,11 @@ public protocol TypeCase: AnySelectionSet { }
// MARK: - SelectionSet
public protocol SelectionSet: AnySelectionSet {
associatedtype Schema: SchemaConfiguration

/// A type representing all of the fragments the `SelectionSet` can be converted to.
/// Defaults to a stub type with no fragments.
/// A `SelectionSet` with fragments should provide a type that conforms to `FragmentContainer`
associatedtype Fragments = NoFragments
}

extension SelectionSet {
Expand All @@ -71,3 +80,8 @@ extension SelectionSet {
return T.init(data: data)
}
}

extension SelectionSet where Fragments: FragmentContainer {
/// Contains accessors for all of the fragments the `SelectionSet` can be converted to.
var fragments: Fragments { Fragments(data: data) }
}
60 changes: 0 additions & 60 deletions Sources/ApolloCodegenLib/CLIDownloader.swift

This file was deleted.

145 changes: 0 additions & 145 deletions Sources/ApolloCodegenLib/CLIExtractor.swift

This file was deleted.

Loading