Skip to content

Commit

Permalink
Squashed 'apollo-ios-codegen/' changes from 10a0738e..8e730238
Browse files Browse the repository at this point in the history
8e730238 Alphabetize fragment ordering in source text (#130)

git-subtree-dir: apollo-ios-codegen
git-subtree-split: 8e7302381016d6b1422d3fd843766c84c5516cf1
  • Loading branch information
gh-action-runner authored and gh-action-runner committed Nov 9, 2023
1 parent 0c32a82 commit ba1283f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
50 changes: 26 additions & 24 deletions Sources/ApolloCodegenLib/OperationDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,38 @@ public struct OperationDescriptor: Sendable {
}

func sourceText(withFormat format: SourceFormat) -> String {
format.formatted(underlyingDefinition)
format.formatted(self)
}


fileprivate var allReferencedFragments: [CompilationResult.FragmentDefinition] {
func insertAllFragments(
from fragment: CompilationResult.FragmentDefinition,
into set: inout Set<CompilationResult.FragmentDefinition>
) {
set.insert(fragment)
for referencedFragment in fragment.referencedFragments {
insertAllFragments(from: referencedFragment, into: &set)
}
}

var fragmentSet = Set<CompilationResult.FragmentDefinition>()
for fragment in underlyingDefinition.referencedFragments {
insertAllFragments(from: fragment, into: &fragmentSet)
}
return fragmentSet.sorted { $0.name < $1.name }
}

}

// MARK: - Formatting

fileprivate extension OperationDescriptor.SourceFormat {
func formatted(_ operation: CompilationResult.OperationDefinition) -> String {
var source = operation.source.convertedToSingleLine()
var set = Set<String>()
append(
to: &source,
set: &set,
fragments: operation.referencedFragments
)
func formatted(_ operation: OperationDescriptor) -> String {
var source = operation.underlyingDefinition.source.convertedToSingleLine()
for fragment in operation.allReferencedFragments {
source += formatted(fragment)
}

switch self {
case .rawSource:
return source
Expand All @@ -86,20 +102,6 @@ fileprivate extension OperationDescriptor.SourceFormat {
}
}

private func append(
to source: inout String,
set: inout Set<String>,
fragments: [CompilationResult.FragmentDefinition]
) {
for fragment in fragments {
if !set.contains(fragment.name) {
set.insert(fragment.name)
source += formatted(fragment)
append(to: &source, set: &set, fragments: fragment.referencedFragments)
}
}
}

private func formatted(_ fragment: CompilationResult.FragmentDefinition) -> String {
switch self {
case .rawSource:
Expand Down
4 changes: 4 additions & 0 deletions Sources/IR/IR+RootFieldBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ class RootFieldBuilder {
from: rootSelectionSet
)

referencedFragments.sort(by: {
$0.name < $1.name
})

return Result(
rootField: EntityField(rootField, selectionSet: rootIrSelectionSet),
referencedFragments: referencedFragments,
Expand Down

0 comments on commit ba1283f

Please sign in to comment.