Help with Configuring Apollo iOS Code Generation

Hi everyone,

I’m using Apollo and GraphQL in an iOS project, and I’m trying to configure the code generation for my queries and mutations. Following the Apollo documentation, I noticed that it generates a Swift Package Manager (SPM) package by default. However, if I want to add multiple queries, this approach could potentially lead to unnecessary SPM packages, which I would prefer to avoid.

Currently, my configuration looks like this:

{
  "schemaNamespace": "RocketReserverAPI",
  "input": {
    "operationSearchPaths": [
      "**/*.graphql"
    ],
    "schemaSearchPaths": [
      "**/*.graphqls"
    ]
  },
  "output": {
    "testMocks": {
      "none": {}
    },
    "schemaTypes": {
      "path": "./RocketReserverAPI",
      "moduleType": {
        "embeddedInTarget": {
          "name": "GraphQLwithApolloSWAPI"
        }
      }
    },
    "operations": {
      "inSchemaModule": {}
    }
  },
  "schemaDownloadConfiguration": {
    "downloadMethod": {
      "introspection": {
        "endpointURL": "https://swapi-graphql.netlify.app/.netlify/functions/index",
        "httpMethod": {
          "POST": {}
        },
        "includeDeprecatedInputValues": false,
        "outputFormat": "SDL"
      }
    },
    "downloadTimeout": 60,
    "headers": [],
    "outputPath": "./graphql/schema.graphqls"
  }
}

When I run apollo-cli generate, I encounter the following error:

Error: JavaScriptError: GraphQLError-Syntax Error: Expected Name, found String "__schema".-GraphQLError@
c@
expectToken@
parseName@
parseField@
many@
parseSelectionSet@
parseOperationDefinition@
many@
parseDocument@

Additionally, I came across external tutorials that suggest using a script like this:

# Derived Data Path where the source packages are checked out.
DERIVED_DATA_CANDIDATE="${BUILD_ROOT}"

while ! [ -d "${DERIVED_DATA_CANDIDATE}/SourcePackages" ]; do
  if [ "${DERIVED_DATA_CANDIDATE}" = / ]; then
    echo >&2 "error: Unable to locate SourcePackages directory from BUILD_ROOT: '${BUILD_ROOT}'"
    exit 1
  fi

  DERIVED_DATA_CANDIDATE="$(dirname "${DERIVED_DATA_CANDIDATE}")"
done

# Grab a reference to the directory where scripts are checked out
SCRIPT_PATH="${DERIVED_DATA_CANDIDATE}/SourcePackages/checkouts/apollo-ios/scripts"

if [ -z "${SCRIPT_PATH}" ]; then
    echo >&2 "error: Couldn't find the CLI script in your checked out SPM packages; make sure to add the framework to your project."
    exit 1
fi

cd "${SRCROOT}/${TARGET_NAME}"
"${SCRIPT_PATH}"/run-bundled-codegen.sh codegen:generate --target=swift --includes=./**/*.graphql --localSchemaFile="schema.json" GraphQL/API.swift

However, in Apollo iOS version 1.15.3, the run-bundled-codegen.sh script seems to be missing. It might have been removed or replaced.

What I’d really like to achieve is to automatically generate my models inside my project, not in an external package. Is this possible with the latest version of Apollo? If so, what’s the recommended approach?

Any guidance would be greatly appreciated. Thank you in advance!

Hi :wave:

Following the Apollo documentation, I noticed that it generates a Swift Package Manager (SPM) package by default. However, if I want to add multiple queries, this approach could potentially lead to unnecessary SPM packages, which I would prefer to avoid

The SPM package output is our recommended configuration yes. It will generate an SPM package per schema not per query, so if you do choose to generate an SPM package all your operations against the same schema will be in the same SPM package. The only thing to note here is if you choose to use the relative(subpath:accessModifier:) or absolute(path:accessModifier:) operation output options because those will generate the Swift operation models where the operation files are found which could be in different internal modules.

Error: JavaScriptError: GraphQLError-Syntax Error: Expected Name, found String

This error looks like a problem parsing the schema or operation documents. Your configuration looks correct and is valid JSON so I don’t think that is the issue.

However, in Apollo iOS version 1.15.3, the run-bundled-codegen.sh script seems to be missing. It might have been removed or replaced.

That script was the recommended approach for the 0.x (legacy) versions. We no longer have support for that in 1.x and recommend you use the CLI tool instead.

What I’d really like to achieve is to automatically generate my models inside my project, not in an external package. Is this possible with the latest version of Apollo? If so, what’s the recommended approach?

You should then use the embeddedInTarget(name:accessModifier:) output option, which you already have in the configuration file.

Once you resolve the errors in your schema or operation files you should get the code generated as needed.

I truly appreciate your contribution; it was incredibly helpful to me. It has greatly enhanced my understanding of how Apollo is managed!

1 Like