Skip to content

Instantly share code, notes, and snippets.

@d108
Last active June 7, 2023 04:50
Show Gist options
  • Save d108/59546d047b80cdd0d38ed98a1ccd0839 to your computer and use it in GitHub Desktop.
Save d108/59546d047b80cdd0d38ed98a1ccd0839 to your computer and use it in GitHub Desktop.
author title
**Daniel Zhang**
**There's always a trick to it**

If you haven't followed along with GraphQL and Apollo, you might find it confusing when you start your Swift app that calls a GraphQL endpoint.

Quickly get up to speed

Installing the Swift package for Apollo is the easy part.

To ensure that your project is properly configured, make sure that the correct framework is linked in the general tab of your target. Beginning with Apollo alone should suffice.

Preparing for code generation

Generating code to access an API might be strange if you haven't used something like Apollo before. You might encounter documentation for the legacy version when searching for answers. I want to help you avoid some of the pitfalls I encountered in 2023.

Breaking changes came with Apollo version 1.0; the library is now on 1.2.1. For example, code generation is no longer recommended to be added to a run script phase. Instead, the recommendation is to generate code from the command line.

But Xcode 14.3 has no menu command for installing Apollo iOS CLI because the plugin is not working, even though it works in Xcode 14.2. What is needed instead is to grab the Apollo CLI binary from their release page on GitHub. It exists as a separate gzip file.

Once you have the binary, you can run it from the command line. Note you shouldn't run it from the top level of your project because it will recursively scan all subdirectories, including .git and others.

To generate code, you need a schema file. If you don't have one, you can get it from your endpoint if it supports introspection after installing a tool like get-graphql-schema from npm, shown below.

$ npm install -g get-graphql-schema

Then you can download a schema with

$ get-graphql-schema ${YOUR_ENDPOINT} > schema.graphqls

Forming queries

That's not all you need. No code will be generated if you don't specify some queries or mutations. You will create these files using a .graphql extension instead of .graphqls for the schema.

For example, a query might look like the following:

The above query is for a SpaceX API. It will give a result similar to what is listed below.

Code generation

You'll need a configuration file before you can run the code generator. One can be created with the following command:

$ ./apollo-ios-cli init --schema-namespace SpaceX --module-type embeddedInTarget --target-name MyApp --overwrite

After putting all the pieces together, you should be able to generate Swift code for a GraphQL API using the command listed below.

$ ./apollo-ios-cli generate -v

GraphQL summary

By the way, GraphQL endpoints are usually accessible from a single URL.

In GraphQL, an object can have fields that return other objects or scalars. Scalars include strings, integers, floats, booleans, and enums. One key thing to remember is that every query must eventually resolve to a scalar or list of scalar types. Thinking about representing data in JSON might help you relate.

Finally, it is helpful to consider how GraphQL is often used to encapsulate access to a microservices architecture. It forms a standard interface that can be used to access data from many different services in a unified way.

However, the information hiding that GraphQL provides can make it difficult to understand the underlying architecture.

Therefore, having a solid understanding of GraphQL can assist in effectively working with scalable data architectures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment