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

feature: Adding codegen config support for spm module type versions #539

Merged
merged 10 commits into from
Nov 27, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,7 @@ class ApolloCodegenConfigurationCodableTests: XCTestCase {
output: .init(
schemaTypes: .init(
path: "/output/path",
moduleType: .swiftPackage(apolloSDKDependency: ApolloCodegenConfiguration.SchemaTypesFileOutput.ModuleType.ApolloSDKDependency())
moduleType: .swiftPackage(apolloSDKDependency: .default)
),
operations: .absolute(path: "/absolute/path", accessModifier: .internal)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class SwiftPackageManagerModuleTemplateTests: XCTestCase {
output: .init(
schemaTypes: .init(
path: "path/",
moduleType: .swiftPackage(apolloSDKDependency: ApolloCodegenConfiguration.SchemaTypesFileOutput.ModuleType.ApolloSDKDependency())
moduleType: .swiftPackage(apolloSDKDependency: .default)
))
))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
moduleType: ModuleType
) {
self.path = path
self.moduleType = moduleType == .swiftPackageManager ? .swiftPackage(apolloSDKDependency: ModuleType.ApolloSDKDependency()) : moduleType
self.moduleType = moduleType == .swiftPackageManager ? .swiftPackage(apolloSDKDependency: .default) : moduleType
}

/// Compatible dependency manager automation.
Expand All @@ -288,7 +288,7 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
/// Generates a `Package.swift` file that is suitable for linking then generated schema types
/// files to your project using Swift Package Manager. Uses the `apolloSDKDependency`
/// to determine how to setup the dependency on `apollo-ios`.
case swiftPackage(apolloSDKDependency: ApolloSDKDependency = ApolloSDKDependency())
case swiftPackage(apolloSDKDependency: ApolloSDKDependency = .default)
/// No module will be created for the generated types and you are required to create the
/// module to support your preferred dependency manager. You must specify the name of the
/// module you will create in the `schemaNamespace` property as this will be used in `import`
Expand Down Expand Up @@ -326,7 +326,7 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
self = .embeddedInTarget(name: name, accessModifier: accessModifier)

case .swiftPackageManager:
self = .swiftPackage(apolloSDKDependency: ApolloSDKDependency())
self = .swiftPackage(apolloSDKDependency: .default)

case .swiftPackage:
let nestedContainer = try container.nestedContainer(
Expand All @@ -350,6 +350,8 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
/// Type of SPM dependency to use.
let sdkVersion: SDKVersion

public static let `default` = ApolloSDKDependency()

public init(
url: String = "https://github.com/apollographql/apollo-ios",
sdkVersion: SDKVersion = .default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct SwiftPackageManagerModuleTemplate: TemplateRenderer {
case .swiftPackage(let apolloSDKDependency):
self.apolloSDKDependency = apolloSDKDependency
default:
self.apolloSDKDependency = ApolloCodegenConfiguration.SchemaTypesFileOutput.ModuleType.ApolloSDKDependency()
self.apolloSDKDependency = .default
}
}

Expand Down
61 changes: 55 additions & 6 deletions docs/source/code-generation/codegen-configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ The properties to configure `output` are:
"output": {
"schemaTypes": {
"moduleType": {
"swiftPackageManager": {}
"swiftPackage": {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's long bugged me that we had swiftPackageManager as a ModuleType option and swiftPackage as a TestMocks option; I'm happy these are more similar now. 😄

},
"path": "./generated/schema/"
},
Expand All @@ -125,7 +125,7 @@ let configuration = ApolloCodegenConfiguration(
output: ApolloCodegenConfiguration.FileOutput(
schemaTypes: ApolloCodegenConfiguration.SchemaTypesFileOutput(
path: "./generated/schema/",
moduleType: .swiftPackageManager
moduleType: .swiftPackage()
)
operations: .inSchemaModule,
testMocks: .none
Expand Down Expand Up @@ -156,7 +156,7 @@ Use the [`ModuleType`](https://www.apollographql.com/docs/ios/docc/documentation

For single target applications, [`embeddedInTarget(name:)`](#embedded-in-target) can be used.

For multi-module projects, it is recommended that the schema types be created in a separate module to help with separation of concerns. If using Swift Package Manager, the `swiftPackageManager` option can help automate module creation.
For multi-module projects, it is recommended that the schema types be created in a separate module to help with separation of concerns. If using Swift Package Manager, the `swiftPackage` option can help automate module creation.

> **Note:** This option must be set correctly for your project structure or generated code will fail to compile.

Expand All @@ -165,7 +165,8 @@ The possible values for `moduleType` are:
| Value | Description |
| ----- | ----------- |
| [`embeddedInTarget(name:accessModifier:)`](#embedded-in-target) | Indicates that you would like to include the generated module directly in your application target. |
| [`swiftPackageManager`](#swift-package-manager) | Creates a schema module as an SPM package that can be included in your project. |
| [`swiftPackageManager`](#swift-package-manager) | Creates a schema module as an SPM package that can be included in your project. Note: This option has been deprecated in favor of the new [`swiftPackage`](#swift-package) option. |
| [`swiftPackage`](#swift-package) | Creates a schema module as an SPM package that can be included in your project and allows for customization of the `apollo-ios` dependency of the pacakge. |
| [`other`](#other-schema-module-types) | Indicates that you would like to manually define a schema module using a third party package manager (such as Cocoapods). |

#### Embedded in target
Expand Down Expand Up @@ -257,11 +258,59 @@ let configuration = ApolloCodegenConfiguration(

</MultiCodeBlock>

#### Swift package

**[`ModuleType.swiftPackage(apolloSDKDependency: ApolloSDKDependency)`](https://www.apollographql.com/docs/ios/docc/documentation/apollocodegenlib/apollocodegenconfiguration/schematypesfileoutput/moduletype-swift.enum/swiftpackage)**

This option automates the creation of an SPM package for your schema types. This generates a `Package.swift` file that is suitable for linking the generated schema types files to your project using Swift Package Manager.

The `apolloSDKDependency` paramter allows you to customize the `apollo-ios` dependency of your SPM package by providing a url and SDK version. The `url` parameter allows you to customize the location of the `apollo-ios` dependency used by the generated SPM package, and has a default value that points to the main [`apollo-ios`](https://github.com/apollographql/apollo-ios) repository. The `sdkVersion` parameter allows you to set the version of the `apollo-ios` dependency to use. The default value of this is the `default` case, which uses an exact version of `apollo-ios` matching the code generation version in use which is the recommended way of handling this. The other provided options allow flexibility for doing things such as testing experimental branches, or using local versions for testing.
BobaFetters marked this conversation as resolved.
Show resolved Hide resolved

The schema types will be contained in a stand-alone module with the [`schemaNamespace`](#schema-namespace) provided. Other targets in your application that contain generated operation models must have a dependency on this target.

For multi-module projects using Swift Package Manager, this is the recommended configuration option.

> **Including schema types in your own `Package.swift` file:**
>
> This option generates an SPM package with its own `Package.swift` file. This is ideal if you want to share your schema module across multiple projects or using Xcode's Package Dependency GUI.
>
> If you would like to include your schema module as a target in your own `Package.swift` file, you can define a target with the name and path of the generated files in your `Package.swift` file.

<MultiCodeBlock>

```json title="CLI Configuration JSON"
"output": {
"schemaTypes": {
"moduleType": {
"swiftPackage": {
"apolloSDKDependency" : {}
}
},
"path": "./generated/schema/"
}
}
```

```swift title="Swift Codegen Setup"
let configuration = ApolloCodegenConfiguration(
// Other properties not shown
output: ApolloCodegenConfiguration.FileOutput(
schemaTypes: ApolloCodegenConfiguration.SchemaTypesFileOutput(
path: "./generated/schema/",
moduleType: .swiftPackage(apolloSDKDependency: .default)
)
...
)
)
```

</MultiCodeBlock>

#### Other schema module types

**[`ModuleType.other`](https://www.apollographql.com/docs/ios/docc/documentation/apollocodegenlib/apollocodegenconfiguration/schematypesfileoutput/moduletype-swift.enum/other)**

This value should be used when you would like to define a schema module using another package management system (such as CocoaPods), or you want more control over the generated package than what [`ModuleType.swiftPackageManager`](https://www.apollographql.com/docs/ios/docc/documentation/apollocodegenlib/apollocodegenconfiguration/schematypesfileoutput/moduletype-swift.enum/swiftpackagemanager) allows, or manually creating your own targets or modules in another way.
This value should be used when you would like to define a schema module using another package management system (such as CocoaPods), or you want more control over the generated package than what [`ModuleType.swiftPackage`](https://www.apollographql.com/docs/ios/docc/documentation/apollocodegenlib/apollocodegenconfiguration/schematypesfileoutput/moduletype-swift.enum/swiftpackage) allows, or manually creating your own targets or modules in another way.

`ModuleType.other` indicates to the Code Generation Engine that your schema types will be contained in their own target, rather than embedded in your application target. This affects the `import` statements in your generated operation definition files.

Expand Down Expand Up @@ -376,7 +425,7 @@ This option generates test mock files and defines a target in the schema modules

The name of the test mock target can be specified with the `targetName` parameter. If no `targetName` is provided, the target name defaults to `"${schemaNamespace}TestMocks"`.

> **Note:** Using this option requires your [`output.schemaTypes.moduleType`](#module-type) to be [`.swiftPackageManager`](#swift-package-manager). If this option is provided without the `.swiftPackageManager` module type, code generation will fail.
> **Note:** Using this option requires your [`output.schemaTypes.moduleType`](#module-type) to be [`.swiftPackage`](#swift-package). If this option is provided without the `.swiftPackage` module type, code generation will fail.

#### Absolute test mocks output

Expand Down
Loading