Apollo-ios: Swift 11, SwiftUI, Xcode11 beta, Apollo GraphQL, WatchOS6

Created on 2 Sep 2019  Â·  22Comments  Â·  Source: apollographql/apollo-ios

Hey guys I'm new here, and in general in all of this so please bear with me.

I am looking into how to setup Xcode11 / SwiftUI with Apollo in a WatchOS6 project, and are having a hard time finding some good resources on exactly how to do this.

I am following this guide

I have installed 'Apollo (0.15.0)' via CocoaPods into my Xcode 11 swiftUI project.
I noticed that it installed 'Starscream (3.1.0)' also.

At first in my pod file I installed the pods with the platform set as :ios, '9.0'.
But since changed that line to platform :ios, '11.0' and I ran pod update and pod install again just to be sure. I don't know if this might have broken my program. But I can't run the program now even though I haven't changed anything in the code.

I am currently getting this (full) error:
''Command PhaseScriptExecution failed with a nonzero exit code''

I noticed that I have 3 targets in my project but only one of the targets has a Compile Sources script so I put it in that one.
The targets I have are called: 1: WatchOS6GraphQLApolloIOS, 2: WatchOS6GraphQLApolloIOS WatchKit App, 3: WatchOS6GraphQLApolloIOS WatchKit Extension.
I added this line on top of the script: 'source ~/.bash_profile'

I have my project in a folder called sandbox that I manage via Sourcetree, I don't know if that could cause any trouble in any way?
Both Apollo and adding libraries in this way is very new to me so sorry if I am saying stuff that doesn't make sense.

What I have done
I have a .json schema in my project
I have installed pods
I have a .graphql with a simple query inside of it
I have added a script in the build phases and placed it above compile sources
I have added source ~/.bash_profile on the top of the script
I have tried both scripts for both of them provided under: 'If you ARE integrating Apollo using CocoaPods', and 'If you are integrating Apollo using SPM + Xcode 11 [BETA]'

I hope you can help or at least give me some insights or point me in the right direction
Phillip

beta-xcode dependency-management

All 22 comments

So this looks to be the root of your issue:

/Users/phillipeismark/Library/Developer/Xcode/DerivedData/WatchOS6GraphQLApolloIOS-azskjqtoubvpapbsywgnrrcfftaq/Build/Intermediates.noindex/WatchOS6GraphQLApolloIOS.build/Debug-watchsimulator/WatchOS6GraphQLApolloIOS WatchKit Extension.build/Script-76EAEB00231D38DC00BE1CF5.sh: line 14: /Users/phillipeismark/Library/Developer/Xcode/DerivedData/WatchOS6GraphQLApolloIOS-azskjqtoubvpapbsywgnrrcfftaq/Build/Products/check-and-run-apollo-cli.sh: No such file or directory

You need to adjust the run script you copied to call where the file check-and-run-apollo-cli.sh exists.

So this looks to be the root of your issue:

/Users/phillipeismark/Library/Developer/Xcode/DerivedData/WatchOS6GraphQLApolloIOS-azskjqtoubvpapbsywgnrrcfftaq/Build/Intermediates.noindex/WatchOS6GraphQLApolloIOS.build/Debug-watchsimulator/WatchOS6GraphQLApolloIOS WatchKit Extension.build/Script-76EAEB00231D38DC00BE1CF5.sh: line 14: /Users/phillipeismark/Library/Developer/Xcode/DerivedData/WatchOS6GraphQLApolloIOS-azskjqtoubvpapbsywgnrrcfftaq/Build/Products/check-and-run-apollo-cli.sh: No such file or directory

You need to adjust the run script you copied to call where the file check-and-run-apollo-cli.sh exists.

Hey Casey-chow, thanks for your reply.

Does that mean that I need to replace everything that's inside ${} like ${PODS_ROOT} to a directory of where to find it so it looks like:

${/Users/phillipeismark/sandbox/WatchOS6Apollo/Pods/Apollo/scripts}?

Or is it only some of them or one of them?

"${SRCROOT}/ and ${TARGET_NAME}", or "${SCRIPT_PATH}" only?

Hey guys a little update to provide some more hopefully useful information.

I made a new project and I made some changes to the script so that it looks like this now:

SCRIPT_PATH="/Users/phillipeismark/sandbox/WatchOS6Apollo/Pods/Apollo/scripts" cd /Users/phillipeismark/sandbox/WatchOS6Apollo/Pods/Apollo/scripts/check-and-run-apollo-cli.sh codegen:generate --target=swift --includes=./**/*.graphql --localSchemaFile="instrospectionSchema.json" API.swift
Heres a full list of all of the different scripts I have tried

When I try to run the program it throws these errors:

/Users/phillipeismark/sandbox/WatchOS6Apollo/Pods/Apollo/scripts/check-and-run-apollo-cli.sh: line 61: npx: command not found
warning: Apollo iOS requires version 2.17.x of the Apollo CLI to be installed either globally or in a local node_modules directory.
warning: Installing [email protected] in your project directory to avoid version conflicts...
/Users/phillipeismark/sandbox/WatchOS6Apollo/Pods/Apollo/scripts/check-and-run-apollo-cli.sh: line 76: npm: command not found Command PhaseScriptExecution failed with a nonzero exit code

I am currently running:
macOS Catalina beta version 10.15
Xcode Version 11.0 beta 6
Using Apollo (0.15.0) installed via CocoaPods
I created a swiftUI WatchOS6 project and was able to run it after installing pods, but hit a wall when I try to put in the script.

I noticed that the error mentions a version 2.17 but the Apollo pod is version (0.15.0).

I have made a new project that's a normal SwiftUI project for the phone and not the watch.

I am getting these errors:

Showing All Messages
/Users/phillipeismark/sandbox/SwiftUIApollo/Pods/Apollo/scripts/check-and-run-apollo-cli.sh: line 61: npx: command not found

Apollo iOS requires version 2.17.x of the Apollo CLI to be installed either globally or in a local node_modules directory.

Installing [email protected] in your project directory to avoid version conflicts...

/Users/phillipeismark/sandbox/SwiftUIApollo/Pods/Apollo/scripts/check-and-run-apollo-cli.sh: line 76: npm: command not found

Command PhaseScriptExecution failed with a nonzero exit code

And this is the script I am running, I have made no changes to it I just copied directly from the guide

npm: command not found

So it looks like you don't have NPM installed. You can install it here.

The instructions for this with Xcode 11 haven't been updated as Xcode 11 is still in Beta, but I'm gonna be honest: You're almost certainly better off using Swift Package Manager with Xcode 11.

You'd still need to install npm globally for now, but we're working on getting things packaged as a binary so iOS devs don't have to fight with it.

The instructions for this with Xcode 11 haven't been updated as Xcode 11 is still in Beta, but I'm gonna be honest: You're almost certainly better off using Swift Package Manager with Xcode 11.

You'd still need to install npm globally for now, but we're working on getting things packaged as a binary so iOS devs don't have to fight with it.

Hey Designatednerd, I have made a new project using SPM. It is a WatchOS6 project.
I have added the dependencies in the package.swift file and I have run this command in the terminal to make the project:

‘swift package init --type=executable’

this is the dependencies I added

package(url: "https://github.com/apollographql/apollo-ios.git", .exact("0.15.0"))
 .target(
    name:"Apollo",
   dependencies:[]), 

The script I am using is:

cd "${BUILD_ROOT}"
cd "../../SourcePackages/checkouts/apollo-ios/scripts"

APOLLO_SCRIPT_PATH="$(pwd)"

if [ -z "${APOLLO_SCRIPT_PATH}" ]; then
    echo "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}"
"${APOLLO_SCRIPT_PATH}"/check-and-run-apollo-cli.sh codegen:generate --target=swift --includes=./**/*.graphql --localSchemaFile="schema.json" API.swift

The script Is named Generate Apollo GraphQL API and I dragged it over compile sources.

There is a scheme.json file and a .qraphql file in my project too.

But I am getting this error:

/Users/phillipeismark/Library/Developer/Xcode/DerivedData/ApolloOS6-bezgzvgntqbivvbapfewgxqetshn/Build/Intermediates.noindex/ApolloOS6.build/Debug-watchsimulator/ApolloOS6 WatchKit Extension.build/Script-76B153D6231FC77200CB4377.sh: line 15: /Users/phillipeismark/Library/Developer/Xcode/DerivedData/ApolloOS6-bezgzvgntqbivvbapfewgxqetshn/Build/Products/check-and-run-apollo-cli.sh: No such file or directory

In Xcode 11, you can add swift packages directly without having to do all the nonsense with the command line. The SPM Beta instructions are designed for this setup rather than the method you've used, so that may be why it's not finding things properly.

In Xcode 11, you can add swift packages directly without having to do all the nonsense with the command line. The SPM Beta instructions are designed for this setup rather than the method you've used, so that may be why it's not finding things properly.

Okay, I am starting to get a better understanding about all of this, but is there a resource with all the information? I tried with CocoaPods first as the guide was saying, then I found out that didn't work unless you had something called NPM installed, and then I found out CocoaPods don't work with Xcode 11 beta, so I used SPM, and now I'm finding out that I should use SPM through Xcode and not the terminal.
I really want to find a place with all of this information, where does people using Xcode beta figure this out? I have spent 4 days trying to setup Apollo in my program because I just haven't been able to find any good information about how to do this.

If not maybe I should write a quick guide somewhere for people like me if I figure it out.

By the way my approach to making the project now is:
1: create new watchOS project, pick Watch app and swiftUI
2: navigate to folder in the terminal and run ‘swift package init --type=executable’
3: add dependencies to the package.swift file
4: add the scheme.json to the project
5: make a .graphql file with a simple query inside
6: add the script in build phase, drag it over compile sources and name it Generate Apollo GraphQL API
7: write 'swift package generate-xcodeproj' in the console
8: use this line 'sudo xcode-select -switch /Applications/Xcode-Beta.app/Contents/Developer'

and here is where I currently get the error:
Showing All Messages

Build target WatchOS6ApolloGraphql of project WatchOS6ApolloGraphql with configuration Debug
error: unable to resolve product type 'com.apple.product-type.tool' for platform 'watchsimulator' (in target 'WatchOS6ApolloGraphql' from project 'WatchOS6ApolloGraphql')error: unable to resolve product type 'com.apple.product-type.tool' for platform 'watchsimulator' (in target 'WatchOS6ApolloGraphql' from project 'WatchOS6ApolloGraphql')

I am reading this post to try and fix the issue. but am currently stuck here.

SPM will not work without NPM either. Let me try to share some details:

  • SPM is the _Swift Package Manager_. It is used for pulling in the dependencies of a Swift project. It has existed since Swift 2, but has only been integrated into Xcode with Xcode 11.

    Previously, you had to use command line to add or manage packages. With Xcode 11, you do not need to, and in fact should not, use the command line for this. You should add packages through the File > Swift Packages > Add package dependency... menu item.

    This ease of use with Xcode will lead us to change our recommendation starting when Xcode 11 gets out of beta, but since it's still in beta, we have not changed our recommendation yet.

  • NPM is the _Node Package Manager_. This is necessary because right now, code based on your .graphql files is generated using a javascript package. As you've seen, this can lead to some serious Dependency Hell. We are working on getting this packaged as a binary so iOS developers don't have to mess with NPM anymore, but that project is not yet complete.

Any dependency management solution for iOS you use (SPM, CocoaPods, or Carthage, or even just dragging files into your project) will need NPM support until the binary is ready to roll. Longer-term, we are looking to move the actual "generation" part of code generation to Swift, you can follow that project if you'd like updates on it.

I'm sorry the docs are such a moving target - this is definitely something I plan to address in the next few weeks, particularly as Xcode 11 (hopefully) goes to Gold Master.

Thank you so much for the clarification, this was very helpfull.
I have now installed Apollo through the integrated SPM and not the terminal.

Now I get an error that looks more fixable:

Loading Apollo Project [failed] → Error initializing Apollo GraphQL project "Unnamed Project": Error: Error in "Loading schema for Unnamed Project": TypeError: Cannot read property 'reduce' of undefined › Error: Error initializing Apollo GraphQL project "Unnamed Project": Error: › Error in "Loading schema for Unnamed Project": TypeError: Cannot read › property 'reduce' of undefined Command PhaseScriptExecution failed with a nonzero exit code

What I have tried:
1: I have a scheme in the project called schema.json
2: Clean the build folder
3: Delete node_modules folder
4: Check my .graphql file with queries for syntax error with this tool toolkit
5: I have had the schema.json file in this path: /Users/phillipeismark/sandbox/GraphQL/IOS/WatchOS6IntegratedSPM
And this path:
/Users/phillipeismark/sandbox/GraphQL/IOS/WatchOS6IntegratedSPM/WatchOS6IntegratedSPM WatchKit Extension
And more specific paths like:
/Users/phillipeismark/sandbox/GraphQL/IOS/WatchOS6IntegratedSPM/WatchOS6IntegratedSPM WatchKit Extension/schema.json

Mys script looks like this:

cd "${BUILD_ROOT}"
cd "../../SourcePackages/checkouts/apollo-ios/scripts"

APOLLO_SCRIPT_PATH="$(pwd)"


if [ -z "${APOLLO_SCRIPT_PATH}" ]; then
    echo "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}"
"${APOLLO_SCRIPT_PATH}"/check-and-run-apollo-cli.sh codegen:generate --target=swift --includes=./**/*.graphql --localSchemaFile="schema.json" API.swift

I noticed that the project can run when I put this in the middle and the bottom of the script:
echo $(pwd)

But I suspect it is because it ruins the script so it doesn't run.

You'd still need to install npm globally for now, but we're working on getting things packaged as a binary so iOS devs don't have to fight with it.

For what it's worth; the firebase cli is written in javascript too, but is bundled as a single binary.

This can be done with tools like https://github.com/zeit/pkg or https://github.com/nexe/nexe, and would thus not require users to do the npm dance, which could be a short term solution.

Although I can see having parser/generator for code in a single place, in a single language has its merits too, so maybe having it as a single binary doesn't require the codegen to be rewritten in swift.

Edit; or since the cli is using oclif, oclif-dev pack could be a solution, but it's not as straightforward as just running that command.

@koenpunt yeah the tooling team is working on something for this, hoping to land it soon!

@PhillipFraThailand Can you confirm which beta of Xcode you're using?

I Am using version 11.0 beta 6

OK, so I just tried it out with 11.0 beta 7 on Catalina Beta 7, and everything seems to work fine on my end.

I also ruled out the possibility it's because you've got spaces in your project path - I created a folder and project called SPM Space Test and everything worked fine.

It seems like the script is dying importing your schema from the error message - can you validate that your schema.json is valid JSON? This is usually what I use for that.

If it's valid JSON, I would then check:

  • Are all your GraphQL files under the correct folder? My file tree looks something like this:
    | - Containing Folder | - node_modules | (all node modules installed locally) | Project.xcodeproj | - ProjectFolder | AppDelegate.swift | Assets.xcassets | schema.json | HeroName.graphql | Info.plist | - AnyOtherFolder | MoreQueries.graphql
  • Is there a property called reduce somewhere in your schema that you are querying? It's possible this might be confusing the hell out of something.

If it's not any of these, could you please either upload your schema as an attachment here or drop it to me in an email at ellen at apollographql dot com? Thanks!

Hey there is no property called reduce.

The JSON file passed the tool.

My tree looks like this:

image

Theres currently two json files, but I have tried with only having one or the other.

I can't attach the schema here so I'll send you a mail with it.

Thanks for your help.

Hey @PhillipFraThailand, I'm sorry I got sidetracked on this. I opened the file you sent me and it appears the schema format is out of date. Compare your format to that of the Star Wars API in this test repo.

Did you download this from the iGraphQL tool or using the Apollo CLI? My understanding is iGraphQL does not provide the correct format.

Hey @PhillipFraThailand, I'm sorry I got sidetracked on this. I opened the file you sent me and it appears the schema format is out of date. Compare your format to that of the Star Wars API in this test repo.

Did you download this from the iGraphQL tool or using the Apollo CLI? My understanding is iGraphQL does not provide the correct format.

Hey Ellen, sorry I got caught up in some other stuff too. I actually ended up making the queries myself. Apollo is still on the table but for now I'm good with just doing one call and then i'll return when I need to make more than just one. I found out that the query that works in the tool I checked the query with added stuff to the call behind the scenes, so I had to make my HTTP request look like this:
{"operationName":null,"variables":{},"query":"{\n artist(id: \"29174892\") {\n name\n id\n }\n}\n"}
I am not sure what this information might help with, but I think its worth putting out there for people as confused as me also trying to copy paste the query into a homemade request.

Ok, no worries - I'd still love to know where you got the schema, so I can see if maybe that might be the issue.

Ok, no worries - I'd still love to know where you got the schema, so I can see if maybe that might be the issue.

Hey Ellen, I will mail you the link as im not sure if I am allowed to share it publicly.

I'm gonna go ahead and close this issue out - it looks like the schema was downloaded directly from GraphiQL, which for JSON produces an outdated format which our CLI doesn't handle, unfortunately. If you're having similar problems and you got your schema from GraphiQL, try downloading it in SDL rather than in JSON.

Was this page helpful?
0 / 5 - 0 ratings