Skip to content

Commit

Permalink
Use Apollo CLI for codegen (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadaj authored and martijnwalraven committed Jul 7, 2018
1 parent d71e772 commit 50c9c94
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 33 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Expand Up @@ -49,9 +49,11 @@ cache:
- ../starwars-server

before_install:
- nvm install 8
- nvm alias default 8
- (./scripts/install-or-update-starwars-server.sh)
- (cd ../starwars-server && npm start) &
- npm install -g apollo-codegen
- npm install -g apollo

script:
- set -o pipefail
Expand Down
2 changes: 1 addition & 1 deletion Apollo.podspec
Expand Up @@ -20,7 +20,7 @@ Pod::Spec.new do |s|

s.subspec 'Core' do |ss|
ss.source_files = 'Sources/Apollo/*.swift'
ss.resource = 'scripts/check-and-run-apollo-codegen.sh'
ss.resource = 'scripts/check-and-run-apollo-cli.sh'
end

# Apollo provides exactly one persistent cache out-of-the-box, as a reasonable default choice for
Expand Down
12 changes: 6 additions & 6 deletions Apollo.xcodeproj/project.pbxproj
Expand Up @@ -82,7 +82,7 @@
9FD637F51E6AD43F001EDBC8 /* Apollo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9FC750441D2A532C00458D91 /* Apollo.framework */; };
9FD637F61E6AD43F001EDBC8 /* Apollo.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9FC750441D2A532C00458D91 /* Apollo.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
9FE1C6E71E634C8D00C02284 /* PromiseTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FE1C6E61E634C8D00C02284 /* PromiseTests.swift */; };
9FE3F3981DADBD870072078F /* check-and-run-apollo-codegen.sh in Resources */ = {isa = PBXBuildFile; fileRef = 9FE3F3971DADBD870072078F /* check-and-run-apollo-codegen.sh */; };
9FE3F3981DADBD870072078F /* check-and-run-apollo-cli.sh in Resources */ = {isa = PBXBuildFile; fileRef = 9FE3F3971DADBD870072078F /* check-and-run-apollo-cli.sh */; };
9FE941D01E62C771007CDD89 /* Promise.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FE941CF1E62C771007CDD89 /* Promise.swift */; };
9FEB050D1DB5732300DA3B44 /* JSONSerializationFormat.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FEB050C1DB5732300DA3B44 /* JSONSerializationFormat.swift */; };
9FEC15B41E681DAD00D461B4 /* Collections.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FEC15B31E681DAD00D461B4 /* Collections.swift */; };
Expand Down Expand Up @@ -319,7 +319,7 @@
9FD637ED1E6ACF88001EDBC8 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
9FD637EF1E6ACF88001EDBC8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9FE1C6E61E634C8D00C02284 /* PromiseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PromiseTests.swift; sourceTree = "<group>"; };
9FE3F3971DADBD870072078F /* check-and-run-apollo-codegen.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; name = "check-and-run-apollo-codegen.sh"; path = "scripts/check-and-run-apollo-codegen.sh"; sourceTree = SOURCE_ROOT; };
9FE3F3971DADBD870072078F /* check-and-run-apollo-cli.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; name = "check-and-run-apollo-cli.sh"; path = "scripts/check-and-run-apollo-cli.sh"; sourceTree = SOURCE_ROOT; };
9FE941CF1E62C771007CDD89 /* Promise.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Promise.swift; sourceTree = "<group>"; };
9FEB050C1DB5732300DA3B44 /* JSONSerializationFormat.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONSerializationFormat.swift; sourceTree = "<group>"; };
9FEC15B31E681DAD00D461B4 /* Collections.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Collections.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -655,7 +655,7 @@
children = (
9FC750471D2A532C00458D91 /* Apollo.h */,
9FC750491D2A532C00458D91 /* Info.plist */,
9FE3F3971DADBD870072078F /* check-and-run-apollo-codegen.sh */,
9FE3F3971DADBD870072078F /* check-and-run-apollo-cli.sh */,
);
name = "Supporting Files";
sourceTree = "<group>";
Expand Down Expand Up @@ -963,7 +963,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
9FE3F3981DADBD870072078F /* check-and-run-apollo-codegen.sh in Resources */,
9FE3F3981DADBD870072078F /* check-and-run-apollo-cli.sh in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1006,7 +1006,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "APOLLO_FRAMEWORK_PATH=$(eval find $FRAMEWORK_SEARCH_PATHS -name \"Apollo.framework\" -maxdepth 1)\n\nif [ -z \"$APOLLO_FRAMEWORK_PATH\" ]; then\necho \"error: Couldn't find Apollo.framework in FRAMEWORK_SEARCH_PATHS; make sure to add the framework to your project.\"\nexit 1\nfi\n\ncd ${SRCROOT}/Tests/GitHubAPI\n$(find $APOLLO_FRAMEWORK_PATH -name 'check-and-run-apollo-codegen.sh') generate $(find . -name '*.graphql') --schema schema.json --merge-in-fields-from-fragment-spreads true --output API.swift";
shellScript = "APOLLO_FRAMEWORK_PATH=$(eval find $FRAMEWORK_SEARCH_PATHS -name \"Apollo.framework\" -maxdepth 1)\n\nif [ -z \"$APOLLO_FRAMEWORK_PATH\" ]; then\necho \"error: Couldn't find Apollo.framework in FRAMEWORK_SEARCH_PATHS; make sure to add the framework to your project.\"\nexit 1\nfi\n\ncd ${SRCROOT}/Tests/GitHubAPI\n$(find $APOLLO_FRAMEWORK_PATH -name 'check-and-run-apollo-cli.sh') codegen:generate --queries=\"$(find . -name '*.graphql')\" --schema=schema.json --mergeInFieldsFromFragmentSpreads API.swift";
};
9FCE2D061E6C251100E34457 /* Generate Apollo Client API */ = {
isa = PBXShellScriptBuildPhase;
Expand All @@ -1020,7 +1020,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "APOLLO_FRAMEWORK_PATH=$(eval find $FRAMEWORK_SEARCH_PATHS -name \"Apollo.framework\" -maxdepth 1)\n\nif [ -z \"$APOLLO_FRAMEWORK_PATH\" ]; then\necho \"error: Couldn't find Apollo.framework in FRAMEWORK_SEARCH_PATHS; make sure to add the framework to your project.\"\nexit 1\nfi\n\ncd ${SRCROOT}/Tests/StarWarsAPI\n$(find $APOLLO_FRAMEWORK_PATH -name 'check-and-run-apollo-codegen.sh') generate $(find . -name '*.graphql') --schema schema.json --merge-in-fields-from-fragment-spreads true --output API.swift";
shellScript = "APOLLO_FRAMEWORK_PATH=$(eval find $FRAMEWORK_SEARCH_PATHS -name \"Apollo.framework\" -maxdepth 1)\n\nif [ -z \"$APOLLO_FRAMEWORK_PATH\" ]; then\necho \"error: Couldn't find Apollo.framework in FRAMEWORK_SEARCH_PATHS; make sure to add the framework to your project.\"\nexit 1\nfi\n\ncd ${SRCROOT}/Tests/StarWarsAPI\n$(find $APOLLO_FRAMEWORK_PATH -name 'check-and-run-apollo-cli.sh') codegen:generate --queries=\"$(find . -name '*.graphql')\" --schema=schema.json --mergeInFieldsFromFragmentSpreads API.swift";
};
/* End PBXShellScriptBuildPhase section */

Expand Down
6 changes: 3 additions & 3 deletions docs/source/downloading-schema.md
Expand Up @@ -4,14 +4,14 @@ title: Downloading a schema

Apollo iOS requires a GraphQL schema file as input to the code generation process. A schema file is a JSON file that contains the results of an an introspection query. Conventionally this file is called `schema.json`, and you store it next to the `.graphql` files in your target.

You can use `apollo-codegen` to download a GraphQL schema by sending an introspection query to the server:
You can use `apollo` to download a GraphQL schema by sending an introspection query to the server:

```sh
apollo-codegen download-schema http://localhost:8080/graphql --output schema.json
apollo schema:download --endpoint=http://localhost:8080/graphql schema.json
```

If needed, you can use the `header` option to add additional HTTP headers to the request. For example, to include an authentication token, use `--header "Authorization: Bearer <token>"`:

```sh
apollo-codegen download-schema https://api.github.com/graphql --output schema.json --header "Authorization: Bearer <token>"
apollo schema:download --endpoint=http://localhost:8080/graphql --header="Authorization: Bearer <token>" schema.json
```
2 changes: 1 addition & 1 deletion docs/source/fetching-queries.md
Expand Up @@ -8,7 +8,7 @@ Note that when using Apollo iOS, you don't have to learn anything special about

Apollo iOS takes a schema and a set of `.graphql` files and uses these to generate code you can use to execute queries and access typed results.

> All `.graphql` files in your project (or the subset you specify as input to `apollo-codegen` if you customize the script you define as the code generation build phase) will be combined and treated as one big GraphQL document. That means fragments defined in one `.graphql` file are available to all other `.graphql` files for example, but it also means operation names and fragment names have to be unique and you will receive validation errors if they are not.
> All `.graphql` files in your project (or the subset you specify as input to `apollo` if you customize the script you define as the code generation build phase) will be combined and treated as one big GraphQL document. That means fragments defined in one `.graphql` file are available to all other `.graphql` files for example, but it also means operation names and fragment names have to be unique and you will receive validation errors if they are not.
<h2 id="creating-queries">Creating queries</h2>

Expand Down
10 changes: 5 additions & 5 deletions docs/source/installation.md
Expand Up @@ -59,7 +59,7 @@ You can also manually clone the [`apollo-ios` repository](https://github.com/apo

<h2 id="adding-build-step">Adding a code generation build step</h2>

In order to invoke `apollo-codegen` as part of the Xcode build process, create a build step that runs before "Compile Sources".
In order to invoke `apollo` as part of the Xcode build process, create a build step that runs before "Compile Sources".

1. On your application targets’ "Build Phases" settings tab, click the "+" icon and choose "New Run Script Phase". Create a Run Script, change its name to "Generate Apollo GraphQL API" and drag it just above "Compile Sources". Then add the following contents to the script area below the shell:

Expand All @@ -73,7 +73,7 @@ if [ -z "$APOLLO_FRAMEWORK_PATH" ]; then
fi

cd "${SRCROOT}/${TARGET_NAME}"
$APOLLO_FRAMEWORK_PATH/check-and-run-apollo-codegen.sh generate $(find . -name '*.graphql') --schema schema.json --output API.swift
$APOLLO_FRAMEWORK_PATH/check-and-run-apollo-cli.sh codegen:generate --queries="$(find . -name '*.graphql')" --schema=schema.json API.swift
```
for macOS Project
```sh
Expand All @@ -85,12 +85,12 @@ exit 1
fi

cd "${SRCROOT}/${TARGET_NAME}"
$APOLLO_FRAMEWORK_PATH/Versions/Current/Resources/check-and-run-apollo-codegen.sh generate $(find . -name '*.graphql') --schema schema.json --output API.swift
$APOLLO_FRAMEWORK_PATH/Versions/Current/Resources/check-and-run-apollo-cli.sh codegen:generate --queries="$(find . -name '*.graphql')" --schema=schema.json API.swift
```



The script above will invoke `apollo-codegen` through the `check-and-run-apollo-codegen.sh` wrapper script, which is actually contained in the `Apollo.framework` bundle. The main reason for this is to check whether the version of `apollo-codegen` 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.
The script above will invoke `apollo` through the `check-and-run-apollo-cli.sh` wrapper script, which is actually contained in the `Apollo.framework` bundle. The main reason for this 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.

<h2 id="adding-schema">Adding a schema file to your target directory</h2>

Expand All @@ -100,7 +100,7 @@ Apollo iOS requires a GraphQL schema file as input to the code generation proces

<h2 id="building-target">Build your target</h2>

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-codegen` script created above, otherwise you'll get a build error such as:
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, otherwise you'll get a build error such as:
> Cannot find GraphQL schema file [...]
<h2 id="adding-generated-api">Adding the generated API file to your target</h2>
Expand Down
@@ -1,26 +1,26 @@
# Only major and minor version should be specified here
REQUIRED_APOLLO_CODEGEN_VERSION=0.20
REQUIRED_APOLLO_CLI_VERSION=1.2.0

# Part of this code has been adapted from
# https://github.com/facebook/react-native/blob/master/scripts/react-native-xcode.sh

# This script is supposed to be invoked as part of the Xcode build process
# and relies on environment variables set by Xcode

install_apollo_codegen() {
install_apollo_cli() {
# Exit immediately if the command fails
set -e
npm install -g apollo-codegen@$REQUIRED_APOLLO_CODEGEN_VERSION
npm install -g apollo@$REQUIRED_APOLLO_CLI_VERSION
set +e
}

# We consider versions to be compatible if the major and minor versions match
are_versions_compatible() {
[[ "$(cut -d. -f1-2 <<< $1)" == "$(cut -d. -f1-2 <<< $2)" ]]
[[ "$(cut -d/ -f2 <<< $1 | cut -d. -f1-2)" == "$(cut -d/ -f2 <<< $2 | cut -d. -f1-2)" ]]
}

get_installed_version() {
version=$(apollo-codegen --version)
version=$(apollo -v)
if [[ $? -eq 0 ]]; then
echo "$version"
else
Expand All @@ -47,28 +47,28 @@ if [[ -x "$HOME/.nodenv/bin/nodenv" ]]; then
eval "$("$HOME/.nodenv/bin/nodenv" init -)"
fi

if [[ -s "$SRCROOT/node_modules/.bin/apollo-codegen" ]]; then
if [[ -s "$SRCROOT/node_modules/.bin/apollo" ]]; then
# If it's installed locally, use version build instead
set -x

exec "$SRCROOT/node_modules/.bin/apollo-codegen" "$@"
exec "$SRCROOT/node_modules/.bin/apollo" "$@"
else
# Otherwise use a global install
if ! type "apollo-codegen" >/dev/null 2>&1; then
echo "Can't find apollo-codegen. Installing..."
install_apollo_codegen
if ! type "apollo" >/dev/null 2>&1; then
echo "Can't find Apollo CLI. Installing..."
install_apollo_cli
fi

INSTALLED_APOLLO_CODEGEN_VERSION="$(get_installed_version)"
INSTALLED_APOLLO_CLI_VERSION="$(get_installed_version)"

if ! are_versions_compatible $INSTALLED_APOLLO_CODEGEN_VERSION $REQUIRED_APOLLO_CODEGEN_VERSION; then
echo "The version of Apollo.framework in your project requires apollo-codegen $REQUIRED_APOLLO_CODEGEN_VERSION, \
but $INSTALLED_APOLLO_CODEGEN_VERSION seems to be installed. Installing..."
install_apollo_codegen
if ! are_versions_compatible $INSTALLED_APOLLO_CLI_VERSION $REQUIRED_APOLLO_CLI_VERSION; then
echo "The version of Apollo.framework in your project requires Apollo CLI $REQUIRED_APOLLO_CLI_VERSION, \
but $INSTALLED_APOLLO_CLI_VERSION seems to be installed. Installing..."
install_apollo_cli
fi

# Print commands before executing them (useful for troubleshooting)
set -x

exec apollo-codegen "$@"
exec apollo "$@"
fi

0 comments on commit 50c9c94

Please sign in to comment.