Running code generation in Swift code
For most use cases, the Apollo iOS Codegen CLI is the recommended method for generating code and downloading your schema. The CLI uses the
ApolloCodegenLib to execute these operation.
To enable advanced use, the
ApolloCodegenLib can be included as a dependency of your own Swift projects. This enables you to use Swift scripting to perform operations that cannot be done with the CLI. Calling into the
ApolloCodegenLib directly allows you to integrate Apollo into your development process however you would like.
ApolloCodegenLib is included as a product in the Apollo iOS SPM Package.
At this time,
ApolloCodegenLib only supports
macOS. You cannot run code generation scripts on other platforms.
To use it within your own Swift project, just include it as a dependency.
.target(name: "MyApp",dependencies: [.product(name: "ApolloCodegenLib", package: "apollo-ios"),])
Important: Because code generation is tied to its usage in the Apollo iOS SDK, you make sure you are using the
ApolloCodegenLib from the same version of the package as the application that uses your generated code.
Once you've installed the
ApolloCodegenLib dependency in your project, you can use it to:
To configure and run code generation using
ApolloCodegenLib, create an
ApolloCodegenConfiguration, then pass it to
ApolloCodegen.build(with configuration:withRootURL:) function.
By default, the Code Generation Engine will resolve all file paths in your
ApolloCodegenConfiguration relative to the current working directory. The
rootURL parameter can be used to provide a local file path URL of the root of the project that you want to run code generation on. This will resolve file paths in your configuration relative to the provided
import ApolloCodgenLiblet configuration = ApolloCodegenConfiguration(schemaName: "MySchemaName",input: ApolloCodegenConfiguration.FileInput(schemaSearchPaths: ["**/*.graphqls"],operationSearchPaths: ["**/*.graphql"]),output: ApolloCodegenConfiguration.FileOutput(schemaTypes: ApolloCodegenConfiguration.SchemaTypesFileOutput(path: "./generated/schema/",moduleType: .swiftPackageManager)operations: .inSchemaModule,testMocks: .none,),options: ApolloCodegenConfiguration.OutputOptions(additionalInflectionRules: [.pluralization(singularRegex: "animal",replacementRegex: "animals")],queryStringLiteralFormat: .multiline,deprecatedEnumCases: .include,schemaDocumentation: .include,apqs: .disabled,cocoapodsCompatibleImportStatements: false,warningsOnDeprecatedUsage: .include,conversionStrategies: ApolloCodegenConfiguration.ConversionStrategies(enumCases: .camelCase),pruneGeneratedFiles: true))try! ApolloCodegen.build(with: configuration)
For more information on the configuring code generation, see the configuration documentation.
If you would like to download a GraphQL schema from you own Swift project, use the
The schema downloader can be configured with an
import ApolloCodegenLiblet configuration = ApolloSchemaDownloadConfiguration(using: .introspection(endpointURL: endpoint, outputFormat: .SDL),outputPath: outputURL.path)try! ApolloSchemaDownloader.fetch(configuration: configuration)
For more information on schema fetching, see Downloading a schema.