Skip to content

Commit

Permalink
Feature: Schema Type Renaming (#388)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Miller <anthonymdev@gmail.com>
Co-authored-by: Calvin Cestari <calvincestari@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 13, 2024
1 parent 5bc0a45 commit d5cbc75
Show file tree
Hide file tree
Showing 68 changed files with 2,207 additions and 422 deletions.
23 changes: 12 additions & 11 deletions Tests/ApolloCodegenInternalTestHelpers/MockGraphQLType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public extension GraphQLCompositeType {
_ name: String = ""
) -> GraphQLCompositeType {
GraphQLCompositeType(
name: name,
name: GraphQLName(schemaName: name),
documentation: nil
)
}
Expand All @@ -23,7 +23,7 @@ public extension GraphQLObjectType {
documentation: String? = nil
) -> GraphQLObjectType {
GraphQLObjectType(
name: name,
name: GraphQLName(schemaName: name),
documentation: documentation,
fields: fields,
interfaces: interfaces
Expand All @@ -39,7 +39,7 @@ public extension GraphQLInterfaceType {
documentation: String? = nil
) -> GraphQLInterfaceType {
GraphQLInterfaceType(
name: name,
name: GraphQLName(schemaName: name),
documentation: documentation,
fields: fields,
interfaces: interfaces
Expand All @@ -54,7 +54,7 @@ public extension GraphQLUnionType {
documentation: String? = nil
) -> GraphQLUnionType {
GraphQLUnionType(
name: name,
name: GraphQLName(schemaName: name),
documentation: documentation,
types: types
)
Expand All @@ -73,7 +73,7 @@ public extension GraphQLScalarType {
documentation: String? = nil
) -> GraphQLScalarType {
GraphQLScalarType(
name: name,
name: GraphQLName(schemaName: name),
documentation: documentation,
specifiedByURL: specifiedByURL
)
Expand Down Expand Up @@ -112,7 +112,7 @@ public extension GraphQLEnumType {
documentation: String? = nil
) -> GraphQLEnumType {
GraphQLEnumType(
name: name,
name: GraphQLName(schemaName: name),
documentation: documentation,
values: values
)
Expand All @@ -126,7 +126,7 @@ public extension GraphQLEnumValue {
documentation: String? = nil
) -> GraphQLEnumValue {
GraphQLEnumValue(
name: Name(value: name),
name: GraphQLName(schemaName: name),
documentation: documentation,
deprecationReason: deprecationReason
)
Expand All @@ -137,12 +137,13 @@ public extension GraphQLInputObjectType {
class func mock(
_ name: String,
fields: [GraphQLInputField] = [],
documentation: String? = nil
documentation: String? = nil,
config: ApolloCodegenConfiguration = .mock()
) -> GraphQLInputObjectType {
GraphQLInputObjectType(
name: name,
name: GraphQLName(schemaName: name),
documentation: documentation,
fields: OrderedDictionary.init(uniqueKeysWithValues: fields.map({ ($0.name, $0) }))
fields: OrderedDictionary.init(uniqueKeysWithValues: fields.map({ ($0.render(config: config), $0) }))
)
}
}
Expand All @@ -156,7 +157,7 @@ public extension GraphQLInputField {
deprecationReason: String? = nil
) -> GraphQLInputField {
GraphQLInputField(
name: name,
name: GraphQLName(schemaName: name),
type: type,
documentation: documentation,
deprecationReason: deprecationReason,
Expand Down
14 changes: 7 additions & 7 deletions Tests/ApolloCodegenInternalTestHelpers/MockIRSubscripts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@ import GraphQLCompiler

extension IR.Schema {
public subscript(object name: String) -> GraphQLObjectType? {
return referencedTypes.objects.first { $0.name == name }
return referencedTypes.objects.first { $0.name.schemaName == name }
}

public subscript(interface name: String) -> GraphQLInterfaceType? {
return referencedTypes.interfaces.first { $0.name == name }
return referencedTypes.interfaces.first { $0.name.schemaName == name }
}

public subscript(union name: String) -> GraphQLUnionType? {
return referencedTypes.unions.first { $0.name == name }
return referencedTypes.unions.first { $0.name.schemaName == name }
}

public subscript(scalar name: String) -> GraphQLScalarType? {
return referencedTypes.scalars.first { $0.name == name }
return referencedTypes.scalars.first { $0.name.schemaName == name }
}

public subscript(enum name: String) -> GraphQLEnumType? {
return referencedTypes.enums.first { $0.name == name }
return referencedTypes.enums.first { $0.name.schemaName == name }
}

public subscript(inputObject name: String) -> GraphQLInputObjectType? {
return referencedTypes.inputObjects.first { $0.name == name }
return referencedTypes.inputObjects.first { $0.name.schemaName == name }
}
}

extension CompilationResult {

public subscript(type name: String) -> GraphQLNamedType? {
return referencedTypes.first { $0.name == name }
return referencedTypes.first { $0.name.schemaName == name }
}

public subscript(operation name: String) -> CompilationResult.OperationDefinition? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ class ApolloCodegenConfigurationCodableTests: XCTestCase {
],
deprecatedEnumCases: .exclude,
schemaDocumentation: .exclude,
schemaCustomization: .init(
customTypeNames: [
"MyEnum": .enum(
name: "CustomEnum",
cases: [
"CaseOne": "CustomCaseOne"
]
),
"MyInterface": .type(name: "CustomInterface"),
"MyObject": .type(name: "CustomObject")
]
),
cocoapodsCompatibleImportStatements: true,
warningsOnDeprecatedUsage: .exclude,
conversionStrategies:.init(
Expand Down Expand Up @@ -104,6 +116,20 @@ class ApolloCodegenConfigurationCodableTests: XCTestCase {
"definition"
],
"pruneGeneratedFiles" : false,
"schemaCustomization" : {
"customTypeNames" : {
"MyEnum" : {
"enum" : {
"cases" : {
"CaseOne" : "CustomCaseOne"
},
"name" : "CustomEnum"
}
},
"MyInterface" : "CustomInterface",
"MyObject" : "CustomObject"
}
},
"schemaDocumentation" : "exclude",
"selectionSetInitializers" : {
"localCacheMutations" : true
Expand Down
112 changes: 112 additions & 0 deletions Tests/ApolloCodegenTests/ApolloCodegenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2484,5 +2484,117 @@ class ApolloCodegenTests: XCTestCase {
expect(matches.contains(where: { $0.contains(".swiftpm") })).to(beFalse())
expect(matches.contains(where: { $0.contains(".Pods") })).to(beFalse())
}

// MARK: - Schema Customization Tests

func test_typeNames_givenSchemaCustomization_shouldGenerateCustomTypeNames() async throws {
// given
let schemaPath = ApolloCodegenInternalTestHelpers.Resources.AnimalKingdom.Schema.path
let operationsPath = ApolloCodegenInternalTestHelpers.Resources.url
.appendingPathComponent("animalkingdom-graphql")
.appendingPathComponent("**/*.graphql").path

let config = ApolloCodegen.ConfigurationContext(config: ApolloCodegenConfiguration(
schemaNamespace: "AnimalKingdomAPI",
input: .init(schemaPath: schemaPath, operationSearchPaths: [operationsPath]),
output: .init(
schemaTypes: .init(path: directoryURL.path,
moduleType: .swiftPackageManager),
operations: .inSchemaModule
),
options: .init(
schemaCustomization: .init(
customTypeNames: [
"Crocodile": .type(name: "CustomCrocodile"), // Object
"Animal": .type(name: "CustomAnimal"), // Interface
"ClassroomPet": .type(name: "CustomClassroomPet"), // Union
"Date": .type(name: "CustomDate"), // Custom Scalar
"SkinCovering": .enum( // Enum
name: "CustomSkinCovering",
cases: [
"HAIR": "CUSTOMHAIR"
]
),
"PetSearchFilters": .inputObject( // Input Object
name: "CustomPetSearchFilters",
fields: [
"size": "customSize"
]
)
]
)
)
), rootURL: nil)

let subject = ApolloCodegen(
config: config,
operationIdentifierFactory: OperationIdentifierFactory(),
itemsToGenerate: .code
)

// when
let compilationResult = try await subject.compileGraphQLResult()

let ir = IRBuilder(compilationResult: compilationResult)

subject.processSchemaCustomizations(ir: ir)

for objType in ir.schema.referencedTypes.objects {
if objType.name.schemaName == "Crocodile" {
expect(objType.name.customName).to(equal("CustomCrocodile"))
break
}
}

for interfaceType in ir.schema.referencedTypes.interfaces {
if interfaceType.name.schemaName == "Animal" {
expect(interfaceType.name.customName).to(equal("CustomAnimal"))
break
}
}

for unionType in ir.schema.referencedTypes.unions {
if unionType.name.schemaName == "ClassroomPet" {
expect(unionType.name.customName).to(equal("CustomClassroomPet"))
break
}
}

for customScalarType in ir.schema.referencedTypes.customScalars {
if customScalarType.name.schemaName == "Date" {
expect(customScalarType.name.customName).to(equal("CustomDate"))
break
}
}

for enumType in ir.schema.referencedTypes.enums {
if enumType.name.schemaName == "SkinCovering" {
expect(enumType.name.customName).to(equal("CustomSkinCovering"))

for enumCase in enumType.values {
if enumCase.name.schemaName == "HAIR" {
expect(enumCase.name.customName).to(equal("CUSTOMHAIR"))
}
}

break
}
}

for inputObjectType in ir.schema.referencedTypes.inputObjects {
if inputObjectType.name.schemaName == "PetSearchFilters" {
expect(inputObjectType.name.customName).to(equal("CustomPetSearchFilters"))

for inputField in inputObjectType.fields.values {
if inputField.name.schemaName == "size" {
expect(inputField.name.customName).to(equal("customSize"))
}
}

break
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ class ApolloSchemaDownloaderInternalTests: XCTestCase {
let schema = try await frontend.loadSchema(from: [source])

let authorType = try await schema.getType(named: "Author")
XCTAssertEqual(authorType?.name, "Author")
XCTAssertEqual(authorType?.name.schemaName, "Author")

let postType = try await schema.getType(named: "Post")
XCTAssertEqual(postType?.name, "Post")
XCTAssertEqual(postType?.name.schemaName, "Post")
}

// MARK: Request Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CustomScalarFileGeneratorTests: XCTestCase {
// given
buildSubject()

let expected = graphqlScalar.name
let expected = graphqlScalar.name.schemaName

// then
expect(self.subject.fileName).to(equal(expected))
Expand All @@ -48,4 +48,17 @@ class CustomScalarFileGeneratorTests: XCTestCase {
// then
expect(self.subject.overwrite).to(beFalse())
}

// MARK: Schema Customization Tests

func test__filename_matchesCustomName() throws {
// given
let customName = "MyCustomScalar"
graphqlScalar.name.customName = customName
buildSubject()

// then
expect(self.subject.fileName).to(equal(customName))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class EnumFileGeneratorTests: XCTestCase {
// given
buildSubject()

let expected = graphqlEnum.formattedName
let expected = graphqlEnum.name.schemaName

// then
expect(self.subject.fileName).to(equal(expected))
Expand All @@ -48,4 +48,16 @@ class EnumFileGeneratorTests: XCTestCase {
// then
expect(self.subject.overwrite).to(beTrue())
}

// MARK: Schema Customization Tests

func test__filename_matchesCustomName() throws {
// given
let customName = "MyCustomEnum"
graphqlEnum.name.customName = customName
buildSubject()

// then
expect(self.subject.fileName).to(equal(customName))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class InputObjectFileGeneratorTests: XCTestCase {
// given
buildSubject()

let expected = graphqlInputObject.name
let expected = graphqlInputObject.name.schemaName

// then
expect(self.subject.fileName).to(equal(expected))
Expand All @@ -48,4 +48,16 @@ class InputObjectFileGeneratorTests: XCTestCase {
// then
expect(self.subject.overwrite).to(beTrue())
}

// MARK: Schema Customization Tests

func test__filename_matchesCustomName() throws {
// given
let customName = "MyCustomInputObject"
graphqlInputObject.name.customName = customName
buildSubject()

// then
expect(self.subject.fileName).to(equal(customName))
}
}
Loading

0 comments on commit d5cbc75

Please sign in to comment.