Skip to content

Commit

Permalink
Feature: Schema Type Renaming (apollographql/apollo-ios-dev#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobaFetters authored and gh-action-runner committed Jun 13, 2024
1 parent 54e956a commit d4eaa54
Show file tree
Hide file tree
Showing 33 changed files with 636 additions and 237 deletions.
97 changes: 82 additions & 15 deletions Sources/ApolloCodegenLib/ApolloCodegen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ public class ApolloCodegen {
try config.validateConfigValues()

let compilationResult = try await compileGraphQLResult()

try config.validate(compilationResult)

let ir = IRBuilder(compilationResult: compilationResult)

processSchemaCustomizations(ir: ir)

try await withThrowingTaskGroup(of: Void.self) { group in
if itemsToGenerate.contains(.operationManifest) {
Expand Down Expand Up @@ -321,6 +322,72 @@ public class ApolloCodegen {
}
}
}

func processSchemaCustomizations(ir: IRBuilder) {
for (name, customization) in config.options.schemaCustomization.customTypeNames {
if let type = ir.schema.referencedTypes.allTypes.first(where: { $0.name.schemaName == name }) {
if type is GraphQLObjectType ||
type is GraphQLInterfaceType ||
type is GraphQLUnionType {
switch customization {
case .type(let name):
type.name.customName = name
default:
break
}
} else if let scalarType = type as? GraphQLScalarType {
guard scalarType.isCustomScalar else {
return
}

switch customization {
case .type(let name):
type.name.customName = name
default:
break
}
} else if let enumType = type as? GraphQLEnumType {
switch customization {
case .type(let name):
enumType.name.customName = name
break
case .enum(let name, let cases):
enumType.name.customName = name

if let cases = cases {
for value in enumType.values {
if let caseName = cases[value.name.schemaName] {
value.name.customName = caseName
}
}
}
break
default:
break
}
} else if let inputObjectType = type as? GraphQLInputObjectType {
switch customization {
case .type(let name):
inputObjectType.name.customName = name
break
case .inputObject(let name, let fields):
inputObjectType.name.customName = name

if let fields = fields {
for (_, field) in inputObjectType.fields {
if let fieldName = fields[field.name.schemaName] {
field.name.customName = fieldName
}
}
}
break
default:
break
}
}
}
}
}

/// Generates the schema types and schema metadata files for the `ir`'s compiled schema.
private func generateSchemaFiles(
Expand All @@ -333,21 +400,21 @@ public class ApolloCodegen {

nonFatalErrors.merge(
try await nonFatalErrorCollectingTaskGroup() { group in
for graphQLObject in ir.schema.referencedTypes.objects {
for graphqlObject in ir.schema.referencedTypes.objects {
addFileGenerationTask(
for: ObjectFileGenerator(
graphqlObject: graphQLObject,
graphqlObject: graphqlObject,
config: config
),
to: &group,
fileManager: fileManager
)

if config.output.testMocks != .none {
let fields = await ir.fieldCollector.collectedFields(for: graphQLObject)
let fields = await ir.fieldCollector.collectedFields(for: graphqlObject)
addFileGenerationTask(
for: MockObjectFileGenerator(
graphqlObject: graphQLObject,
graphqlObject: graphqlObject,
fields: fields,
ir: ir,
config: config
Expand All @@ -362,10 +429,10 @@ public class ApolloCodegen {

nonFatalErrors.merge(
try await nonFatalErrorCollectingTaskGroup() { group in
for graphQLEnum in ir.schema.referencedTypes.enums {
for graphqlEnum in ir.schema.referencedTypes.enums {
addFileGenerationTask(
for: EnumFileGenerator(
graphqlEnum: graphQLEnum,
graphqlEnum: graphqlEnum,
config: config
),
to: &group,
Expand All @@ -378,10 +445,10 @@ public class ApolloCodegen {

nonFatalErrors.merge(
try await nonFatalErrorCollectingTaskGroup() { group in
for graphQLInterface in ir.schema.referencedTypes.interfaces {
for graphqlInterface in ir.schema.referencedTypes.interfaces {
addFileGenerationTask(
for: InterfaceFileGenerator(
graphqlInterface: graphQLInterface,
graphqlInterface: graphqlInterface,
config: config
),
to: &group,
Expand All @@ -393,10 +460,10 @@ public class ApolloCodegen {

nonFatalErrors.merge(
try await nonFatalErrorCollectingTaskGroup() { group in
for graphQLUnion in ir.schema.referencedTypes.unions {
for graphqlUnion in ir.schema.referencedTypes.unions {
addFileGenerationTask(
for: UnionFileGenerator(
graphqlUnion: graphQLUnion,
graphqlUnion: graphqlUnion,
config: config
),
to: &group,
Expand All @@ -408,10 +475,10 @@ public class ApolloCodegen {

nonFatalErrors.merge(
try await nonFatalErrorCollectingTaskGroup() { group in
for graphQLInputObject in ir.schema.referencedTypes.inputObjects {
for graphqlInputObject in ir.schema.referencedTypes.inputObjects {
addFileGenerationTask(
for: InputObjectFileGenerator(
graphqlInputObject: graphQLInputObject,
graphqlInputObject: graphqlInputObject,
config: config
),
to: &group,
Expand All @@ -423,10 +490,10 @@ public class ApolloCodegen {

nonFatalErrors.merge(
try await nonFatalErrorCollectingTaskGroup() { group in
for graphQLScalar in ir.schema.referencedTypes.customScalars {
for graphqlScalar in ir.schema.referencedTypes.customScalars {
addFileGenerationTask(
for: CustomScalarFileGenerator(
graphqlScalar: graphQLScalar,
graphqlScalar: graphqlScalar,
config: config
),
to: &group, fileManager: fileManager
Expand Down
14 changes: 14 additions & 0 deletions Sources/ApolloCodegenLib/ApolloCodegenConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
public let selectionSetInitializers: SelectionSetInitializers
/// How to generate the operation documents for your generated operations.
public let operationDocumentFormat: OperationDocumentFormat
/// Customization options to be applie to the schema during code generation.
public let schemaCustomization: SchemaCustomization
/// Generate import statements that are compatible with including `Apollo` via Cocoapods.
///
/// Cocoapods bundles all files from subspecs into the main target for a pod. This means that
Expand Down Expand Up @@ -514,6 +516,7 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
public static let schemaDocumentation: Composition = .include
public static let selectionSetInitializers: SelectionSetInitializers = [.localCacheMutations]
public static let operationDocumentFormat: OperationDocumentFormat = .definition
public static let schemaCustomization: SchemaCustomization = .init()
public static let cocoapodsCompatibleImportStatements: Bool = false
public static let warningsOnDeprecatedUsage: Composition = .include
public static let conversionStrategies: ConversionStrategies = .init()
Expand Down Expand Up @@ -546,6 +549,7 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
schemaDocumentation: Composition = Default.schemaDocumentation,
selectionSetInitializers: SelectionSetInitializers = Default.selectionSetInitializers,
operationDocumentFormat: OperationDocumentFormat = Default.operationDocumentFormat,
schemaCustomization: SchemaCustomization = Default.schemaCustomization,
cocoapodsCompatibleImportStatements: Bool = Default.cocoapodsCompatibleImportStatements,
warningsOnDeprecatedUsage: Composition = Default.warningsOnDeprecatedUsage,
conversionStrategies: ConversionStrategies = Default.conversionStrategies,
Expand All @@ -557,6 +561,7 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
self.schemaDocumentation = schemaDocumentation
self.selectionSetInitializers = selectionSetInitializers
self.operationDocumentFormat = operationDocumentFormat
self.schemaCustomization = schemaCustomization
self.cocoapodsCompatibleImportStatements = cocoapodsCompatibleImportStatements
self.warningsOnDeprecatedUsage = warningsOnDeprecatedUsage
self.conversionStrategies = conversionStrategies
Expand All @@ -574,6 +579,7 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
case selectionSetInitializers
case apqs
case operationDocumentFormat
case schemaCustomization
case cocoapodsCompatibleImportStatements
case warningsOnDeprecatedUsage
case conversionStrategies
Expand Down Expand Up @@ -614,6 +620,11 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
forKey: .apqs
)?.operationDocumentFormat ??
Default.operationDocumentFormat

schemaCustomization = try values.decodeIfPresent(
SchemaCustomization.self,
forKey: .schemaCustomization
) ?? Default.schemaCustomization

cocoapodsCompatibleImportStatements = try values.decodeIfPresent(
Bool.self,
Expand Down Expand Up @@ -649,6 +660,7 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
try container.encode(self.schemaDocumentation, forKey: .schemaDocumentation)
try container.encode(self.selectionSetInitializers, forKey: .selectionSetInitializers)
try container.encode(self.operationDocumentFormat, forKey: .operationDocumentFormat)
try container.encode(self.schemaCustomization, forKey: .schemaCustomization)
try container.encode(self.cocoapodsCompatibleImportStatements, forKey: .cocoapodsCompatibleImportStatements)
try container.encode(self.warningsOnDeprecatedUsage, forKey: .warningsOnDeprecatedUsage)
try container.encode(self.conversionStrategies, forKey: .conversionStrategies)
Expand Down Expand Up @@ -1368,6 +1380,7 @@ extension ApolloCodegenConfiguration.OutputOptions {
self.conversionStrategies = conversionStrategies
self.pruneGeneratedFiles = pruneGeneratedFiles
self.markOperationDefinitionsAsFinal = markOperationDefinitionsAsFinal
self.schemaCustomization = Default.schemaCustomization
}

/// Deprecated initializer.
Expand Down Expand Up @@ -1417,6 +1430,7 @@ extension ApolloCodegenConfiguration.OutputOptions {
self.conversionStrategies = conversionStrategies
self.pruneGeneratedFiles = pruneGeneratedFiles
self.markOperationDefinitionsAsFinal = markOperationDefinitionsAsFinal
self.schemaCustomization = Default.schemaCustomization
}

/// Whether the generated operations should use Automatic Persisted Queries.
Expand Down
Loading

0 comments on commit d4eaa54

Please sign in to comment.