I am newer to iOS & GraphQL that said I have done my best to follow the docs and get a working version of the latest SDK even though there seem to be no tutorials online for the newer version.
Here are some rough steps I went through…
- Install Rover
- Generate schema
$ rover graph introspect http://localhost:8080/graphql --output plain > ios/schema.graphqls
- Manually Install the apollo-ios-cli because a github issue lead me to that being the best path forward.
- Generate apollo-codgen-config.json
$ ./apollo-ios-cli init --schema-name BackendAPI --module-type embeddedInTarget --target-name ios
- Generate swift files based on schema
$ ./apollo-ios-cli generate
It seems to work and creates the following files/folders:
- BackendAPI.graphql.swift
- Operations/ …
- Schema/ …
Then I try to use it with something like…
import Foundation
import Apollo
extension MoreView {
class MoreViewModel: ObservableObject {
let client = ApolloClient(url: URL(string: "http://localhost:8080/graphql")!)
init() {
client.apollo.fetch(query: MeQuery()) { result in
guard let data = try? result.get().data else { return }
print(data.me.displayName) // Luke Skywalker
}
}
}
}
However, no matter what I try regarding adding files to the project/target I always get “Cannot find ‘MeQuery’ in scope”
Please help
Hi @afgarcia86
I am newer to iOS & GraphQL that said I have done my best to follow the docs and get a working version of the latest SDK even though there seem to be no tutorials online for the newer version.
That’s correct, we haven’t updated the iOSTutorial repo to be compatible with 1.0. We are working on a replacement but feel that the 0.x tutorial focused too much on the mechanics of building an iOS app rather than focusing on the how and why of adding GraphQL to an iOS app.
- Generate apollo-codgen-config.json
$ ./apollo-ios-cli init --schema-name BackendAPI --module-type embeddedInTarget --target-name ios
Because you used the .embeddedInTarget
module type you will need to manually add all the generated files to your project. Once those are in the project you should have no problem referencing the generated types.
If you still get the same build error check to see what query types were generated and that MeQuery
is the correct one to be using.
Hi @calvincestari thanks for getting back to me so quickly… I figure I am just missing something dumb here but maybe this screenshot will help determine if the MyQuery generation looks right. It does to me
Again being a newb at iOS I may not be adding the files to the project correctly, but I selected “add files to ios” and added the BackendAPI folder as a group.
Currently I don’t have the actual schema.graphqls
and Me.graphql
in the iOS project but I have tried both ways and that doesn’t seem to be related.
Thanks again, I was about to give up hope and go for the legacy package but I would hate to do that if I am not to far off.
Again being a newb at iOS I may not be adding the files to the project correctly, but I selected “add files to ios” and added the BackendAPI folder as a group.
It looks like you’ve done it correctly.
Currently I don’t have the actual schema.graphqls
and Me.graphql
in the iOS project but I have tried both ways and that doesn’t seem to be related.
No, you shouldn’t need those files in your project unless you want them there for ease of editing. If you add them though be sure they’re not included in any of the application targets because they don’t need to be deployed with your application.
Thanks again, I was about to give up hope and go for the legacy package but I would hate to do that if I am not to far off.
When using .embeddedInTarget
the generated files are enclosed in a caseless enum for namespacing; to reduce potential for type naming conflicts. Try BackendAPI.MeQuery()
instead of just MeQuery()
in your view model.
1 Like
That was it! I could have sworn I tried that! Thanks for the help
No problem, glad you’ve got it working.