Skip to content

Commit

Permalink
Cleanup and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMDev committed May 30, 2023
1 parent 0cd8668 commit a929f97
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 24 deletions.
3 changes: 1 addition & 2 deletions Sources/ApolloCodegenLib/IR/IR+NamedFragmentBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ extension IR {
)

let rootEntity = Entity(
source: .namedFragment(fragmentDefinition),
rootTypePath: LinkedList(fragmentDefinition.type)
source: .namedFragment(fragmentDefinition)
)

let result = RootFieldBuilder.buildRootEntityField(
Expand Down
3 changes: 1 addition & 2 deletions Sources/ApolloCodegenLib/IR/IR+OperationBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ extension IR {
)

let rootEntity = Entity(
source: .operation(operationDefinition),
rootTypePath: LinkedList(operationDefinition.rootType)
source: .operation(operationDefinition)
)

let result = RootFieldBuilder.buildRootEntityField(
Expand Down
43 changes: 31 additions & 12 deletions Sources/ApolloCodegenLib/IR/IR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,19 @@ class IR {
/// Multiple `SelectionSet`s may select fields on the same `Entity`. All `SelectionSet`s that will
/// be selected on the same object share the same `Entity`.
class Entity {

/// Represents the location within a GraphQL definition (operation or fragment) of an `Entity`.
struct Location: Hashable {
enum SourceDefinition: Hashable {
case operation(CompilationResult.OperationDefinition)
case namedFragment(CompilationResult.FragmentDefinition)

var rootType: GraphQLCompositeType {
switch self {
case let .operation(definition): return definition.rootType
case let .namedFragment(definition): return definition.type
}
}
}

struct FieldComponent: Hashable {
Expand All @@ -77,6 +86,23 @@ class IR {

/// The operation or fragment definition that the entity belongs to.
let source: SourceDefinition

/// The path of fields from the root of the ``source`` definition to the entity.
///
/// Example:
/// For an operation:
/// ```graphql
/// query MyQuery {
/// allAnimals {
/// predators {
/// height {
/// ...
/// }
/// }
/// }
/// }
/// ```
/// The `Height` entity would have a field path of [allAnimals, predators, height].
let fieldPath: FieldPath?

func appending(_ fieldComponent: FieldComponent) -> Location {
Expand All @@ -97,25 +123,18 @@ class IR {
/// The selections that are selected for the entity across all type scopes in the operation.
/// Represented as a tree.
let selectionTree: EntitySelectionTree

#warning("TODO: udpate docs")
/// A list of path components indicating the path to the field containing the `Entity` in
/// an operation or fragment.

/// The location within a GraphQL definition (operation or fragment) where the `Entity` is
/// located.
let location: Location

var rootTypePath: LinkedList<GraphQLCompositeType> { selectionTree.rootTypePath }

var rootType: GraphQLCompositeType { rootTypePath.last.value }

#warning("TODO: infer rootTypePath and remove param")
init(
source: Location.SourceDefinition,
rootTypePath: LinkedList<GraphQLCompositeType>
// fieldPath: FieldPath?
) {
init(source: Location.SourceDefinition) {
self.location = .init(source: source, fieldPath: nil)
self.selectionTree = EntitySelectionTree(rootTypePath: rootTypePath)
// self.fieldPath = fieldPath
self.selectionTree = EntitySelectionTree(rootTypePath: LinkedList(source.rootType))
}

init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ class IRRootFieldBuilderTests: XCTestCase {
type: .nonNull(.entity(operation.rootType)),
selectionSet: operation.selectionSet
),
onRootEntity: IR.Entity(
source: .operation(operation),
rootTypePath: LinkedList(operation.rootType)
),
onRootEntity: IR.Entity(source: .operation(operation)),
inIR: ir
)
subject = result.rootField
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ class IRSelectionSet_IncludeSkip_Tests: XCTestCase {
type: .nonNull(.entity(operation.rootType)),
selectionSet: operation.selectionSet
),
onRootEntity: IR.Entity(
source: .operation(operation),
rootTypePath: LinkedList(operation.rootType)
),
onRootEntity: IR.Entity(source: .operation(operation)),
inIR: ir
)
subject = result.rootField
Expand Down

0 comments on commit a929f97

Please sign in to comment.