I have a project structure like this:
↳Subpackage (as SPM Local Package)
I want to make the iOS Apollo client a dependency of “Subpackage”; both the main app target and the App Clip target should depend on Subpackage.
The Installing Apollo iOS docs instruct me to add the code generation step as a Run Script phase in the main app target. This is not really what I want, the GraphQL code needs to be regenerated when I make changes to the GraphQL files in “Subpackage”, not when I change the main target. Is SE-0303 “Extensible Build Tools” any help here?
I had to add my
QueryPost.graphql files as
.copy() resources to silence an Xcode warning, but I don’t really want that either I think as they’re only needed at build time. Is there a better way of silencing this warning?
I’ve added GraphQL as a
.dependency() in Subpackage’s
Package.swift but the
import Apollo statement in the generated
API.swift (which lives alongside
QueryPost.graphql in Subpackage, I modified the generation script) fails to compile with “No such module ‘Apollo’”
Are there any up-to-date docs on using Apollo GraphQL as a dependency of an SPM Local Package?
I managed to get this working. The approach is to use the Swift scripting method of doing the schema download/code generation and running it manually instead of trying to trigger it from a Run Script phase of the app target. I cloned the example Swift scripting repo into the SPM Local Module’s directory hierarchy and fiddled with the paths in
main.swift until everything lined up, then it’s simple enough to do
swift run ApolloCodegen downloadSchema/
swift run ApolloCodegen generate manually when you make a change.
I didn’t bother with the extra step of integrating it into the build as an “extensible tool”, it’s not really necessary for my demo app.
The problem with the
import Apollo compile error was me not understanding how to use
Package.swift properly—as well as declaring the dependency inside the package, you also need to set the
dependencies: param inside the
.target() declaration. Similarly,
.target() has an
exclude: parameter you can use instead of of
.copy(), if you actually want to exclude something.
Hi @ratkins, apologies for the late reply and glad you managed to get it working.
We’re trending away from the build phase that does code generation on every build and will be making use of SPM plugins for versioning with the new codegen CLI tool we’ve built as a part of the imminent 1.0 release - check it out if you haven’t seen it yet.