Adding Digitransit API GraphQL to an Swift project

This is a step by step instructions on how to add GraphQL API of Finnish public transportation information to a Swift project in Xcode. This is mostly for my own future reference and collected from more verbose post in Medium Using GraphQL in iOS with Swift - Better Programming - Medium.

(as of writing instructions are valid for Swift 5.1 and Xcode 11)

Step 0: prerequisites:

Install apollo npm toolchain for generating the schema file npm install -g npm

Step 1: Create an Xcode project

Nothing special here.

Step 2: Install Apollo framework dependency for your project

All in detail at Apollo documentation Installation - Client (iOS) - Apollo GraphQL Docs

Step 3: Generate GraphQL schema.json

Run the apollo tool you installed in step 0 to generate the schema.json to be used for generating the datatypes

apollo schema:download –…

Step 3.1: Add schema.json to XCode project

Nothing weird here

Step 4: Add a Code Generation Step

As documented here: Installation - Client (iOS) - Apollo GraphQL Docs

Step 5: Add .graphql queries to project

Next step is to add .graphql file to your project. Digitransit has an excellent documentation and a sandbox for trying things out

Digitransit documentation for stop arrival information Stops | Digitransit has convenient links to sandbox for testing the API out

Step 6: (Re-)Generate API.swift file when needed

Finally, one runs the build to generate the API.swift file that contains the types of the queries you created. Everytime you add a new type, rerun the build to generate new API.swift

Installation - Client (iOS) - Apollo GraphQL Docs