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

Schema type renaming #276

Open
wants to merge 20 commits into
base: feature/rename-schema-types
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
4a89484
Draft idea for custom schema type names
BobaFetters Feb 17, 2024
6e33253
Merge branch 'main' into feature/codegen-rename-types
BobaFetters Feb 17, 2024
53a591d
First step for new IR type
BobaFetters Feb 22, 2024
bc49804
Merge branch 'feature/rename-schema-types' into feature/codegen-renam…
BobaFetters Feb 22, 2024
4d9474b
Updating templates and file generators
BobaFetters Feb 23, 2024
b7c4674
Removing some old changes
BobaFetters Feb 23, 2024
88e7307
Checking in current progress
BobaFetters Mar 1, 2024
876d7de
Merge branch 'feature/rename-schema-types' into feature/codegen-renam…
BobaFetters May 7, 2024
d3be312
Reworking name handling
BobaFetters May 20, 2024
fb0de90
Merge branch 'feature/rename-schema-types' into feature/codegen-renam…
BobaFetters May 20, 2024
3c032cf
Updating name suffix handling
BobaFetters May 20, 2024
53205e5
Adding documentation for custom type names
BobaFetters May 21, 2024
1d1a206
Fixing existing tests
BobaFetters May 22, 2024
dc5f479
Adding tests for custom naming
BobaFetters May 24, 2024
cf73a84
Merge branch 'feature/rename-schema-types' into feature/codegen-renam…
BobaFetters May 24, 2024
c06e1c4
Finishing unit test and adding test codegen project
BobaFetters May 26, 2024
2eca858
Merge branch 'feature/rename-schema-types' into feature/codegen-renam…
BobaFetters May 26, 2024
266da55
Fixing cache mutation tests
BobaFetters Jun 4, 2024
62dacbf
Merge branch 'feature/rename-schema-types' into feature/codegen-renam…
BobaFetters Jun 4, 2024
fbb32f7
Adding missing schema configuration for test
BobaFetters Jun 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class InterfaceFileGeneratorTests: XCTestCase {
// given
buildSubject()

let expected = graphqlInterface.name
let expected = graphqlInterface.name.schemaName

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

// MARK: Schema Customization Tests

func test__filename_matchesCustomName() throws {
// given
let customName = "MyCustomInterface"
graphqlInterface.name.customName = customName
buildSubject()

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

let expected = "\(graphqlObject.name)+Mock"
let expected = "\(graphqlObject.name.schemaName)+Mock"

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

// MARK: Schema Customization Tests

func test__filename_matchesCustomName() throws {
// given
let customName = "MyCustomObject"
graphqlObject.name.customName = customName
buildSubject()

// then
expect(self.subject.fileName).to(equal("\(customName)+Mock"))
}
}