Skip to content

Commit 41537cf

Browse files
Merge pull request #766 from apollographql/rm/node-nasties
Include Node Binaries Rather Than Fighting With NPM
2 parents af353ba + 2da7517 commit 41537cf

14 files changed

Lines changed: 140 additions & 66 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ Carthage/Build
5252

5353
# Local Node Modules for Apollo CLI
5454
node_modules/
55-
package-lock.json
55+
package-lock.json
56+
scripts/apollo

Apollo.podspec

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ Pod::Spec.new do |s|
2222

2323
s.subspec 'Core' do |ss|
2424
ss.source_files = 'Sources/Apollo/*.swift'
25-
ss.preserve_paths = ['scripts/check-and-run-apollo-cli.sh', 'scripts/check-and-run-apollo-codegen.sh']
25+
ss.preserve_paths = [
26+
'scripts/check-and-run-apollo-cli.sh',
27+
'scripts/check-and-run-apollo-codegen.sh',
28+
'scripts/run-bundled-codegen.sh',
29+
'scripts/apollo.tar.gz',
30+
]
2631
end
2732

2833
# Apollo provides exactly one persistent cache out-of-the-box, as a reasonable default choice for

Apollo.xcodeproj/project.pbxproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@
266266
90690D2422433C8000FC2E54 /* Apollo-Target-PerformanceTests.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Apollo-Target-PerformanceTests.xcconfig"; sourceTree = "<group>"; };
267267
90690D2522433CAF00FC2E54 /* Apollo-Target-TestSupport.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Apollo-Target-TestSupport.xcconfig"; sourceTree = "<group>"; };
268268
9B708AAC2305884500604A11 /* ApolloClientProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApolloClientProtocol.swift; sourceTree = "<group>"; };
269+
9B74BCBE2333F4ED00508F84 /* run-bundled-codegen.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; name = "run-bundled-codegen.sh"; path = "scripts/run-bundled-codegen.sh"; sourceTree = SOURCE_ROOT; };
269270
9B78C71B2326E859000C8C32 /* ErrorGenerationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorGenerationTests.swift; sourceTree = "<group>"; };
270271
9B8D864E22E7A846001F6D50 /* RepoURL.graphql */ = {isa = PBXFileReference; lastKnownFileType = text; path = RepoURL.graphql; sourceTree = "<group>"; };
271272
9B95EDBF22CAA0AF00702BB2 /* GETTransformerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GETTransformerTests.swift; sourceTree = "<group>"; };
@@ -780,6 +781,7 @@
780781
children = (
781782
9FC750471D2A532C00458D91 /* Apollo.h */,
782783
9FC750491D2A532C00458D91 /* Info.plist */,
784+
9B74BCBE2333F4ED00508F84 /* run-bundled-codegen.sh */,
783785
9FE3F3971DADBD870072078F /* check-and-run-apollo-cli.sh */,
784786
9F2A74DA2137C21500E63C85 /* check-and-run-apollo-codegen.sh */,
785787
);
@@ -1141,7 +1143,8 @@
11411143
);
11421144
runOnlyForDeploymentPostprocessing = 0;
11431145
shellPath = /bin/sh;
1144-
shellScript = "# 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.\nQUOTED_FRAMEWORK_SEARCH_PATHS=\\\"$(echo $FRAMEWORK_SEARCH_PATHS | tr -d '\"' | sed -e 's/ \\//\" \"\\//g')\\\"\n\n# Get the first result searching for the framework\nAPOLLO_FRAMEWORK_PATH=\"$(eval find ${QUOTED_FRAMEWORK_SEARCH_PATHS} -name \"Apollo.framework\" -maxdepth 1 -print | head -n 1)\"\n\nif [ -z \"${APOLLO_FRAMEWORK_PATH}\" ]; then\n echo \"error: Couldn't find Apollo.framework in FRAMEWORK_SEARCH_PATHS; make sure to add the framework to your project.\"\n exit 1\nfi\n\ncd ${SRCROOT}/Tests/StarWarsAPI\nif [[ $SDK_NAME == *\"macos\"* ]]; then\n # Run the mac script\n\"${APOLLO_FRAMEWORK_PATH}\"/Versions/Current/Resources/check-and-run-apollo-cli.sh codegen:generate --target=swift --localSchemaFile=\"schema.json\" --includes=./**/*.graphql --mergeInFieldsFromFragmentSpreads API.swift\nelse\n # run the non-mac script\n\"${APOLLO_FRAMEWORK_PATH}\"/check-and-run-apollo-cli.sh codegen:generate --target=swift --localSchemaFile=\"schema.json\" --includes=./**/*.graphql --mergeInFieldsFromFragmentSpreads API.swift\nfi\n";
1146+
shellScript = "SCRIPT_PATH=\"${SRCROOT}/scripts/run-bundled-codegen.sh\"\n\ncd \"${SRCROOT}/Tests/StarWarsAPI\"\n\n\"${SCRIPT_PATH}\" codegen:generate --target=swift --localSchemaFile=\"schema.json\" --includes=./**/*.graphql --mergeInFieldsFromFragmentSpreads API.swift\n";
1147+
showEnvVarsInLog = 0;
11451148
};
11461149
/* End PBXShellScriptBuildPhase section */
11471150

Tests/StarWarsAPI/API.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public struct ReviewInput: GraphQLMapConvertible {
7272
/// Comment about the movie, optional
7373
public var commentary: Swift.Optional<String?> {
7474
get {
75-
return graphQLMap["commentary"] as? Swift.Optional<String?> ?? .none
75+
return graphQLMap["commentary"] as? Swift.Optional<String?> ?? Swift.Optional<String?>.none
7676
}
7777
set {
7878
graphQLMap.updateValue(newValue, forKey: "commentary")
@@ -82,7 +82,7 @@ public struct ReviewInput: GraphQLMapConvertible {
8282
/// Favorite color, optional
8383
public var favoriteColor: Swift.Optional<ColorInput?> {
8484
get {
85-
return graphQLMap["favorite_color"] as? Swift.Optional<ColorInput?> ?? .none
85+
return graphQLMap["favorite_color"] as? Swift.Optional<ColorInput?> ?? Swift.Optional<ColorInput?>.none
8686
}
8787
set {
8888
graphQLMap.updateValue(newValue, forKey: "favorite_color")

docs/source/installation.md

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,29 @@ 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 CocoaPods, Carthage, or by manually integrating it with Xcode.
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

21-
### CocoaPods
21+
### Swift Package Manager Installation
22+
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)
24+
25+
1. Go to **File > Swift Packages > Add Package Dependency... **
26+
27+
![add a dependency](screenshot/spm_packages_add_dependency.png)
28+
29+
1. Paste the URL to the Apollo iOS repo on GitHub into the search bar, then hit the **Next** button:
30+
31+
![paste in the url](screenshot/spm_paste_url.png)
32+
33+
1. Select what version you want to use, then hit next. Xcode will automatically suggest the current version `Up to Next Major`, we **strongly** suggest that while the iOS SDK is on a `0.x.x` version scheme, you select `Up To Next Minor` instead, as we will still be releasing breaking changes on minor versions:
34+
35+
![select a version](screenshot/spm_select_version.png)
36+
37+
1. Select which packages you want to use. If you're just getting started, try selecting just the main `Apollo` library first - you can always come back and add the other packages later if you need them. Then hit finish.
38+
39+
![select the packages you want to use](screenshot/spm_select_package.png)
40+
41+
### CocoaPods Installation
2242

2343
1. Because Apollo iOS has been written using Swift 5, you need to use version `1.7.0` or higher. You can install CocoaPods using:
2444

@@ -34,7 +54,7 @@ You can install `Apollo.framework` into your project using CocoaPods, Carthage,
3454

3555
1. Use the `.xcworkspace` file generated by CocoaPods to work on your project.
3656

37-
### Carthage
57+
### Carthage Installation
3858

3959
Since Carthage [does not allow choosing which schemes in a repo to build](https://github.com/Carthage/Carthage/issues/1874), we've moved our dependencies to a [`Cartfile.private`](https://github.com/apollographql/apollo-ios/blob/master/Cartfile.private) file so that those dependencies are not forced on people using only the `Apollo` framework and not either of our optional frameworks, `ApolloSQLite` or `ApolloWebSocket`.
4060

@@ -66,7 +86,7 @@ This makes setup a hair more complicated if you *are* using those, but is a big
6686

6787
This script works around an [App Store submission bug](http://www.openradar.me/radar?id=6409498411401216) triggered by universal binaries and ensures that necessary bitcode-related files and dSYMs are copied when archiving.
6888

69-
### Manual integration
89+
### Manual Integration
7090

7191
You can also manually clone the [`apollo-ios` repository](https://github.com/apollostack/apollo-ios), drag `Apollo.xcodeproj` into your project or workspace, add a dependency on `Apollo.framework` to your target.
7292

@@ -123,35 +143,28 @@ If you have the Xcode add-ons installed, you can use the Xcode companion view to
123143
124144
## Adding a code generation build step
125145

126-
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.
127147

128-
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.
129149

130-
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:
150+
The location of this wrapper script is slightly different based on how you've integrated Apollo into your project, but the first steps are the same everywhere:
131151

132152
1. On your application target's **Build Phases** settings tab, click the **+** icon and choose **New Run Script Phase**.
133153
2. In the created Run Script, change its name to **Generate Apollo GraphQL API**
134154
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.
135-
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:
136156

137-
### 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)
138161

139-
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
140163

141-
```sh
142-
SCRIPT_PATH="${PODS_ROOT}/Apollo/scripts"
143-
cd "${SRCROOT}/${TARGET_NAME}"
144-
"${SCRIPT_PATH}"/check-and-run-apollo-cli.sh codegen:generate --target=swift --includes=./**/*.graphql --localSchemaFile="schema.json" API.swift
145-
```
146-
147-
### If you are integrating Apollo using SPM + Xcode 11 [BETA]
148-
149-
> 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!
150-
151-
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:
152165

153166
```sh
154-
# 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.
155168
cd "${BUILD_ROOT}"
156169
cd "../../SourcePackages/checkouts/apollo-ios/scripts"
157170

@@ -163,63 +176,54 @@ if [ -z "${APOLLO_SCRIPT_PATH}" ]; then
163176
fi
164177

165178
cd "${SRCROOT}/${TARGET_NAME}"
166-
"${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
167180
```
168181

169182
> 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.
170183
171-
### If you're NOT integrating Apollo using CocoaPods
172-
173-
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
174185

175-
📱 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:
176187

177188
```sh
178-
# 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.
179-
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+
```
180193

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

184-
if [ -z "${APOLLO_FRAMEWORK_PATH}" ]; then
185-
echo "error: Couldn't find Apollo.framework in FRAMEWORK_SEARCH_PATHS; make sure to add the framework to your project."
186-
exit 1
187-
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.
188197

189-
cd "${SRCROOT}/${TARGET_NAME}"
190-
"${APOLLO_FRAMEWORK_PATH}"/check-and-run-apollo-cli.sh codegen:generate --target=swift --includes=./**/*.graphql --localSchemaFile="schema.json" API.swift
191-
```
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.
192199

193-
💻 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:
194201

195202
```sh
196-
# 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.
197-
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+
```
198207

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

202-
if [ -z "${APOLLO_FRAMEWORK_PATH}" ]; then
203-
echo "error: Couldn't find Apollo.framework in FRAMEWORK_SEARCH_PATHS; make sure to add the framework to your project."
204-
exit 1
205-
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
206211

212+
Replace `__YOUR_PATH_TO_THE_SCRIPT_FOLDER__` with (you guessed it) your path to the script folder in this script:
213+
214+
```sh
215+
SCRIPT_PATH=__YOUR_PATH_TO_THE_SCRIPT_FOLDER__
207216
cd "${SRCROOT}/${TARGET_NAME}"
208-
"${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
209218
```
210219

220+
211221
## Build your target
212222

213223
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.
214224

215225
### Troubleshooting
216226

217-
If you've installed `apollo` globally with NPM and the script still seems to be having trouble finding it, add the following to the beginning of your Run Script Build Phase:
218-
219-
```bash
220-
source ~/.bash_profile
221-
```
222-
223227
If you get this error:
224228

225229
> Cannot find GraphQL schema file [...]
@@ -230,13 +234,7 @@ If you get this error:
230234

231235
> No operations or fragments found to generate code for.
232236
233-
If you don't have any `.graphql` files in your build tree, and you need to create at least `.graphql` file with a valid query.
234-
235-
If you get this error:
236-
237-
> Ensure that there is only one instance of "graphql" in the node_modules directory. If different versions of "graphql" are the dependencies of other relied on modules, use "resolutions" to ensure only one version is installed.
238-
239-
Delete the `node_modules` folder in your source root and rebuild.
237+
If you don't have any `.graphql` files in your build tree, and you need to create at least `.graphql` file with a valid query. If you need to validate the structure of your query, please use [the GraphQL Formatter tool](http://toolbox.sangria-graphql.org/format).
240238

241239
## Adding the generated API file to your target
242240

326 KB
Loading
72.8 KB
Loading
340 KB
Loading
339 KB
Loading
346 KB
Loading

0 commit comments

Comments
 (0)