Skip to content

Commit

Permalink
Updating Persisted Query Configuration (#3175)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Miller <[email protected]>
Co-authored-by: Calvin Cestari <[email protected]>
  • Loading branch information
3 people authored Aug 9, 2023
1 parent 4147f78 commit e21116a
Show file tree
Hide file tree
Showing 21 changed files with 427 additions and 335 deletions.
12 changes: 12 additions & 0 deletions Apollo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
54DDB0921EA045870009DD99 /* InMemoryNormalizedCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54DDB0911EA045870009DD99 /* InMemoryNormalizedCache.swift */; };
5AC6CA4322AAF7B200B7C94D /* GraphQLHTTPMethod.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5AC6CA4222AAF7B200B7C94D /* GraphQLHTTPMethod.swift */; };
5BB2C0232380836100774170 /* VersionNumberTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BB2C0222380836100774170 /* VersionNumberTests.swift */; };
6608B3362A7D402B006FB655 /* ApolloCodegenConfiguration+OperationManifestConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6608B3342A7D3FF5006FB655 /* ApolloCodegenConfiguration+OperationManifestConfiguration.swift */; };
662EA65E2A701483008A1931 /* GenerateOperationManifest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662EA65D2A701483008A1931 /* GenerateOperationManifest.swift */; };
662EA6602A705BD7008A1931 /* GenerateOperationManifestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662EA65F2A705BD7008A1931 /* GenerateOperationManifestTests.swift */; };
66321AE72A126C4400CC35CB /* IR+Formatting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66321AE62A126C4400CC35CB /* IR+Formatting.swift */; };
Expand Down Expand Up @@ -1140,6 +1141,7 @@
54DDB0911EA045870009DD99 /* InMemoryNormalizedCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InMemoryNormalizedCache.swift; sourceTree = "<group>"; };
5AC6CA4222AAF7B200B7C94D /* GraphQLHTTPMethod.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GraphQLHTTPMethod.swift; sourceTree = "<group>"; };
5BB2C0222380836100774170 /* VersionNumberTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VersionNumberTests.swift; sourceTree = "<group>"; };
6608B3342A7D3FF5006FB655 /* ApolloCodegenConfiguration+OperationManifestConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ApolloCodegenConfiguration+OperationManifestConfiguration.swift"; sourceTree = "<group>"; };
662EA65D2A701483008A1931 /* GenerateOperationManifest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GenerateOperationManifest.swift; sourceTree = "<group>"; };
662EA65F2A705BD7008A1931 /* GenerateOperationManifestTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GenerateOperationManifestTests.swift; sourceTree = "<group>"; };
66321AE62A126C4400CC35CB /* IR+Formatting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "IR+Formatting.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2206,6 +2208,14 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
6608B3332A7D3FCB006FB655 /* CodegenConfiguration */ = {
isa = PBXGroup;
children = (
6608B3342A7D3FF5006FB655 /* ApolloCodegenConfiguration+OperationManifestConfiguration.swift */,
);
path = CodegenConfiguration;
sourceTree = "<group>";
};
66B18E862A15366400525DFB /* Network */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -2548,6 +2558,7 @@
9BD681332405F6BB000874CB /* Codegen */ = {
isa = PBXGroup;
children = (
6608B3332A7D3FCB006FB655 /* CodegenConfiguration */,
9B7B6F57233C287100F32205 /* ApolloCodegen.swift */,
9B7B6F58233C287100F32205 /* ApolloCodegenConfiguration.swift */,
E674DB40274C0A9B009BB90E /* Glob.swift */,
Expand Down Expand Up @@ -5053,6 +5064,7 @@
E6203342284F1C9600A291D1 /* MockUnionsFileGenerator.swift in Sources */,
DE6D07F927BC3B6D009F5F33 /* GraphQLInputField+Rendered.swift in Sources */,
9F1A966C258F34BB00A06EEB /* GraphQLSchema.swift in Sources */,
6608B3362A7D402B006FB655 /* ApolloCodegenConfiguration+OperationManifestConfiguration.swift in Sources */,
9BE74D3D23FB4A8E006D354F /* FileFinder.swift in Sources */,
E64F7EBC27A11A510059C021 /* GraphQLNamedType+SwiftName.swift in Sources */,
9B7B6F59233C287200F32205 /* ApolloCodegen.swift in Sources */,
Expand Down
114 changes: 61 additions & 53 deletions Sources/ApolloCodegenLib/ApolloCodegen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ public class ApolloCodegen {
}
}
}

public struct ItemsToGenerate: OptionSet {
public var rawValue: Int

public static let code = ItemsToGenerate(rawValue: 1 << 0)
public static let operationManifest = ItemsToGenerate(rawValue: 1 << 1)
public static let all: ItemsToGenerate = [
.code,
.operationManifest
]

public init(rawValue: Int) {
self.rawValue = rawValue
}

}

/// Executes the code generation engine with a specified configuration.
///
Expand All @@ -77,15 +93,17 @@ public class ApolloCodegen {
/// If `nil`, the current working directory of the executing process will be used.
public static func build(
with configuration: ApolloCodegenConfiguration,
withRootURL rootURL: URL? = nil
withRootURL rootURL: URL? = nil,
itemsToGenerate: ItemsToGenerate = [.code]
) throws {
try build(with: configuration, rootURL: rootURL)
try build(with: configuration, rootURL: rootURL, itemsToGenerate: itemsToGenerate)
}

internal static func build(
with configuration: ApolloCodegenConfiguration,
rootURL: URL? = nil,
fileManager: ApolloFileManager = .default
fileManager: ApolloFileManager = .default,
itemsToGenerate: ItemsToGenerate
) throws {

let configContext = ConfigurationContext(
Expand All @@ -103,58 +121,43 @@ public class ApolloCodegen {
try validate(configContext, with: compilationResult)

let ir = IR(compilationResult: compilationResult)

if itemsToGenerate == .operationManifest {
var operationIDsFileGenerator = OperationManifestFileGenerator(config: configContext)

for operation in compilationResult.operations {
autoreleasepool {
let irOperation = ir.build(operation: operation)
operationIDsFileGenerator?.collectOperationIdentifier(irOperation)
}
}

try operationIDsFileGenerator?.generate(fileManager: fileManager)
}

var existingGeneratedFilePaths = configuration.options.pruneGeneratedFiles ?
try findExistingGeneratedFilePaths(
config: configContext,
fileManager: fileManager
) : []

try generateFiles(
compilationResult: compilationResult,
ir: ir,
config: configContext,
fileManager: fileManager
)
if itemsToGenerate.contains(.code) {
var existingGeneratedFilePaths = configuration.options.pruneGeneratedFiles ?
try findExistingGeneratedFilePaths(
config: configContext,
fileManager: fileManager
) : []

if configuration.options.pruneGeneratedFiles {
try deleteExtraneousGeneratedFiles(
from: &existingGeneratedFilePaths,
afterCodeGenerationUsing: fileManager
try generateFiles(
compilationResult: compilationResult,
ir: ir,
config: configContext,
fileManager: fileManager,
itemsToGenerate: itemsToGenerate
)

if configuration.options.pruneGeneratedFiles {
try deleteExtraneousGeneratedFiles(
from: &existingGeneratedFilePaths,
afterCodeGenerationUsing: fileManager
)
}
}
}

public static func generateOperationManifest(
with configuration: ApolloCodegenConfiguration,
withRootURL rootURL: URL? = nil,
fileManager: ApolloFileManager = .default
) throws {
let configContext = ConfigurationContext(
config: configuration,
rootURL: rootURL
)

try validate(configContext)

let compilationResult = try compileGraphQLResult(
configContext,
experimentalFeatures: configuration.experimentalFeatures
)

try validate(configContext, with: compilationResult)

let ir = IR(compilationResult: compilationResult)

var operationIDsFileGenerator = OperationManifestFileGenerator(config: configContext)

for operation in compilationResult.operations {
let irOperation = ir.build(operation: operation)
operationIDsFileGenerator?.collectOperationIdentifier(irOperation)
}

try operationIDsFileGenerator?.generate(fileManager: fileManager)
}

// MARK: Internal

Expand Down Expand Up @@ -411,7 +414,8 @@ public class ApolloCodegen {
compilationResult: CompilationResult,
ir: IR,
config: ConfigurationContext,
fileManager: ApolloFileManager = .default
fileManager: ApolloFileManager = .default,
itemsToGenerate: ItemsToGenerate
) throws {
for fragment in compilationResult.fragments {
try autoreleasepool {
Expand All @@ -431,11 +435,15 @@ public class ApolloCodegen {
try OperationFileGenerator(irOperation: irOperation, config: config)
.generate(forConfig: config, fileManager: fileManager)

operationIDsFileGenerator?.collectOperationIdentifier(irOperation)
if itemsToGenerate.contains(.operationManifest) {
operationIDsFileGenerator?.collectOperationIdentifier(irOperation)
}
}
}

try operationIDsFileGenerator?.generate(fileManager: fileManager)
if itemsToGenerate.contains(.operationManifest) {
try operationIDsFileGenerator?.generate(fileManager: fileManager)
}
operationIDsFileGenerator = nil

for graphQLObject in ir.schema.referencedTypes.objects {
Expand Down
Loading

0 comments on commit e21116a

Please sign in to comment.