Apollo-ios: API.swift generated class is empty

Created on 29 Oct 2016  路  13Comments  路  Source: apollographql/apollo-ios

I have followed the steps on the installation page and have a compiling project. I have also imported the proper schema.json from my server, but the script seems to generate a blank API.swift file. Am I missing something here?

I am using Cocoapods to install the latest version (0.3.1 I believe), and have installed apollo-codegen from npm. Any help would be appreciated!

All 13 comments

Hmmm, do you have any queries defined yet in .graphql files? Because Apollo iOS creates query-specific result types, it needs queries to generate code from.

If you have written some queries, where are your .graphql files located? They'll have to be stored inside the project directory (or you would have to change the apollo-codegen invocation in the run script build step).

yep, that looks like the problem. Just had the question answered on slack. Thanks for the quick response! Really looking forward to the future of this library 馃槃

I'm still getting an empty API.swift. I've checked out the Slack channel by the way.

My .graphql as well as the schema.json file resides in a folder called data right under my project folder.
So I modified $APOLLO_FRAMEWORK_PATH/check-and-run-apollo-codegen.sh generate $(find . -name '*.graphql') --schema schema.json --output API.swift with $APOLLO_FRAMEWORK_PATH/check-and-run-apollo-codegen.sh generate $(find . -name '*.graphql') --schema data/schema.json --output API.swift

My schema has been used a few days ago in my React Native/Relay app, so I'm pretty sure my .graphql is standard. Any possible solutions?

So just to make sure, your .graphql files contain queries?

Are there any error messages when you build your target?

If you've copied the default build script, it expects both your schema.json and the .graphql files in the target directory:

cd ${SRCROOT}/${TARGET_NAME}

So if you've put them in the parent project directory instead, that may be the issue. But I would have expected an error message in that case.

Sorry, I haven't been doing iOS dev for about a year, so its a bit rusty.
So I've gotten rid of that data directory I've mentioned above.

My target directory is called MusicApp, and the actual directory structure is like this:

  -> API.swift
  -> AppDelegate.swift
  -> schema.graphql
  -> schema.json
Pods

I haven't got any errors when building and running the script as is which I got from the docs.

My schema contains stuff like:

schema {
  query: RootQueryType
}

enum AlbumType {
  SINGLE
  EP
  ALBUM
  MAXI_ALBUM
  PROMO
}

type ArticleConnection {
  pageInfo: PageInfo!
  edges: [ArticleEdge]
  count: Int
}

etc.

That looks good, except for the schema.graphql. The schema should be in schema.json, and .graphql files are then used to define queries and mutations that use the schema.

apollo-codegen generates query-specific types, that only contain the fields you actually query for. So without any queries, API.swift will indeed be empty.

You may want to take a look at the FrontPage example app.

Ah, OK, that example app helped me out a lot. Thank you 馃枛. I didn't quite understand what it meant by *.graphql since my GraphQL outputs a schema.json and schema.graphql by default whenever I push a release.

So if there is a SoundtrackViewController.swift there should be a SoundtrackViewController.graphql in the target project directory, correct?

You can organize your .graphql files any way you want, and they will all be processed together. But it is a best practice to keep your queries together with the components that use them.

An added benefit of that is that we also have an Xcode integration that shows you the corresponding .graphql file for a Swift file in a companion view so you can edit them side by side. See the short screen recording at the end of this blog post for an example.

I have written some query in my .graphql file but still my API.swift is empty ..can anyone help?

I solved my problem. Actually my .graphql file was outside of project directory hence API.swift file was empty.

I didn't get the solution ....Anyone please help me

hi guys, new to apollo-ios, i still cant generate code in my API.swift, my .graphql file and schema.json are already under my project folder, any other suggestions as to this issue?

In some cases if the syntax of queries/mutations is incorrect, you don't get any error on compile time but an empty API.swift file. Make sure that your syntax is correct.

Was this page helpful?
0 / 5 - 0 ratings