Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beginner - Apollo installation page appears to be out of date. Can I help? #1233

Closed
lastneutrino opened this issue May 29, 2020 · 25 comments
Closed
Labels
docs Focuses on documentation changes
Milestone

Comments

@lastneutrino
Copy link

I set up Apollo server, very easy. I was able to install the iOS client package dependencies, but the rest of the instructions at

https://www.apollographql.com/docs/ios/installation/

didn't match the latest xcode and apollo-ios client. If someone could work with me to get the client going, I would be glad to write a new tutorial for iOS client, and include more examples.

@designatednerd
Copy link
Contributor

Hi, where are you running into problems? It would help me fix the docs to know where something's not working.

@designatednerd
Copy link
Contributor

There also is already a step by step tutorial available here.

@lastneutrino
Copy link
Author

lastneutrino commented May 30, 2020

The step by step example seems to be more complete, I will try and work through that - is there a reason why this isn't linked instead of the current main page? I will work through it and capture any issues. Thank you very for the help, I'm glad to help as much as I can in return.

@lastneutrino
Copy link
Author

I'm getting the error below in xcode when attempting the build. I removed the package dependency (from the original install) and reinstalled, adding web sockets as indicated, and also the code generation, but still see this error. It looks like it's not getting through the first few lines of the script - but not clear (at least to me) what the script is looking for.

error: Unable to locate SourcePackages directory from BUILD_ROOT: '/Users/lastneutrino/Library/Developer/Xcode/DerivedData/Build/Products'

@designatednerd
Copy link
Contributor

is there a reason why this isn't linked instead of the current main page?

I updated the intro page yesterday to make the link to the tutorial more prominent.

It looks like it's not getting through the first few lines of the script - but not clear (at least to me) what the script is looking for.

The script is looking through the DerivedData directory to try and find the SourcePackages directory, which is where SPM checks out the repositories for all dependencies. Because shell scripts can't be included in SPM dependencies directly, it has to look through the Git checkout to find the shell script it needs to run.

Usually there's a YourProject-aBunchOfGibberish folder between DerivedData and Build. If you look in that folder using Finder, there should be a SourcePackages folder at the same level as Build, and that should be where your SPM packages are checked out. I would take a closer look at where the BUILD_ROOT path is coming from if it doesn't include that YourProject-aBunchOfGibberish folder. If it's not there, I'd double check in Xcode's Preferences > Locations panel where your derived data is set up to be output.

Tangentially, this whole process is ultimately part of why we're switching to Swift-based scripting (currently in beta) in the long run, because treasure hunting for the source packages in the derived data directory is not a super-stable way of doing this.

@lastneutrino
Copy link
Author

lastneutrino commented Jun 2, 2020

Build doesn't look to be at the sibling level, is something else wrong?

% pwd
/Users/lastneutrino/Library/Developer/Xcode/DerivedData/MyProject-aBunchOfGibberish
% find . -name "SourcePackages"
./SourcePackages
./SourcePackages/checkouts/apollo-ios/Tests/ApolloCodegenTests/SourcePackages
% find . -name "Build"
./Logs/Build

I tried the beta as well - schema.json didn't download from server. There is actually another Codegen directory appearing under Codegen/Sources - I tried to remove it, thinking that might be a redundant redundancy, but it wouldn't build without Sources/Codegen - Package.swift complained. No mention in cli about the Codegen directory scripts failing, or doing anything, will try again tomorrow with a fresh project, I wonder if something else is wrong. Hello world! displaying was the last verified function output.

@designatednerd
Copy link
Contributor

So the script does look at the sibling level - let me break this down a bit. Here's the bash code in question:

# Go to the build root and search up the chain to find the 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"

First line should give you a path like: /Users/lastneutrino/Library/Developer/Xcode/DerivedData/MyProject-aBunchOfGibberish/Build/Products/Platform

while loop 1: Does Platform contain a directory named SourcePacages? No. Go up one directory.
while loop 2: Does Products contain a directory named SourcePackages? No. Go up one directory.
while loop 3: Does Build contain a directory named SourcePackages? No. Go up one directory.
while loop 4: Does MyProject-aBunchOfGibberish contain a directory named SourcePackages? YES. Proceed.

The script then sets the SCRIPT_PATH var based on that path which was found to contain a SourcePackages directory. If that while loop never finds a folder containing a SourcePackages, it will error out when it gets to /, or the top level of the filesystem.

Regarding the Codegen/Sources/Codegen with Swift Package manager, there's two different codegen folders there. The outer Codegen directory is the name of the overall project. The inner Codegen directory contains the actual source files for the Codegen executable that you're creating with that project. This is Swift Package Manager convention. I'll update the file tree examples in the Swift Scripting to include this folder as well.

@lastneutrino
Copy link
Author

Intermediate - if i keep the 27.0 as a dependency, i get the following error, which is removed when depending on 28.0 instead. I'm attempting to rebuild from scratch with beta.

Resolving https://github.com/kylef/Spectre.git at 0.9.0
*project/Codegen/.build/checkouts/PathKit/Sources/PathKit.swift:98:14: warning: 'Hashable.hashValue' is deprecated as a protocol requirement; conform type 'Path' to 'Hashable' by implementing 'hash(into:)' instead
public var hashValue: Int {

*project/Codegen/.build/checkouts/Stencil/Sources/Template.swift:11:3: warning: 'internal(set)' modifier is redundant for an internal property
internal(set) var environment: Environment
^~~~~~~~~~~~~~

@designatednerd
Copy link
Contributor

@lastneutrino What version of Swift are you using? As of 5.2 that deprecation should be a warning rather than an error (unless you're doing treat all warnings as errors, which I would not recommend for the scripting project).

@lastneutrino
Copy link
Author

lastneutrino commented Jun 7, 2020

Swift version below. No warnings to errors. Further, I created a new blank single page app. I followed the beta instructions - I'm able to get Hello World, but after that I left "MyProject" as-is which doesn't seem to be created, and no schema.json. I'm able to bring up the playground in the web browser. What am I missing here?

% swift --version
Apple Swift version 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51)
Target: x86_64-apple-darwin19.4.0

% find . -name "ApolloCLI"
./TestCodegen/Codegen/.build/checkouts/apollo-ios/SwiftScripts/ApolloCLI
% ls ./TestCodegen/Codegen/.build/checkouts/apollo-ios/SwiftScripts/ApolloCLI
%

% find . -name "schema.json"
./TestCodegen/Codegen/.build/checkouts/apollo-ios/Sources/GitHubAPI/schema.json
./TestCodegen/Codegen/.build/checkouts/apollo-ios/Sources/StarWarsAPI/schema.json
%

import ApolloCodegenLib
print("Hello, world!")

let parentFolderOfScriptFile = FileFinder.findParentFolder()
let sourceRootURL = parentFolderOfScriptFile
.deletingLastPathComponent() // Result: Sources folder
.deletingLastPathComponent() // Result: Codegen folder
.deletingLastPathComponent() // Result: MyProject source root folder

let cliFolderURL = sourceRootURL
.appendingPathComponent("Codegen")
.appendingPathComponent("ApolloCLI")

let endpoint = URL(string: "http://localhost:4000/graphql")!

let output = sourceRootURL
.appendingPathComponent("MyProject")

try FileManager
.default
.apollo_createFolderIfNeeded(at: output)

let options = ApolloSchemaOptions(endpointURL: endpoint,
outputFolderURL: output)

do {
try ApolloSchemaDownloader.run(with: cliFolderURL,
options: options)
} catch {
exit(1)
}

@designatednerd
Copy link
Contributor

The find commands you're using are finding things within the .build folder, which means they're finding only bits which have been checked out with the apollo-ios repo.

So I would take a look at how you've got your project set up. This is roughly the structure you're going for:

MyProject // Source Root
  | MyProject.xcodeproj
  | - MyProject // Contains app target source files
  | - Codegen // Contains Swift Scripting files
      | Package.swift
      | README.md
      | - Sources
          |- Codegen
              | main.swift

The ApolloCLI folder will not get created until you actually run the script, and if the script isn't running, the schema won't get downloaded. Am I correct that the script is still not running, given the errors you're receiving above?

@lastneutrino
Copy link
Author

I do have the structure you are indicating.

The implication I get from the beta page from "Build and run using the Xcode project. Note that if you're on Catalina you might get a warning asking if your executable can access files in a particular folder like this:" is that building, in this case, "TestCodegen", would perform 'swift run'?

Assuming that assumption is not true, if I run 'swift run' in the same directory as the main.swift file, I get errors related to URL and FileManager not found. If I import the Foundation class, I get the following errors if I run 'swift run':

TestCodegen/Codegen/Sources/Codegen/main.swift:23:2: error: value of type 'FileManager' has no member 'apollo_createFolderIfNeeded'
.apollo_createFolderIfNeeded(at: output)

is this because I'm running 28.0? Am I correct in thinking that the build will not run this script automagically?

@designatednerd
Copy link
Contributor

designatednerd commented Jun 8, 2020

Aha, a couple of things:

  • Yes, you need to import Foundation - good catch that that's not in the instructions, will update.
  • When swift running from the command line, you need to run from the folder where Package.swift lives, otherwise it won't pick up any of your dependencies.
  • If you're editing with Xcode, a scheme that for your codegen executable should be auto-generated so you can just hit the run button to run it in Xcode. Are you not seeing that?
  • In 0.28.0 there were some changes to how we use some of the extensions, I'll make sure that's updated in the tutorial. The short answer is we went to an Rx-style apollo.createFolderIfNeeded style for those extensions where that can be done.

@designatednerd
Copy link
Contributor

(#1252 has those updates)

@lastneutrino
Copy link
Author

lastneutrino commented Jun 8, 2020

I was building the top level project, not the Codegen project. If the codegen executable is expected to be invoked when the top level project is built, that's not happening. The ApolloCLI folder is populated when I build the Codegen project, not the top level project.

As a result of this, the ApolloCLI directory is populating, I see the .zip file, but not the schema.json. The CLI log on the first run reported this, but when I re-run, I do not see output although "All Output" is selected.

Output of the CLI when shut down, restarted and re-built Codegen:


Hello, world!
[DEBUG - ApolloCodegenLib:CLIDownloader.swift:43] - Zip file with the CLI is already downloaded!
[DEBUG - ApolloCodegenLib:CLIExtractor.swift:57] - Binary already extracted!

@designatednerd
Copy link
Contributor

designatednerd commented Jun 9, 2020

The schema.json will be in whatever folder you told it to download to - do you mind posting the code?

Also, can you clarify what you mean by "Top level project" - if you can post a screenshot of the generated schemas with the one you're using selected, that'd really help disambiguate.

@lastneutrino
Copy link
Author

Top level project == MyProject which is TestCodegen in my example.

Are you looking for main.swift? It's below. I can upload the whole TestCodegen directory as well.

Note that I ran the Codegen project to get ApolloCLI populated, but TestCodegen did not run the script.

Note that I am not seeing the schema.json indicated in the CLI log, and a find command does not locate one.


import Foundation
import ApolloCodegenLib

print("Hello, world!")

let parentFolderOfScriptFile = FileFinder.findParentFolder()
let sourceRootURL = parentFolderOfScriptFile
  .deletingLastPathComponent() // Result: Sources folder
  .deletingLastPathComponent() // Result: Codegen folder
  .deletingLastPathComponent() // Result: MyProject source root folder

let cliFolderURL = sourceRootURL
.appendingPathComponent("Codegen")
.appendingPathComponent("ApolloCLI")

let endpoint = URL(string: "http://localhost:4000/graphql")!

let output = sourceRootURL
.appendingPathComponent("MyProject")

try FileManager
.default
    .apollo.createFolderIfNeeded(at: output)

let options = ApolloSchemaOptions(endpointURL: endpoint,
outputFolderURL: output)

do {
  try ApolloSchemaDownloader.run(with: cliFolderURL,
                                 options: options)
} catch {
  exit(1)
}

@lastneutrino
Copy link
Author

Here's the Codegen project:

image

@designatednerd
Copy link
Contributor

So from the code you posted, taking a look at this line:

let output = sourceRootURL
  .appendingPathComponent("MyProject")

If sourceRootURL is your outer MyProject directory, then the schema.json will not be downloaded into the ApolloCLI folder, or anywhere within the Codegen project. This is from the instructions, because you want it to be somewhere you can get to it easily from within your main project. It should be here:

MyProject // SourceRoot
  | MyProject.xcodeproj
  | - MyProject // Contains app target source files
      | schema.json // <--- This is where the schema.json is set to download in your code
  | - Codegen // Contains Swift Scripting files
      | Package.swift
      | README.md
      | - ApolloCLI // Contains downloaded typescript CLI
      | - Sources  
          | - Codegen    
              | main.swift

@lastneutrino
Copy link
Author

That's a mistake on my part - that part should have been the following:

let output = sourceRootURL
.appendingPathComponent("TestCodegen")

for some reason - the CLI started responding with that the introspection request was HTML instead of JSON. I added introspection: true to the server, /graphql to the endpoint, and the schema finally appeared. I backed off both the introspection variable and the endpoint modification, and it's still downloading the schema. I'm not quite sure why the CLI started responding or why it suddenly started working.

@designatednerd
Copy link
Contributor

Yeah, I'm not sure - it does sound like something changed on the server side if the same code suddenly started working. No idea what, though. Gremlins? 🙃

@lastneutrino
Copy link
Author

I would assume the problem is between the chair and keyboard. I'm wondering if the decision to write to "MyProject", assuming that the directory would be created in parallel, wasn't true, and I changed come client code in between "TestCodegen" and "MyProject". I'll update when I have the complete chain working, and I'll repeat it to make sure it's functioning.

I do see this warning:
Warning: apollo update available from 2.27.4 to 2.28.2.

which is odd, since the package dependency above says 0.28.0. Seems like everything is off there.

@designatednerd
Copy link
Contributor

Yeah that warning is from our JS CLI, which is used under the hood, and which is currently at 2.28.x. I know that's confusing - hopefully as they continue releasing and we continue releasing that will diverge.

@designatednerd designatednerd added the docs Focuses on documentation changes label Jun 12, 2020
@designatednerd
Copy link
Contributor

Please take a look at #1263 - I've made more changes to the swift scripting instructions to make them clearer. Once that gets merged I'll close this issue out, but would love to hear any feedback. Thanks!

@designatednerd
Copy link
Contributor

Updated swift scripting docs have shipped! I'm going to close this issue out since it's so general, if you have additional problems, please open a new issue with specific details on where you're having a problem. Thank you!

@designatednerd designatednerd added this to the 0.29.0 milestone Jul 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Focuses on documentation changes
Projects
None yet
Development

No branches or pull requests

2 participants