Skip to content

Commit 19a0fa4

Browse files
update codegen build step instructions for bundled codegen
1 parent ecc13d2 commit 19a0fa4

1 file changed

Lines changed: 35 additions & 45 deletions

File tree

docs/source/installation.md

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ Follow along with these steps (described in detail below) to use Apollo iOS in y
1616

1717
## Installing the Apollo framework
1818

19-
You can install `Apollo.framework` into your project using [Swift Package Manager](#swift-package-manager-installation), [CocoaPods](#cocoapods-installation), [carthage](#carthage-installation), or by [manually integrating it with Xcode](#manual-integration).
19+
You can install `Apollo.framework` into your project using [Swift Package Manager](#swift-package-manager-installation), [CocoaPods](#cocoapods-installation), [Carthage](#carthage-installation), or by [manually integrating it with Xcode](#manual-integration).
2020

2121
### Swift Package Manager Installation
2222

23-
> **NOTE**: These instructions are intended for usage on Xcode 11 and higher. Xcode 11 is the first version of Xcode that integrates Swift Package manager and makes it _way_ easier to use than it was at the command line. If you are using older versions of Xcode, we recommend using [CocoaPods](#CocoaPods-Installation)
23+
> **NOTE**: These instructions are intended for usage on Xcode 11 and higher. Xcode 11 is the first version of Xcode that integrates Swift Package manager and makes it _way_ easier to use than it was at the command line. If you are using older versions of Xcode, we recommend using [CocoaPods](#cocoapods-installation)
2424
2525
1. Go to **File > Swift Packages > Add Package Dependency... **
2626

@@ -143,35 +143,28 @@ If you have the Xcode add-ons installed, you can use the Xcode companion view to
143143
144144
## Adding a code generation build step
145145

146-
In order to invoke `apollo` as part of the Xcode build process, create a build step that runs before "Compile Sources" to invoke `apollo` through the `check-and-run-apollo-cli.sh` wrapper script.
146+
Code generation uses your `.graphql` files to generate API code that will help you send queries, subscriptions, and mutations, as well as parse and cache responses. To run code generation as part of the Xcode build process, you need to create a build step that runs before "Compile Sources" to invoke a wrapper script.
147147

148-
The main reason for calling the wrapper is to check whether the version of `apollo` installed on your system is compatible with the framework version installed in your project, and to warn you if it isn't. Without this check, you could end up generating code that is incompatible with the runtime code contained in the framework.
148+
The wrapper will call through to the included binaries and files that constitute the `apollo` command-line interface. This ensures that you can use our tooling without having to worry about NPM Version Hell™, and that the version of the framework you're using is compatible with the version of the codegen you're using.
149149

150150
The location of this wrapper script is slightly different based on how you've integrated Apollo into you project, but the first steps are the same everywhere:
151151

152152
1. On your application target's **Build Phases** settings tab, click the **+** icon and choose **New Run Script Phase**.
153153
2. In the created Run Script, change its name to **Generate Apollo GraphQL API**
154154
3. Drag this new run script just above **Compile Sources** in your list of **Build Phases** so that it executes before your code is compiled.
155-
4. Add the appropriate contents to the run script from the options below.
155+
4. Add the contents of the appropriate run script for the package manager you're using from this list:
156156

157-
### If you ARE integrating Apollo using CocoaPods
157+
- [Swift Package Manager](#swift-package-manager-run-script)
158+
- [CocoaPods](#cocoapods-run-script)
159+
- [Carthage](#carthage-run-script)
160+
- [Manual Integration](#manual-integration-run-script)
158161

159-
Our CocoaPods install includes the code-generation script as a file which will not be added to the framework. Since this is always installed in a consistent place, you can use the same path to it. Add the following to the Run Script:
162+
### Swift Package Manager Run Script
160163

161-
```sh
162-
SCRIPT_PATH="${PODS_ROOT}/Apollo/scripts"
163-
cd "${SRCROOT}/${TARGET_NAME}"
164-
"${SCRIPT_PATH}"/check-and-run-apollo-cli.sh codegen:generate --target=swift --includes=./**/*.graphql --localSchemaFile="schema.json" API.swift
165-
```
166-
167-
### If you are integrating Apollo using SPM + Xcode 11 [BETA]
168-
169-
> NOTE: These instructions are as of Xcode 11, beta 4. Please file an issue if anything has changed in newer betas or in the final release!
170-
171-
If you're using Xcode 11, SPM will check out the appropriate build script along with the rest of the files. Add the following to your Run Script build phase:
164+
If you're using Xcode 11 or higher, SPM will check out the appropriate build script along with the rest of the files when it checks out the repo. Add the following to your Run Script build phase:
172165

173166
```sh
174-
# Go to the build root and go back up to where SPM keeps the apollo iOS framework checked out.
167+
# Go to the build root and go back up to where SPM keeps the Apollo iOS repo checked out.
175168
cd "${BUILD_ROOT}"
176169
cd "../../SourcePackages/checkouts/apollo-ios/scripts"
177170

@@ -183,51 +176,48 @@ if [ -z "${APOLLO_SCRIPT_PATH}" ]; then
183176
fi
184177

185178
cd "${SRCROOT}/${TARGET_NAME}"
186-
"${APOLLO_SCRIPT_PATH}"/check-and-run-apollo-cli.sh codegen:generate --target=swift --includes=./**/*.graphql --localSchemaFile="schema.json" API.swift
179+
"${APOLLO_SCRIPT_PATH}"/run-bundled-codegen.sh codegen:generate --target=swift --includes=./**/*.graphql --localSchemaFile="schema.json" API.swift
187180
```
188181

189182
> NOTE: If you try to use this with command line SPM, when you regenerate your `xcodeproj` this build script will get wiped out. We strongly recommend using Xcode 11's built-in SPM handling rather than the command line because of this.
190183
191-
### If you're NOT integrating Apollo using CocoaPods
192-
193-
In this case, the `check-and-run-apollo-cli.sh` file is bundled into the framework. The procedures to call it are slightly different based on whether you're using an iOS or macOS target because of the way the frameworks are compiled.
184+
### CocoaPods Run Script
194185

195-
📱 For an **iOS** target or a **Cocoa Touch Framework**, add the following to your Run Script build phase:
186+
Our CocoaPods install includes the code-generation scripts and binaries of the `apollo` CLI client as files which will not be added to the framework, but which you can still call from a Run Script Build Phase. Add the following to the Run Script:
196187

197188
```sh
198-
# Do some magic so we can make sure `FRAMEWORK_SEARCH_PATHS` works correctly when there's a space in the scheme or the folder name.
199-
QUOTED_FRAMEWORK_SEARCH_PATHS=\"$(echo $FRAMEWORK_SEARCH_PATHS | tr -d '"' | sed -e 's/ \//" "\//g')\"
189+
SCRIPT_PATH="${PODS_ROOT}/Apollo/scripts"
190+
cd "${SRCROOT}/${TARGET_NAME}"
191+
"${SCRIPT_PATH}"/run-bundled-codegen.sh codegen:generate --target=swift --includes=./**/*.graphql --localSchemaFile="schema.json" API.swift
192+
```
200193

201-
# Get the first result searching for the framework
202-
APOLLO_FRAMEWORK_PATH="$(eval find ${QUOTED_FRAMEWORK_SEARCH_PATHS} -name "Apollo.framework" -maxdepth 1 -print | head -n 1)"
194+
### Carthage Run Script
203195

204-
if [ -z "${APOLLO_FRAMEWORK_PATH}" ]; then
205-
echo "error: Couldn't find Apollo.framework in FRAMEWORK_SEARCH_PATHS; make sure to add the framework to your project."
206-
exit 1
207-
fi
196+
In the never-ending battle of what's going to be more painful when it comes to dependency management, we've decided to make working with Carthage a bit more of a pain in order to make working with NPM way less of a pain.
208197

209-
cd "${SRCROOT}/${TARGET_NAME}"
210-
"${APOLLO_FRAMEWORK_PATH}"/check-and-run-apollo-cli.sh codegen:generate --target=swift --includes=./**/*.graphql --localSchemaFile="schema.json" API.swift
211-
```
198+
The scripts and binaries which you need to generate code will be included in the `Carthage/Checkouts` folder. If this folder is not checked into version control, all developers on a team (and your CI machine) will need to run `carthage checkout` when changes are made to the version to ensure they have the correct underlying binaries and scripts.
212199

213-
💻 For a **macOS** or a **Cocoa Framework** target, add the following to your Run Script build phase:
200+
Once everyone's on the same page about that, you should be able to use this build script:
214201

215202
```sh
216-
# Do some magic so we can make sure `FRAMEWORK_SEARCH_PATHS` works correctly when there's a space in the scheme or the folder name.
217-
QUOTED_FRAMEWORK_SEARCH_PATHS=\"$(echo $FRAMEWORK_SEARCH_PATHS | tr -d '"' | sed -e 's/ \//" "\//g')\"
203+
SCRIPT_PATH="${SRCROOT}/Carthage/Checkouts/apollo-ios/scripts"
204+
cd "${SRCROOT}/${TARGET_NAME}"
205+
"${SCRIPT_PATH}"/run-bundled-codegen.sh codegen:generate --target=swift --includes=./**/*.graphql --localSchemaFile="schema.json" API.swift
206+
```
218207

219-
# Get the first result searching for the framework
220-
APOLLO_FRAMEWORK_PATH="$(eval find ${QUOTED_FRAMEWORK_SEARCH_PATHS} -name "Apollo.framework" -maxdepth 1 -print | head -n 1)"
208+
### Manual Integration Run Script
221209

222-
if [ -z "${APOLLO_FRAMEWORK_PATH}" ]; then
223-
echo "error: Couldn't find Apollo.framework in FRAMEWORK_SEARCH_PATHS; make sure to add the framework to your project."
224-
exit 1
225-
fi
210+
You'll need to find the place where you've downloaded the Apollo iOS SDK, and manually set the `SCRIPT_PATH` to that folder. Usually this works best if it's related to `SRCROOT` so it's always the same
211+
212+
Replace `__YOUR_PATH_TO_THE_SCRIPT_FOLDER__` with (you guessed it) your path to the script folder in this script:
226213

214+
```sh
215+
SCRIPT_PATH=__YOUR_PATH_TO_THE_SCRIPT_FOLDER__
227216
cd "${SRCROOT}/${TARGET_NAME}"
228-
"${APOLLO_FRAMEWORK_PATH}"/Versions/Current/Resources/check-and-run-apollo-cli.sh codegen:generate --target=swift --includes=./**/*.graphql --localSchemaFile="schema.json" API.swift
217+
"${SCRIPT_PATH}"/run-bundled-codegen.sh codegen:generate --target=swift --includes=./**/*.graphql --localSchemaFile="schema.json" API.swift
229218
```
230219

220+
231221
## Build your target
232222

233223
At this point, you can try building your target in Xcode. This will verify that the `schema.json` file can be found by the `apollo` script created above, and run the codegen.

0 commit comments

Comments
 (0)