Code Generation on Swift

Hi.

I’ve used Apollo some months ago, but working on a new project I’m struggling to generate code from a schema.

So far, I’ve been able to add the Apollo dependency (Apollo, ApolloAPI and ApolloCodegenLib) to my project, download a schema through Apollo Studio, and tried to follow the steps from the following link: Running code generation in Swift code - Apollo GraphQL Docs. However, when implementing the code from the “Running Code Generation” section and building my project on Xcode, an error pops up in several files: “Cannot find ‘ApolloCodegen’ in scope”.

Despite these errors appear on files from the Apollo package, I’m quite sure I’ve correctly added the Apollo dependencies.

On the other hand, I find this new way of code generation a bit more tricky than before (by using build phases): I don’t quite understand the new process.

Do you come up with any solution for these errors? Or is there any other simple way of generating code?

Thank you in advance!

If you are running code generation in swift, you should be including ApolloCodegenLib in a separate target that is used to run codegen, not in the target of your actual application.

The easiest way to run codegen is with the Codegen CLI as is stated in the header of the docs page you linked. Unless you need to do advanced configuration, we recommend using the CLI.

You can even run the CLI from a build phase if you want to, though it’s overkill to run codegen every time you build your project.

@Javi_Gallego is it an iOS target that you’re building? If so then that’s the reason for those build errors, ApolloCodegenLib is macOS only.

I also have this error but I have not included the ApolloCodegenLib in any of my targets. I included the apollo-ios-cli which gives me the right click -> generate tool.

But these errors are coming from the Apollo library files rather than any of the generated code.

EDIT:

OK, I managed to get it working. I removed the apollo-ios-cli from the package and used the cli command from Terminal and that sorted it.

Thanks again for your help on GitHub @calvincestari

Finally got there :smiley:

Glad you got it working @Oliver_Foggin.

apollo-ios-cli is really just an executable wrapper around the CodegenCLI library target, you shouldn’t ever need to include that in the list of dependency targets.