GraphQL setup

As part of this GraphQL series, I write down my experience and observations about adding a GraphQL networking layer to an app. In this article, I will talk about how I added the dependency of Apollo to an existing iOS app.

I started by adding the package to my project (standard procedure). I selected the module in the navigation pane, then the project (not the app target) and finally the Package Dependencies tab.

This is the current URL for the package: https://github.com/apollographql/apollo-ios.git.

In a more evolved project, where there are multiple modules, I find that it was crucial to only import Apollo once (from one module). Importing it from multiple modules made the app crash at run time. The logical solution is to have the import from the networking module only.

Once that done, I installed Apollo's CLI tool by right-clicking on the module which has the Apollo dependency. (On a multiple module project, it will be the one from which you have imported the module.)

The CLI allows for essential steps, mainly fetching the schema and generate code. So once this is done, I could do those commands:

./apollo-ios-cli init --schema-namespace StatSkiAPI --module-type swiftPackageManager
./apollo-ios-cli fetch-schema
./apollo-ios-cli generate

I was interested in checking options for the --module-type because I wasn't keen on adding another module, but the other option is adding the generated files directly to a module. What was a deal-breaker for me was the fact that Xcode does not add the files automatically. So it'd be real easy for another developer to generate the files and wonder why the code is not available. A package is autonomous; I generate and don't care about the content.

Ok... once Apollo is init'd, though, we need to configure the config file before fetching schema. In that config file, you'll let the library know where to fetch such a schema. Note: the URL is the same to the one you use for the "playground" and for the URLSession for fetching queries themselves. It will likely be your-domain.com/graphql.

At this point, I have my project setup with the Apollo library linked, the CLI installed, the config file setup and the schema downloaded.

For the code generation to work, there need to be query files. For a small scale project, you can have them wherever makes sense for you, and we'll continue with this assumption for now. Later in this series, I'll dive into how queries can be shared with Android.

I will also dive into building queries later, but let's pretend I have one ready. It can be copied into a .graphql text file. That file must be located where the config file points to: "operationSearchPaths" : ["**/*.graphql"] .

At this point I should be able to use that query, but Xcode, oh Xcode, might have trouble seeing that new generated code. At least this is the case for me using multiple versions of Xcode 15. I know the drill! Clean, Quit Xcode, Delete Derived Data, open Xcode, Update Packages Cache, Build, then cross fingers.

In the next article in the series, I will talk more about building a query and execute a first fetch.