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

fix: Use correct schema namespace casing in fragments #2730

Merged
merged 4 commits into from
Dec 14, 2022
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
12 changes: 4 additions & 8 deletions Sources/ApolloCodegenLib/ApolloCodegen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ public class ApolloCodegen {

try validate(schemaName: configContext.schemaName, compilationResult: compilationResult)

let ir = IR(
schemaName: configContext.schemaName,
compilationResult: compilationResult
)
let ir = IR(compilationResult: compilationResult)

var existingGeneratedFilePaths = configuration.options.pruneGeneratedFiles ?
try findExistingGeneratedFilePaths(
Expand Down Expand Up @@ -267,7 +264,7 @@ public class ApolloCodegen {
for fragment in compilationResult.fragments {
try autoreleasepool {
let irFragment = ir.build(fragment: fragment)
try FragmentFileGenerator(irFragment: irFragment, schema: ir.schema, config: config)
try FragmentFileGenerator(irFragment: irFragment, config: config)
.generate(forConfig: config, fileManager: fileManager)
}
}
Expand All @@ -277,7 +274,7 @@ public class ApolloCodegen {
for operation in compilationResult.operations {
try autoreleasepool {
let irOperation = ir.build(operation: operation)
try OperationFileGenerator(irOperation: irOperation, schema: ir.schema, config: config)
try OperationFileGenerator(irOperation: irOperation, config: config)
.generate(forConfig: config, fileManager: fileManager)

operationIDsFileGenerator?.collectOperationIdentifier(irOperation)
Expand Down Expand Up @@ -340,7 +337,6 @@ public class ApolloCodegen {
try autoreleasepool {
try InputObjectFileGenerator(
graphqlInputObject: graphQLInputObject,
schema: ir.schema,
config: config
).generate(
forConfig: config,
Expand Down Expand Up @@ -375,7 +371,7 @@ public class ApolloCodegen {

try SchemaMetadataFileGenerator(schema: ir.schema, config: config)
.generate(forConfig: config, fileManager: fileManager)
try SchemaConfigurationFileGenerator(schema: ir.schema, config: config)
try SchemaConfigurationFileGenerator(config: config)
.generate(forConfig: config, fileManager: fileManager)

try SchemaModuleFileGenerator.generate(config, fileManager: fileManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import Foundation
struct FragmentFileGenerator: FileGenerator {
/// Source IR fragment.
let irFragment: IR.NamedFragment
/// Source IR schema.
let schema: IR.Schema
/// Shared codegen configuration.
let config: ApolloCodegen.ConfigurationContext

var template: TemplateRenderer { FragmentTemplate(
fragment: irFragment,
schema: schema,
config: config
) }
var target: FileTarget { .fragment(irFragment.definition) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import Foundation
struct InputObjectFileGenerator: FileGenerator {
/// Source GraphQL input object.
let graphqlInputObject: GraphQLInputObjectType
/// IR representation of a GraphQL schema.
let schema: IR.Schema
/// Shared codegen configuration.
let config: ApolloCodegen.ConfigurationContext

var template: TemplateRenderer {
InputObjectTemplate(graphqlInputObject: graphqlInputObject, schema: schema, config: config)
InputObjectTemplate(graphqlInputObject: graphqlInputObject, config: config)
}
var target: FileTarget { .inputObject }
var fileName: String { graphqlInputObject.name }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@ import Foundation
struct OperationFileGenerator: FileGenerator {
/// Source IR operation.
let irOperation: IR.Operation
/// Source IR schema.
let schema: IR.Schema
/// Shared codegen configuration
let config: ApolloCodegen.ConfigurationContext

var template: TemplateRenderer {
irOperation.definition.isLocalCacheMutation ?
LocalCacheMutationDefinitionTemplate(
operation: irOperation,
schema: schema,
config: config
) :
OperationDefinitionTemplate(
operation: irOperation,
schema: schema,
config: config
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import OrderedCollections

/// Generates a file containing schema metadata used by the GraphQL executor at runtime.
struct SchemaConfigurationFileGenerator: FileGenerator {
/// Source IR schema.
let schema: IR.Schema
/// Shared codegen configuration
let config: ApolloCodegen.ConfigurationContext

var template: TemplateRenderer { SchemaConfigurationTemplate(schema: schema, config: config) }
var template: TemplateRenderer { SchemaConfigurationTemplate(config: config) }
var overwrite: Bool { false }
var target: FileTarget { .schema }
var fileName: String { "SchemaConfiguration" }
Expand Down
3 changes: 0 additions & 3 deletions Sources/ApolloCodegenLib/IR/IR+Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ import OrderedCollections

extension IR {
final class Schema {
let name: String
let referencedTypes: ReferencedTypes
let documentation: String?

init(
name: String,
referencedTypes: IR.Schema.ReferencedTypes,
documentation: String? = nil
) {
self.name = name
self.referencedTypes = referencedTypes
self.documentation = documentation
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/ApolloCodegenLib/IR/IR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ class IR {

var builtFragments: [String: NamedFragment] = [:]

init(schemaName: String, compilationResult: CompilationResult) {
init(compilationResult: CompilationResult) {
self.compilationResult = compilationResult
self.schema = Schema(
name: schemaName,
referencedTypes: .init(compilationResult.referencedTypes),
documentation: compilationResult.schemaDocumentation
)
Expand Down
5 changes: 1 addition & 4 deletions Sources/ApolloCodegenLib/Templates/FragmentTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import Foundation
struct FragmentTemplate: TemplateRenderer {
/// IR representation of source [GraphQL Fragment](https://spec.graphql.org/draft/#sec-Language.Fragments).
let fragment: IR.NamedFragment
/// IR representation of source GraphQL schema.
let schema: IR.Schema

let config: ApolloCodegen.ConfigurationContext

Expand All @@ -16,14 +14,13 @@ struct FragmentTemplate: TemplateRenderer {
TemplateString(
"""
\(embeddedAccessControlModifier)\
struct \(fragment.name.firstUppercased): \(schema.name)\
struct \(fragment.name.firstUppercased): \(config.schemaName.firstUppercased)\
.\(if: isMutable, "Mutable")SelectionSet, Fragment {
public static var fragmentDefinition: StaticString { ""\"
\(fragment.definition.source)
""\" }

\(SelectionSetTemplate(
schema: schema,
mutable: isMutable,
config: config
).BodyTemplate(fragment.rootField.selectionSet))
Expand Down
2 changes: 0 additions & 2 deletions Sources/ApolloCodegenLib/Templates/InputObjectTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import Foundation
struct InputObjectTemplate: TemplateRenderer {
/// IR representation of source [GraphQL Input Object](https://spec.graphql.org/draft/#sec-Input-Objects).
let graphqlInputObject: GraphQLInputObjectType
/// IR representation of a GraphQL schema.
let schema: IR.Schema

let config: ApolloCodegen.ConfigurationContext

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import OrderedCollections
struct LocalCacheMutationDefinitionTemplate: OperationTemplateRenderer {
/// IR representation of source [GraphQL Operation](https://spec.graphql.org/draft/#sec-Language.Operations).
let operation: IR.Operation
/// IR representation of source GraphQL schema.
let schema: IR.Schema

let config: ApolloCodegen.ConfigurationContext

Expand All @@ -23,7 +21,7 @@ struct LocalCacheMutationDefinitionTemplate: OperationTemplateRenderer {

\(section: VariableAccessors(operation.definition.variables, graphQLOperation: false))

\(SelectionSetTemplate(schema: schema, mutable: true, config: config).render(for: operation))
\(SelectionSetTemplate(mutable: true, config: config).render(for: operation))
}

""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import OrderedCollections
struct OperationDefinitionTemplate: OperationTemplateRenderer {
/// IR representation of source [GraphQL Operation](https://spec.graphql.org/draft/#sec-Language.Operations).
let operation: IR.Operation
/// IR representation of source GraphQL schema.
let schema: IR.Schema

let config: ApolloCodegen.ConfigurationContext

Expand All @@ -29,7 +27,7 @@ struct OperationDefinitionTemplate: OperationTemplateRenderer {

\(section: VariableAccessors(operation.definition.variables))

\(SelectionSetTemplate(schema: schema, config: config).render(for: operation))
\(SelectionSetTemplate(config: config).render(for: operation))
}

""")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/// Protocol for a `TemplateRenderer` that renders an operation definition template.
/// This protocol provides rendering helper functions for common template elements.
protocol OperationTemplateRenderer: TemplateRenderer {
var schema: IR.Schema { get }
}
protocol OperationTemplateRenderer: TemplateRenderer { }

extension OperationTemplateRenderer {
func Initializer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import Foundation

/// Renders the Cache Key Resolution extension for a generated schema.
struct SchemaConfigurationTemplate: TemplateRenderer {

/// Source IR schema.
let schema: IR.Schema
/// Shared codegen configuration
let config: ApolloCodegen.ConfigurationContext

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct SchemaMetadataTemplate: TemplateRenderer {

init(schema: IR.Schema, config: ApolloCodegen.ConfigurationContext) {
self.schema = schema
self.schemaName = schema.name.firstUppercased
self.schemaName = config.schemaName.firstUppercased
self.config = config
}

Expand Down
9 changes: 2 additions & 7 deletions Sources/ApolloCodegenLib/Templates/SelectionSetTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@ import InflectorKit

struct SelectionSetTemplate {

let schema: IR.Schema
let isMutable: Bool
let config: ApolloCodegen.ConfigurationContext

private let nameCache: SelectionSetNameCache

init(
schema: IR.Schema,
mutable: Bool = false,
config: ApolloCodegen.ConfigurationContext
) {
self.schema = schema
self.isMutable = mutable
self.config = config

self.nameCache = SelectionSetNameCache(schema: schema, config: config)
self.nameCache = SelectionSetNameCache(config: config)
}

// MARK: - Operation
Expand Down Expand Up @@ -416,11 +413,9 @@ struct SelectionSetTemplate {
fileprivate class SelectionSetNameCache {
private var generatedSelectionSetNames: [ObjectIdentifier: String] = [:]

unowned let schema: IR.Schema
let config: ApolloCodegen.ConfigurationContext

init(schema: IR.Schema, config: ApolloCodegen.ConfigurationContext) {
self.schema = schema
init(config: ApolloCodegen.ConfigurationContext) {
self.config = config
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/ApolloCodegenInternalTestHelpers/IR+Mocking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension IR {
schemaName: String = "TestSchema",
compilationResult: CompilationResult
) -> IR {
return IR(schemaName: schemaName, compilationResult: compilationResult)
return IR(compilationResult: compilationResult)
}

}
Expand Down
30 changes: 6 additions & 24 deletions Tests/ApolloCodegenTests/ApolloCodegenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -664,10 +664,7 @@ class ApolloCodegenTests: XCTestCase {
// when
let compilationResult = try ApolloCodegen.compileGraphQLResult(config)

let ir = IR(
schemaName: config.schemaName,
compilationResult: compilationResult
)
let ir = IR(compilationResult: compilationResult)

try ApolloCodegen.generateFiles(
compilationResult: compilationResult,
Expand Down Expand Up @@ -763,10 +760,7 @@ class ApolloCodegenTests: XCTestCase {
// when
let compilationResult = try ApolloCodegen.compileGraphQLResult(config)

let ir = IR(
schemaName: config.schemaName,
compilationResult: compilationResult
)
let ir = IR(compilationResult: compilationResult)

try ApolloCodegen.generateFiles(
compilationResult: compilationResult,
Expand Down Expand Up @@ -868,10 +862,7 @@ class ApolloCodegenTests: XCTestCase {
experimentalFeatures: .init(clientControlledNullability: true)
)

let ir = IR(
schemaName: config.schemaName,
compilationResult: compilationResult
)
let ir = IR(compilationResult: compilationResult)

try ApolloCodegen.generateFiles(
compilationResult: compilationResult,
Expand Down Expand Up @@ -935,10 +926,7 @@ class ApolloCodegenTests: XCTestCase {
experimentalFeatures: .init(clientControlledNullability: true)
)

let ir = IR(
schemaName: config.schemaName,
compilationResult: compilationResult
)
let ir = IR(compilationResult: compilationResult)

try ApolloCodegen.generateFiles(
compilationResult: compilationResult,
Expand Down Expand Up @@ -1039,10 +1027,7 @@ class ApolloCodegenTests: XCTestCase {
// when
let compilationResult = try ApolloCodegen.compileGraphQLResult(config)

let ir = IR(
schemaName: config.schemaName,
compilationResult: compilationResult
)
let ir = IR(compilationResult: compilationResult)

try ApolloCodegen.generateFiles(
compilationResult: compilationResult,
Expand Down Expand Up @@ -1141,10 +1126,7 @@ class ApolloCodegenTests: XCTestCase {
// when
let compilationResult = try ApolloCodegen.compileGraphQLResult(config)

let ir = IR(
schemaName: config.schemaName,
compilationResult: compilationResult
)
let ir = IR(compilationResult: compilationResult)

try ApolloCodegen.generateFiles(
compilationResult: compilationResult,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class FragmentFileGeneratorTests: XCTestCase {

subject = FragmentFileGenerator(
irFragment: irFragment,
schema: ir.schema,
config: ApolloCodegen.ConfigurationContext(config: ApolloCodegenConfiguration.mock())
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ class InputObjectFileGeneratorTests: XCTestCase {

// MARK: Test Helpers

private func buildSubject() {
let schema = IR.Schema(name: "TestSchema", referencedTypes: .init([]))
private func buildSubject() {
subject = InputObjectFileGenerator(
graphqlInputObject: graphqlInputObject,
schema: schema,
config: ApolloCodegen.ConfigurationContext(config: ApolloCodegenConfiguration.mock())
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class OperationFileGeneratorTests: XCTestCase {

let config = ApolloCodegen.ConfigurationContext(config: ApolloCodegenConfiguration.mock())

subject = OperationFileGenerator(irOperation: irOperation, schema: ir.schema, config: config)
subject = OperationFileGenerator(irOperation: irOperation, config: config)
}

// MARK: Property Tests
Expand Down
Loading